functions/knowledge/Read-PSUKnowledge.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function Read-PSUKnowledge
{
<#
    .SYNOPSIS
        Searches for knowledge.
     
    .DESCRIPTION
        Searches for knowledge.
        Generally, knowledge must first be generated using Write-PSUKnowledge.
        This allows these functions to server as a searchable notes section right within your console.
     
        However, there might be some other ways to seek knowledge ...
     
    .PARAMETER Name
        The name of the knowledge entry.
     
    .PARAMETER Tags
        Tags to search by. At least one of the specified tags must be contained.
     
    .PARAMETER Pattern
        Search Name and text of the page by using this regex pattern.
     
    .PARAMETER Book
        The book to search in.
        By default you only have one and don't need to worry about this.
     
    .PARAMETER Online
        Mysterious parameter. I wonder what it does ...
     
    .EXAMPLE
        PS C:\> Read-PSUKnowledge
     
        Lists all knowledge entries.
     
    .EXAMPLE
        PS C:\> Read-PSUKnowledge -Tags DNS
     
        Lists all knowledge entries with the tag "DNS"
     
    .EXAMPLE
        PS C:\> read -p ldap
     
        Lists all knowledge entries with the string "ldap" in name or text.
#>

    [CmdletBinding()]
    param (
        [parameter(Position = 0)]
        [Alias('Page')]
        [string]
        $Name = '*',
        
        [string[]]
        $Tags,
        
        [Alias('p','f','filter')]
        [string]
        $Pattern = '.',
        
        [string]
        $Book = '*',
        
        [switch]
        $Online
    )
    
    begin
    {
        $libraryPath = Get-PSFConfigValue -FullName 'PSUtil.Knowledge.LibraryPath'
    }
    process
    {
        # Gimmick: Search in wikipedia
        if ($Online)
        {
            $url = "https://en.wikipedia.org/wiki/Special:Search/$Name"
            Start-Process $url
            return
        }
        
        if (-not (Test-Path -Path $libraryPath)) { return }
        
        foreach ($bookFile in (Get-ChildItem -Path $libraryPath -Filter *.json))
        {
            $bookName = [System.Text.Encoding]::UTF8.GetString(([convert]::FromBase64String($bookFile.BaseName)))
            if ($bookName -notlike $Book) { continue }
            
            $data = Get-Content -Path $bookFile.FullName | ConvertFrom-Json
            
            foreach ($page in $data)
            {
                if ($page.Name -notlike $Name) { continue }
                if ($Tags -and -not ($Tags | Where-Object { $_ -in $page.Tags })) { continue }
                
                if ($Pattern -ne '.')
                {
                    $matched = $false
                    if ($page.Name -match $Pattern) { $matched = $true }
                    elseif ($page.Text -match $Pattern) { $matched = $true }
                    if (-not $matched) { continue }
                }
                
                $page | Select-PSFObject -KeepInputObject -TypeName 'PSUtil.Knowledge.Page'
            }
        }
    }
}
Import-PSUAlias -Name 'read' -Command Read-PSUKnowledge
Import-PSUAlias -Name 'rdk' -Command Read-PSUKnowledge
Import-PSUAlias -Name 'page' -Command Read-PSUKnowledge
Import-PSUAlias -Name 'learn' -Command Read-PSUKnowledge