Private/ConvertTo-ParameterString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# A helper function to best-effort convert a hashtable that will be splatted, to a param string
# ex. -Body 'Hello World' -Timeout 4
function ConvertTo-ParameterString {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
        [hashtable]
        $InputObject
    )
    $paramsArray = @()
    foreach ($dictionaryEntry in ($InputObject.GetEnumerator() | Sort-Object Key)) {
        $paramsArray += "-$($dictionaryEntry.Key)"

        if ($dictionaryEntry.Value.GetType() -eq [string]) {
            $paramsArray += "'$($dictionaryEntry.Value)'"
        } else {
            $paramsArray += "$($dictionaryEntry.Value)"
        }
    }

    $paramsArray -join ' '
}