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

后端 未结 3 1215
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  抹茶落季
    2021-01-12 04:27

    I was trying to store text that prompts the user in a text file. I wanted to be able to have variables in the text file that are expanded from my script.

    My settings are stored in a PSCustomObject called $profile and so in my text I was trying to do something like:

    Hello $($profile.First) $($profile.Last)!!!
    

    and then from my script I was trying to do:

    $profile=GetProfile #Function returns PSCustomObject 
    $temp=Get-Content -Path "myFile.txt"
    $myText=Join-String $temp
    $myText=$ExecutionContext.InvokeCommand.ExpandString($myText) 
    

    which of course left me with the error

    Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object."

    Finally I figured out I only needed to to store the PSCustomObject values I want in regular old variables, change the text file to use those instead of the object.property version and everything worked nicely:

    $profile=GetProfile #Function returns PSCustomObject 
    $First=$profile.First
    $Last=$profile.Last
    $temp=Get-Content -Path "myFile.txt"
    $myText=Join-String $temp
    $myText=$ExecutionContext.InvokeCommand.ExpandString($myText) 
    

    And in the text I changed to

    Hello $First $Last!!!

提交回复
热议问题