Functions/StreamDeck/Clear-StreamDeckProfile.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 Clear-StreamDeckProfile
{
    <#
    .Synopsis
        Clears StreamDeck Profiles
    .Description
        Clears rows or columns from a StreamDeck profile. By default, clears an entire profile.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
    # The name of one or more profiles
    [Parameter(Mandatory)]
    [string[]]
    $ProfileName,

    # The root directory to look for profiles.
    [string]
    $ProfileRoot,

    # One or more rows to clear
    [Parameter(ValueFromPipelineByPropertyName)]    
    [string[]]
    $Row,

    # One or more columns to clear
    [Parameter(ValueFromPipelineByPropertyName)]
    [string[]]
    $Column,

    # One or more action UUIDs to clear
    [Parameter(ValueFromPipelineByPropertyName)]
    [string[]]
    $UUID
    )

    

    begin {
        $sdProfiles = Get-StreamDeckProfile -ProfileRoot $ProfileRoot | 
            Where-Object Name -In $ProfileName

        if (-not $sdProfiles) {
            Write-Error "StreamDeck Profile $ProfileName not found"           
        }
    }

    process {
        if (-not $UUID -and -not $Row -and -not $Column) {
            $row, $Column = '*', '*'
        }

        if ($UUID) {
            foreach ($prof in $sdProfiles) {
                $prof.Actions.psobject.Properties |
                    Where-Object { $_.Value.UUID -in $UUID } | 
                    Select-Object -ExpandProperty Name | 
                    ForEach-Object { $c, $r = $_ -split ','; $Row += $r; $Column += $c }
            }
        }

        if ($Row -and $Column) {
            foreach ($prof in $sdProfiles) {
                foreach ($r in $row) {
                    foreach ($c in $Column) {
                        if ($PSCmdlet.ShouldProcess("Remove Action at $r, $c")) {
                            $prof.RemoveAction($R, $C)
                            $prof.Save()
                        }
                    }
                }                
            }
        }
    }
}