functions/message/Remove-PSFMessageLevelModifier.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
function Remove-PSFMessageLevelModifier
{
    <#
        .SYNOPSIS
            Removes a message level modifier.
         
        .DESCRIPTION
            Removes a message level modifier.
     
            Message Level Modifiers can be created by using New-PSFMessageLevelModifier.
            They are used to emphasize or deemphasize messages, in order to help with debugging.
         
        .PARAMETER Name
            Name of the message level modifier to remove.
         
        .PARAMETER Modifier
            The actual modifier to remove, as returned by Get-PSFMessageLevelModifier.
         
        .PARAMETER EnableException
            This parameters disables user-friendly warnings and enables the throwing of exceptions.
            This is less user friendly, but allows catching exceptions in calling scripts.
         
        .EXAMPLE
            PS C:\> Get-PSFMessageLevelModifier | Remove-PSFMessageLevelModifier
     
            Removes all message level modifiers, restoring everything to their default levels.
     
        .EXAMPLE
            PS C:\> Remove-PSFMessageLevelModifier -Name "mymodule.foo"
     
            Removes the message level modifier named "mymodule.foo"
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]]
        $Name,
        
        [Parameter(ValueFromPipeline = $true)]
        [PSFramework.Message.MessageLevelModifier[]]
        $Modifier,
        
        [switch]
        $EnableException
    )
    
    process
    {
        foreach ($item in $Name)
        {
            if ($item -eq "PSFramework.Message.MessageLevelModifier") { continue }
            
            if ([PSFramework.Message.MessageHost]::MessageLevelModifiers.ContainsKey($item.ToLower()))
            {
                [PSFramework.Message.MessageHost]::MessageLevelModifiers.Remove($item.ToLower())
            }
            else
            {
                Stop-PSFFunction -Message "No message level modifier of name $item found!" -EnableException $EnableException -Category InvalidArgument -Tag 'fail','input','level','message' -Continue
            }
        }
        foreach ($item in $Modifier)
        {
            if ([PSFramework.Message.MessageHost]::MessageLevelModifiers.ContainsKey($item.Name))
            {
                [PSFramework.Message.MessageHost]::MessageLevelModifiers.Remove($item.Name)
            }
            else
            {
                Stop-PSFFunction -Message "No message level modifier of name $($item.Name) found!" -EnableException $EnableException -Category InvalidArgument -Tag 'fail', 'input', 'level', 'message' -Continue
            }
        }
    }
}