Private/ConvertTo-ConnectionParam.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
76
77
78
79
80
Function ConvertTo-ConnectionParam {
    <#
.SYNOPSIS
Converts input parameters to correct format for PAS ConnectionParams

.DESCRIPTION
ConnectionParams consist of "AllowMappingLocalDrives", "AllowConnectToConsole",
"RedirectSmartCards", "PSMRemoteMachine", "LogonDomain" & "AllowSelectHTML5" properties.
If these exist as keys in the input parameters, they are added to the input parameters
as nested hashtable key/value pairs, the top level key/value pairs are not included in the output.

.PARAMETER Parameters
Hashtable containing parameter names and values.

.EXAMPLE
$InputObj = @{
                PSMRemoteMachine = "RemoteMachineValue"
                LogonDomain = "LogonDomainValue"
                SomeProperty = "SomeValue"
            }
$InputObj | ConvertTo-ConnectionParam

Outputs:
$output["ConnectionParams"]["PSMRemoteMachine"]["Value"] = RemoteMachineValue
$output["ConnectionParams"]["LogonDomain"]["Value"] = LogonDomainValue
$output["SomeProperty"] = SomeValue

#>

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ConnectionParams', Justification = "False Positive")]
    [CmdletBinding()]
    [OutputType('System.Hashtable')]
    param(
        [parameter(
            Mandatory = $false,
            ValueFromPipeline = $true
        )]
        [hashtable]$Parameters
    )

    Begin {
        $ConnectionParameters = [Collections.Generic.List[String]]@("AllowMappingLocalDrives", "AllowConnectToConsole",
            "RedirectSmartCards", "PSMRemoteMachine", "LogonDomain", "AllowSelectHTML5")
    }

    Process {

        If ($null -ne $Parameters) {

            #ConnectionParameters are included under the ConnectionParams property of the JSON body
            $Parameters.keys | Where-Object { $ConnectionParameters -contains $PSItem } | ForEach-Object {

                $ConnectionParams = @{ }

            } {

                #For Each ConnectionParams Parameter
                #add key=value to hashtable
                $ConnectionParams.Add($PSItem, @{"value" = $Parameters[$PSItem] })

            } {
                if ($ConnectionParams.keys.count -gt 0) {

                    #if ConnectionParameters have been specified
                    #Add ConnectionParams to boundParameters
                    $Parameters["ConnectionParams"] = $ConnectionParams

                }

                #Remove individual ConnectionParameters from boundParameters
                $Parameters | Get-PASParameter -ParametersToRemove $ConnectionParameters

            }

        }

    }

    End { }

}