gpt4 book ai didi

powershell - 在单引号内扩展变量

转载 作者:行者123 更新时间:2023-12-04 01:27:45 40 4
gpt4 key购买 nike

如何在单引号内扩展$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"

或执行相同操作,但使用反引号(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"

关于powershell - 在单引号内扩展变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127583/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com