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

后端 未结 3 1231
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  Happy的楠姐
    2021-01-12 04:43

    The ExpandString api is not exactly meant for use from PowerShell scripts, it was added more for C# code. It's still a bug that your example doesn't work (and I think it's been fixed in V4), but it does mean there is a workaround - one that I recommend for general use.

    Double quoted strings effectively (but not literally) call ExpandString. So the following should be equivalent:

    $ExecutionContext.InvokeCommand.ExpandString('$($hash.a)')
    "$($hash.a)"
    

提交回复
热议问题