Supplying an input file via '@' gives an error: The splatting operator '@' cannot be used to reference variables in an expression

放肆的年华 提交于 2021-02-08 08:33:26

问题


Following this example here https://docs.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest

I'm getting an error when running my command

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

Error:

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.


回答1:


Note:

  • This answer shows how to escape / quote the @ char. properly in the context of PowerShell's usual parsing rules.

  • 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.


@ 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:

With a literal script filename:

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

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

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"

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] @ 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.




回答2:


You can use this:

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

In PowerShell the special Stop Parsing symbol --% is a signal to PowerShell to stop interpreting any remaining characters on the line. This can be used to call a non-PowerShell utility and pass along some quoted parameters exactly as is.



来源:https://stackoverflow.com/questions/59805670/supplying-an-input-file-via-gives-an-error-the-splatting-operator-canno

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!