Why doesn't $hash.key syntax work inside the ExpandString method?

后端 未结 3 1230
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 04:05

The following Powershell script demonstrates the issue:

$hash = @{\'a\' = 1; \'b\' = 2}
Write-Host $hash[\'a\']        # => 1
Write-Host $hash.a                   


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 04:29

    I use this method, since this bug exists in v4 (not in v5)

    function render() {
        [CmdletBinding()]
        param ( [parameter(ValueFromPipeline = $true)] [string] $str)
    
        #buggy
        #$ExecutionContext.InvokeCommand.ExpandString($str)
    
        "@`"`n$str`n`"@" | iex
    }
    

    Usage for your example:

      '$($hash.a)' | render
    

提交回复
热议问题