functions/knowledge/Remove-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
function Remove-PSUKnowledge
{
<#
    .SYNOPSIS
        Removes pages from the books of your holy console library of knowledge.
     
    .DESCRIPTION
        Removes pages from the books of your holy console library of knowledge.
        Contribute new knowledge by using Write-PSUKnowledge or search it by using Read-PSUKnowlege.
     
    .PARAMETER Name
        The name of the page to rip out.
     
    .PARAMETER Book
        The book of knowledge to deface.
        Defaults to the book specified under the 'PSUtil.Knowledge.DefaultBook' configuration setting.
        It will look for books in your library path, which can be specified under 'PSUtil.Knowledge.LibraryPath'.
     
    .EXAMPLE
        PS C:\> Remove-PSUKnowledge -Name 'DNS for Dummies'
     
        Rips out the page 'DNS for Dummies' from your default book.
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('page')]
        [string[]]
        $Name,
        
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]
        $Book = (Get-PSFConfigValue -FullName 'PSUtil.Knowledge.DefaultBook')
    )
    
    begin
    {
        $libraryPath = Get-PSFConfigValue -FullName 'PSUtil.Knowledge.LibraryPath'
    }
    process
    {
        if (-not (Test-Path -Path $libraryPath)) { return }
        
        $bookName = '{0}.json' -f [System.Text.Encoding]::UTF8.GetBytes($Book)
        $bookPath = Join-Path $libraryPath $bookName
        
        if (-not (Test-Path -Path $bookPath)) { return }
        
        $pageEntries = Get-Content -Path $bookPath | ConvertFrom-Json
        $pageEntries = $pageEntries | Where-Object Name -notin $Name
        $pageEntries | ConvertTo-Json | Set-Content -Path $bookPath
    }
}