通过'@'提供输入文件会出现错误:斜线运算符'@'不能用于引用表达式中的变量 [英] Supplying an input file via '@' gives an error: The splatting operator '@' cannot be used to reference variables in an expression

查看:214
本文介绍了通过'@'提供输入文件会出现错误:斜线运算符'@'不能用于引用表达式中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例以下 https://docs.microsoft.com/zh-CN/cli/azure/vm/run-command?view=azure-cli-latest

运行命令时出现错误

az vm run-command invoke  --command-id RunPowerShellScript --name win-vm -g my-resource-group --scripts @script.ps1

错误:

拼写运算符'@'不能用于引用表达式中的变量."@script"只能用作命令的参数.要在表达式中引用变量,请使用"$ script".

The splatting operator '@' cannot be used to reference variables in an expression. '@script' can be used only as an argument to a command. To reference variables in an expression use '$script'.

将其放在引号中只会传递引号中的内容,而不会传递脚本的内容.

Putting it in quotes only passes in the contents in the quotes, not the contents of the script.

推荐答案

注意:

  • 此答案说明如何转义/引用 @ 字符.正确地.

如果您的命令行仅包含 verbatim 自变量-即,仅文字标记,而不是PowerShell 变量引用(例如, $ file )或表达式(例如,($ dir +'\ script.ps1'))-您可以替换地放置-%停止传递符号,位于传递参数之前,如 Wasif Hasan的答案;请注意,仍然会扩展 cmd.exe 样式的变量引用,例如%FOO%.

If your command line only contains verbatim arguments - i.e., only literal tokens, not PowerShell variable references (e.g, $file) or expressions (e.g., ($dir + '\script.ps1')) - you can alternatively place --%, the stop-parsing symbol, before the pass-through arguments, as shown in Wasif Hasan's answer; note that cmd.exe-style variable references such as %FOO% are still expanded, however.

@ 是PowerShell中的元字符 (具有句法含义的字符 [1] ),因此在为了将其 verbatim 传递给 az ,您必须引用整个参数,或者`-转义 @ 单独:

@ is a metacharacter in PowerShell (a character with syntactic meaning[1]), so in order to pass it verbatim through to az you must either quote the whole argument or `-escape the @ individually:

具有 literal 脚本文件名:

# Either: `-escape the @
az ... --scripts `@script.ps1

#`# Or: quote the whole argument
# Use '...' for a literal argument.
az ... --scripts '@script.ps1'

脚本文件名存储在变量中, $ file :

With the script filename stored in a variable, $file:

# Either: `-escape the @
az ... --scripts `@$file

#`# Or: quote the whole argument
# Use "..." for an argument with variable references, i.e. an expandable string
az ... --scripts "@$file"

注意:在可变的情况下,您可以仅仅使用 @ $ file ,但是考虑到这对任何字符都不起作用. other 而不是 @ 之后的 $ ,最好养成始终引用/转义逐字 @ 的习惯

Note: You could get away with just @$file in the variable case, but given that that doesn't work with any char. other than $ following the @, it's better to get into the habit of always quoting / escaping a verbatim @.

[1] @ 有几种语法用法,具体用法取决于接下来要使用的字符.在您的情况下, @ script.ps1 中的 @ 被解释为

[1] @ has several syntactic uses, and the specific use depends on what character comes next. In your case, the @ in @script.ps1 was interpreted as the splatting operator with a variable named script, with the .ps1 part interpreted as an attempt to access a property named ps1 on that variable - hence the error message.

这篇关于通过'@'提供输入文件会出现错误:斜线运算符'@'不能用于引用表达式中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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