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

在Windows 10 PowerShell中执行Azure CLI命令时遭遇OS类型冲突问题

解决Azure CLI Run Command OS类型不匹配及PowerShell续行引号问题

核心错误原因

你遇到的(Conflict) Run command OS type 'Windows' does not match the target OS Linux错误,根源非常直接:

  • 目标VM是Ubuntu(Linux系统),但你指定的命令ID是RunPowerShellScript——这个ID是专门给Windows虚拟机执行PowerShell脚本用的;
  • 对应Linux虚拟机的正确命令ID是RunShellScript,这也是你不带续行的命令能正常运行的原因。

PowerShell脚本的修正方案

针对你的脚本,需要做两处调整:

  1. 把命令ID从RunPowerShellScript改为RunShellScript;
  2. 修复PowerShell续行时引号丢失的问题——在PowerShell中,用反引号续行时,如果引号内包含空格,需要确保引号被正确保留,或者用单引号包裹脚本内容,或者将脚本内容定义为变量后传入。

修正后的脚本示例

$ResourceGroup= "Development"
$VmName = "ubuntu-test"
# 方案1:用单引号包裹脚本内容,避免续行时引号解析问题
az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunShellScript `
--scripts 'ufw disable'

# 方案2:将脚本内容存入变量,再传入命令,可读性更强
$scriptContent = "ufw disable"
az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunShellScript `
--scripts $scriptContent

关于引号丢失的额外说明

你提到用Write-Host输出命令时,--scripts "ufw disable"变成了--scripts ufw disable,这是PowerShell的解析特性:当你在命令行参数中使用双引号包裹带空格的内容时,PowerShell会将引号解析为参数边界,实际传递给Azure CLI的是去掉引号的内容,但如果内容包含空格,这会导致Azure CLI将ufw和disable当成两个独立参数,引发错误。

使用单引号包裹脚本内容,或者将脚本存入变量,能避免这个问题,确保Azure CLI收到完整的单条脚本命令。

补充:Azure CLI Run Command的命令ID对应关系

为了避免再犯类似错误,记住这两个核心命令ID的适用场景:

  • RunPowerShellScript:仅适用于Windows虚拟机,执行PowerShell脚本;
  • RunShellScript:仅适用于Linux虚拟机,执行Bash等shell脚本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 13:37:33