You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Azure CLI虚拟机运行命令时@传入脚本文件报错求助

解决Azure CLI在PowerShell中使用@script.ps1的报错问题

这个问题我之前也碰到过,根源是PowerShell把@符号识别为splatting运算符,而不是Azure CLI期望的"读取本地脚本文件内容"的语法,所以才会抛出那个错误。下面给你几个可行的解决办法:

方法1:用PowerShell读取脚本内容后传递

直接用Get-Content命令读取脚本的完整内容,再嵌入到Azure CLI的参数中:

az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group --scripts "$(Get-Content -Raw script.ps1)"
  • Get-Content -Raw会一次性读取整个脚本文件的内容为一个字符串
  • $(...)是PowerShell的子表达式语法,把读取到的内容作为参数值传给--scripts,这样Azure CLI就能拿到脚本的实际代码了

方法2:使用PowerShell的停止解析运算符

用--%让PowerShell停止解析后续的参数,直接原样传递给Azure CLI:

az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group --% --scripts @script.ps1
  • --%是PowerShell的特殊运算符,后面的所有参数都会直接传递给目标程序(这里就是Azure CLI),不会被PowerShell解析,这样Azure CLI就能正确识别@script.ps1是读取本地文件的指令

方法3:切换到CMD环境执行

如果你的环境允许,直接打开CMD命令提示符,执行你原来的命令即可:

az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group --scripts @script.ps1
  • CMD不会把@当成特殊运算符,所以Azure CLI可以正常解析这个语法

为什么加引号不行?

当你给@script.ps1加上引号后,PowerShell会把"@script.ps1"当成一个普通字符串参数传给Azure CLI,而不是让Azure CLI去读取文件内容。这时候Azure CLI会把这个字符串本身作为要执行的PowerShell脚本,自然达不到你想要的效果。

内容的提问来源于stack exchange,提问作者Ryvik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:34:10