Expanding variables in file contents

前端 未结 3 2075
闹比i
闹比i 2020-12-09 15:05

I have a file template.txt which contains the following:

Hello ${something}

I would like to create a PowerShell script that re

3条回答
  •  萌比男神i
    2020-12-09 15:42

    Another option is to use ExpandString() e.g.:

    $expanded = $ExecutionContext.InvokeCommand.ExpandString($template)
    

    Invoke-Expression will also work. However be careful. Both of these options are capable of executing arbitrary code e.g.:

    # Contents of file template.txt
    "EvilString";$(remove-item -whatif c:\ -r -force -confirm:$false -ea 0)
    
    $template = gc template.txt
    iex $template # could result in a bad day
    

    If you want to have a "safe" string eval without the potential to accidentally run code then you can combine PowerShell jobs and restricted runspaces to do just that e.g.:

    PS> $InitSB = {$ExecutionContext.SessionState.Applications.Clear(); $ExecutionContext.SessionState.Scripts.Clear(); Get-Command | %{$_.Visibility = 'Private'}}
    PS> $SafeStringEvalSB = {param($str) $str}
    PS> $job = Start-Job -Init $InitSB -ScriptBlock $SafeStringEvalSB -ArgumentList '$foo (Notepad.exe) bar'
    PS> Wait-Job $job > $null
    PS> Receive-Job $job
    $foo (Notepad.exe) bar
    

    Now if you attempt to use an expression in the string that uses a cmdlet, this will not execute the command:

    PS> $job = Start-Job -Init $InitSB -ScriptBlock $SafeStringEvalSB -ArgumentList '$foo $(Start-Process Notepad.exe) bar'
    PS> Wait-Job $job > $null
    PS> Receive-Job $job
    $foo $(Start-Process Notepad.exe) bar
    

    If you would like to see a failure if a command is attempted, then use $ExecutionContext.InvokeCommand.ExpandString to expand the $str parameter.

提交回复
热议问题