在命令内使用变量 (PowerShell)

发布于 2025-01-13 18:14:41 字数 302 浏览 2 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-01-20 18:14:41

单引号字符串不会在 PowerShell 中扩展变量。尝试使用双引号字符串,例如:

"$computer\admin"

Single quoted strings will not expand variables in PowerShell. Try a double quoted string e.g.:

"$computer\admin"
挽袖吟 2025-01-20 18:14:41

要补充 Keith Hill 的有用答案,请提供其他信息:

两者 "$computer\admin"不带引号的形式,$computer\admin,可以工作,因为不带引号的字符串参数隐式被视为就像它们是"..."-封闭-引号),即可扩展字符串执行字符串插值(用它们的值替换嵌入的变量引用和表达式),而不是逐字字符串 ('...'< /强>, 引号),解释其内容。

如有疑问,请显式使用 "...",特别是当字符串包含 |< 等元字符时code>

有关如何将不带引号的标记解析为命令参数的完整规则,请参阅此答案

陷阱

您尝试的部分引用

'$computer'\admin

即使更正为 "$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:

'$computer'\admin

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文