1+ function Read-PSUKnowledge
2+ {
3+ <#
4+ . SYNOPSIS
5+ Searches for knowledge.
6+
7+ . DESCRIPTION
8+ Searches for knowledge.
9+ Generally, knowledge must first be generated using Write-PSUKnowledge.
10+ This allows these functions to server as a searchable notes section right within your console.
11+
12+ However, there might be some other ways to seek knowledge ...
13+
14+ . PARAMETER Name
15+ The name of the knowledge entry.
16+
17+ . PARAMETER Tags
18+ Tags to search by. At least one of the specified tags must be contained.
19+
20+ . PARAMETER Pattern
21+ Search Name and text of the page by using this regex pattern.
22+
23+ . PARAMETER Book
24+ The book to search in.
25+ By default you only have one and don't need to worry about this.
26+
27+ . PARAMETER Online
28+ Mysterious parameter. I wonder what it does ...
29+
30+ . EXAMPLE
31+ PS C:\> Read-PSUKnowledge
32+
33+ Lists all knowledge entries.
34+
35+ . EXAMPLE
36+ PS C:\> Read-PSUKnowledge -Tags DNS
37+
38+ Lists all knowledge entries with the tag "DNS"
39+
40+ . EXAMPLE
41+ PS C:\> read -p ldap
42+
43+ Lists all knowledge entries with the string "ldap" in name or text.
44+ #>
45+ [CmdletBinding ()]
46+ param (
47+ [parameter (Position = 0 )]
48+ [Alias (' Page' )]
49+ [string ]
50+ $Name = ' *' ,
51+
52+ [string []]
53+ $Tags ,
54+
55+ [Alias (' p' , ' f' , ' filter' )]
56+ [string ]
57+ $Pattern = ' .' ,
58+
59+ [string ]
60+ $Book = ' *' ,
61+
62+ [switch ]
63+ $Online
64+ )
65+
66+ begin
67+ {
68+ $libraryPath = Get-PSFConfigValue - FullName ' PSUtil.Knowledge.LibraryPath'
69+ }
70+ process
71+ {
72+ # Gimmick: Search in wikipedia
73+ if ($Online )
74+ {
75+ $url = " https://en.wikipedia.org/wiki/Special:Search/$Name "
76+ Start-Process $url
77+ return
78+ }
79+
80+ if (-not (Test-Path - Path $libraryPath )) { return }
81+
82+ foreach ($bookFile in (Get-ChildItem - Path $libraryPath - Filter * .json))
83+ {
84+ $bookName = [System.Text.Encoding ]::UTF8.GetString(([convert ]::FromBase64String($bookFile.BaseName )))
85+ if ($bookName -notlike $Book ) { continue }
86+
87+ $data = Get-Content - Path $bookFile.FullName | ConvertFrom-Json
88+
89+ foreach ($page in $data )
90+ {
91+ if ($page.Name -notlike $Name ) { continue }
92+ if ($Tags -and -not ($Tags | Where-Object { $_ -in $page.Tags })) { continue }
93+
94+ if ($Pattern -ne ' .' )
95+ {
96+ $matched = $false
97+ if ($page.Name -match $Pattern ) { $matched = $true }
98+ elseif ($page.Text -match $Pattern ) { $matched = $true }
99+ if (-not $matched ) { continue }
100+ }
101+
102+ $page | Select-PSFObject - KeepInputObject - TypeName ' PSUtil.Knowledge.Page'
103+ }
104+ }
105+ }
106+ }
107+ Import-PSUAlias - Name ' read' - Command Read-PSUKnowledge
108+ Import-PSUAlias - Name ' rdk' - Command Read-PSUKnowledge
109+ Import-PSUAlias - Name ' page' - Command Read-PSUKnowledge
110+ Import-PSUAlias - Name ' learn' - Command Read-PSUKnowledge
0 commit comments