Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将变量值复制到脚本块中_Powershell_Powershell 5.0 - Fatal编程技术网

Powershell 将变量值复制到脚本块中

Powershell 将变量值复制到脚本块中,powershell,powershell-5.0,Powershell,Powershell 5.0,考虑以下几点 $y = 'foo' $x = { write-host $y } powershell -NoProfile -ExecutionPolicy Bypass -Command "$x" 这不会打印foo,但我想打印。有没有办法强制powershell内联这些变量?一个选项: $y = 'foo' $x = [scriptblock]::Create("Write-Host $y") powershell -NoProfile -ExecutionPolicy Byp

考虑以下几点

$y = 'foo'
$x = {
    write-host $y
}

powershell -NoProfile -ExecutionPolicy Bypass -Command "$x"
这不会打印foo,但我想打印。有没有办法强制powershell内联这些变量?

一个选项:

$y = 'foo'
$x = [scriptblock]::Create("Write-Host $y")

powershell -NoProfile -ExecutionPolicy Bypass -Command "$x"
一种选择:

$y = 'foo'
$x = [scriptblock]::Create("Write-Host $y")

powershell -NoProfile -ExecutionPolicy Bypass -Command "$x"

在我看来这是个X-Y问题。你想用这个达到什么目的?是否需要将scriptblock作为单独的进程运行?为什么?因为DSC脚本资源被破坏得很厉害,除非在单独的进程中运行,否则无法正常运行。在我看来,这是一个X-Y问题。你想用这个达到什么目的?是否需要将scriptblock作为单独的进程运行?为什么?因为DSC脚本资源被破坏得很厉害,除非您在单独的进程中运行它们,否则无法正常运行。对于该解决方案,您可以这样做。通过使用可扩展字符串,您可以创建包含已扩展的所有局部变量的脚本块。IMHO,与其将其视为字符串,还不如将其视为一个字符串,因为它是一个cmd可执行参数,所以它是一个字符串。对于该解决方案,您可以这样做。通过使用可扩展字符串,您可以创建包含已扩展的所有局部变量的脚本块。IMHO,与其将其视为字符串,还不如将其视为一个字符串,因为它是一个cmd可执行参数,所以它是一个字符串。