使用Set-AzVMRunCommand/AZ Cli执行Azure脚本遇参数及SAS令牌问题
解决Azure Run Command自动化脚本的两个问题
PowerShell AZ模块:参数传递失败
你当前命令里的-Parameter是错误的,正确参数名是**-Parameters**(复数形式),且参数格式需要是哈希表数组。修正后的命令和参数定义如下:
# 正确定义参数数组 $Parameters = @( @{Name="URL"; Value=$ZipURI} ) # 修正后的Set-AzVMRunCommand命令 Set-AzVMRunCommand -ResourceGroupName $ResourceGroupName ` -VMName $VirtualMachineName ` -RunCommandName "Deploy" ` -SourceScriptUri $SourceScriptURI ` -Location $Location ` -Parameters $Parameters
同时,确保存储在Blob中的脚本通过环境变量读取参数,示例脚本片段:
# 从环境变量获取传递的Zip文件URI $zipDownloadUrl = $env:URL # 后续下载、解压、安装逻辑 Invoke-WebRequest -Uri $zipDownloadUrl -OutFile "install.zip" Expand-Archive -Path "install.zip" -DestinationPath "./install" & "./install/setup.exe"
AZ Cli:SAS令牌触发命令解析错误
报错原因是SAS令牌包含&、=等Shell特殊字符,未被正确转义导致被拆分解析。解决方法是用单引号包裹--script-uri和--protected-parameters的参数值,避免Shell解析特殊字符。
PowerShell环境下的修正命令
az vm run-command create --run-command-name "TaniumInstall" ` --vm-name $VirtualMachineName ` --resource-group $ResourceGroupName ` --script-uri "'$SourceScriptURI'" ` --protected-parameters "'URL=$ZipURI'"
Bash环境下的修正命令
az vm run-command create --run-command-name "TaniumInstall" \ --vm-name $VirtualMachineName \ --resource-group $ResourceGroupName \ --script-uri '$SourceScriptURI' \ --protected-parameters 'URL=$ZipURI'
Blob中的脚本同样需要通过环境变量读取参数,逻辑和PowerShell脚本一致。
内容的提问来源于stack exchange,提问作者Rodolphe Beck
相关产品推荐
相关产品推荐

