Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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_Variable Expansion - Fatal编程技术网

Powershell 展开单引号内的变量

Powershell 展开单引号内的变量,powershell,variable-expansion,Powershell,Variable Expansion,如何在单引号内展开$pw $pw = "$PsHome\powershell.exe" cmd.exe /c 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' 您可以使用格式并将其分配给另一个变量: $pw = "$PsHome\powershell.exe"; $comm

如何在单引号内展开
$pw

$pw = "$PsHome\powershell.exe"
cmd.exe /c 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1'

您可以使用格式并将其分配给另一个变量:

$pw = "$PsHome\powershell.exe";
$command = 'schtasks /create /tn cleanup /tr "{0} -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' -f $pw;
cmd.exe /c $command
或者,您可以使用双引号并用引号转义内部引号:

$pw = "$PsHome\powershell.exe"
cmd.exe /c "schtasks /create /tn cleanup /tr ""$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1"" /sc minute /mo 1"
或者也可以这样做,但使用backtick(grave)来逃避它们:

$pw = "$PsHome\powershell.exe"
cmd.exe /c "schtasks /create /tn cleanup /tr `"$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1`" /sc minute /mo 1"

另一个选项是使用
$ExecutionContext

$pw = "$PsHome\powershell.exe"
$cmd = 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1'
$cmd = $ExecutionContext.InvokeCommand.ExpandString($cmd)
cmd.exe /c $cmd

你真是太棒了,你所有的解决方案都奏效了,tks!