在命令内使用变量 (PowerShell)
$computer = gc env:computername
# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password
基本上我需要在计划任务中提供计算机名称...
提前谢谢您!
$computer = gc env:computername
# Argument /RU '$computer'\admin isn't working.
SchTasks /create /SC Daily /tn "Image Verification" /ST 18:00:00 /TR C:\bdr\ImageVerification\ImageVerification.exe /RU '$computer'\admin /RP password
Basically I need to provide the computer name in the scheduled task...
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单引号字符串不会在 PowerShell 中扩展变量。尝试使用双引号字符串,例如:
Single quoted strings will not expand variables in PowerShell. Try a double quoted string e.g.:
要补充 Keith Hill 的有用答案,请提供其他信息:
两者
"$computer\admin"
和不带引号的形式,$computer\admin
,可以工作,因为不带引号的字符串参数隐式被视为就像它们是"..."
-封闭(双-引号),即可扩展字符串执行字符串插值(用它们的值替换嵌入的变量引用和表达式),而不是逐字字符串 ('...'
< /强>, 单引号),不解释其内容。如有疑问,请显式使用
"..."
,特别是当字符串包含|
和<
等元字符时code>有关如何将不带引号的标记解析为命令参数的完整规则,请参阅此答案 。
陷阱:
您尝试的部分引用:
即使更正为
"$computer"\admin
以使插值工作,也不会 工作,因为 PowerShell - 也许令人惊讶 - 然后将$computer
的值和逐字字符串\admin
作为两个传递 论点。 只有当部分引用的复合字符串以不带引号的字符串开头时,它才会被识别为单个参数(例如$computer"\ admin”
可以) - 请参阅此答案了解更多信息。另一个值得注意的陷阱是只有独立变量引用,例如
$computer
和$env:COMPUTERNAME
可以按原样嵌入在“...”
中;要嵌入表达式(包括属性访问和索引访问)或命令,您需要 >将它们括在$(...)
中,即 子表达式运算符。例如,要将表达式$someArray[0]
或$someObj.someProp
的值嵌入可扩展字符串中,必须使用"$($someArray[0 ])"
或"$($someObj.someProp)"
- 参见 此答案了解完整规则。To complement Keith Hill's helpful answer with additional information:
Both
"$computer\admin"
and the unquoted form,$computer\admin
, would work, because unquoted string arguments are implicitly treated as if they were"..."
-enclosed (double-quoted), i.e. as expandable strings that perform string interpolation (replace embedded variable references and expressions with their values), as opposed to verbatim strings ('...'
, single-quoted) that do not interpret their content.When in doubt, use
"..."
explicitly, notably when the string contains metacharacters such as|
and<
For the complete rules on how unquoted tokens are parsed as command arguments, see this answer.
Pitfalls:
The partial quoting you attempted:
even if corrected to
"$computer"\admin
to make interpolation work, would not work, because PowerShell - perhaps surprisingly - then passes the value of$computer
and verbatim string\admin
as two arguments. Only if a compound string with partial quoting starts with an unquoted string is it recognized as a single argument (e.g.$computer"\admin"
would work) - see this answer for more information.Another notable pitfall is that only stand-alone variable references such as
$computer
and$env:COMPUTERNAME
can be embedded as-is in"..."
; to embed an expression - which includes property access and indexed access - or a command, you need to enclose them in$(...)
, the subexpression operator. E.g., to embed the value of expression$someArray[0]
or$someObj.someProp
in an expandable string, you must use"$($someArray[0])"
or"$($someObj.someProp)"
- see this answer for the complete rules.