Private/Expand-String.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
Function Expand-String {
    [cmdletbinding()]
    Param(
        $String
    )

    $ExpressionPattern = '^\$\(.*\)$'

    if ($String -is [string]) {
        if ($String -match $ExpressionPattern) {
            Write-Debug ("Expand-String: String '{0}' is expression" -f $String)
            $result = Invoke-Expression $String
        }
        else {
            Write-Debug ("Expand-String: String '{0}' is string" -f $String)
            $result = $ExecutionContext.InvokeCommand.ExpandString($String)
        }
    }
    else {
        Write-Debug ("Expand-String: input is not a string, ignoring" -f $String)
        $result = $String
    }

    Return $result

}