internal/functions/Remove-DbaMessageLevelModifier.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
function Remove-DbaMessageLevelModifier {
<#
    .SYNOPSIS
        Removes a message level modifier.
     
    .DESCRIPTION
        Removes a message level modifier.
         
        Message Level Modifiers can be created by using New-DbaMessageLevelModifier.
        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-DbaMessageLevelModifier.
     
    .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-DbaMessageLevelModifier | Remove-DbaMessageLevelModifier
         
        Removes all message level modifiers, restoring everything to their default levels.
     
    .EXAMPLE
        PS C:\> Remove-DbaMessageLevelModifier -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)]
        [Sqlcollaborative.Dbatools.Message.MessageLevelModifier[]]
        $Modifier,
        
        [switch]
        $EnableException
    )
    
    process {
        foreach ($item in $Name) {
            if ($item -eq "Sqlcollaborative.Dbatools.Message.MessageLevelModifier") { continue }
            
            if ([Sqlcollaborative.Dbatools.Message.MessageHost]::MessageLevelModifiers.ContainsKey($item.ToLower())) {
                [Sqlcollaborative.Dbatools.Message.MessageHost]::MessageLevelModifiers.Remove($item.ToLower())
            }
            else {
                Stop-Function -Message "No message level modifier of name $item found!" -EnableException $EnableException -Category InvalidArgument -Continue
            }
        }
        foreach ($item in $Modifier) {
            if ([Sqlcollaborative.Dbatools.Message.MessageHost]::MessageLevelModifiers.ContainsKey($item.Name)) {
                [Sqlcollaborative.Dbatools.Message.MessageHost]::MessageLevelModifiers.Remove($item.Name)
            }
            else {
                Stop-Function -Message "No message level modifier of name $($item.Name) found!" -EnableException $EnableException -Category InvalidArgument -Continue
            }
        }
    }
}