如何让Azure自定义脚本扩展同步下载MSI与PS脚本至同一目录?
解决Custom Script Extension未同步下载MSI文件的问题
要让MSI文件和PowerShell脚本下载到同一目录,核心是在Bicep的Custom Script Extension(CSE)配置中明确指定所有需要下载的文件,具体操作如下:
在CSE的
fileUris属性中,同时添加PS脚本和MSI文件的存储账户URL(若容器为私有,需包含有效的SAS令牌)。示例Bicep代码片段:resource vmExtension 'Microsoft.Compute/virtualMachines/extensions@2023-07-01' = { name: 'CustomScriptExtension' parent: vm properties: { publisher: 'Microsoft.Compute' type: 'CustomScriptExtension' typeHandlerVersion: '1.10' autoUpgradeMinorVersion: true settings: { fileUris: [ 'https://<存储账户名>.blob.core.windows.net/<容器名>/install.ps1' 'https://<存储账户名>.blob.core.windows.net/<容器名>/你的安装包.msi' ] } protectedSettings: { commandToExecute: 'powershell -ExecutionPolicy Unrestricted -File install.ps1' // 私有容器需添加SAS令牌 // sasToken: '<你的SAS令牌>' } } }配置后,CSE会自动将
fileUris中的所有文件下载到同一默认目录(即你提到的C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\...\Downloads\0\)。优化PowerShell脚本路径引用:建议用
$PSScriptRoot变量替代.\,该变量会自动指向当前脚本所在目录,避免执行上下文变化导致的路径错误。修改后的脚本片段:$msiPath = Join-Path -Path $PSScriptRoot -ChildPath '你的安装包.msi' Start-Process msiexec.exe -ArgumentList "/i `"$msiPath`" /qn" -Wait确认存储账户权限:若容器为私有,需确保SAS令牌具备读取权限且未过期;生产环境不建议使用公共容器。
内容的提问来源于stack exchange,提问作者mij2222
相关产品推荐
相关产品推荐

