在单引号内扩展变量 [英] Expand variable inside single quotes

查看:82
本文介绍了在单引号内扩展变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

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

You can use formatting and assign it to another variable:

$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

或者您可以使用双引号并将引号内的引号引起来:

Or you can use double quotes and escape the inside quotes with quotes:

$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)使其逃脱:

Or do the same but use backtick (grave) to escape them:

$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"

这篇关于在单引号内扩展变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆