使用Set-AzVMCustomScriptExtension报错:CommandToExecute同时存在于两类配置段
解决Set-AzVMCustomScriptExtension的CommandToExecute重复配置错误
错误原因分析
你遇到的Invalid Configuration - CommandToExecute is present in both protected and public configuration sections错误,本质是因为你的命令同时在**公开配置(public configuration)和受保护配置(protected configuration)**里定义了CommandToExecute字段。
具体来说,你使用了-Run参数指定要执行的脚本,这会自动把基础命令写入公开配置;同时你用-Argument传递了包含密码这类敏感信息的参数,Az模块会尝试把带敏感参数的完整命令也写入受保护配置,导致重复定义触发错误。
解决方案:用ProtectedArgument传递敏感参数
因为你的参数包含密码这类敏感数据,应该使用-ProtectedArgument替代-Argument,它会自动将敏感参数隔离到受保护配置中,避免和公开配置的命令重复。修正后的命令如下:
Set-AzVMCustomScriptExtension -ResourceGroupName XXX -Location XXXX -VMName XXXXX -Name post_config ` -FileName create_databases_mssql2.ps1 ` -ContainerName XXXX ` -StorageAccountName XXXX ` -Run "create_databases_mssql2.ps1" ` -ProtectedArgument "-u $username -p $password -db_user $loginName -dbpwd $password_db"
额外注意:修正你的脚本语法
另外,你的create_databases_mssql2.ps1脚本存在语法问题,两行New-Item命令没有分隔,会导致执行失败,需要改成:
New-Item -Path "F:\Datas2\Prod" -ItemType Directory New-Item -Path "G:\Logs2\Prod" -ItemType Directory
或者用分号分隔在一行:
New-Item -Path "F:\Datas2\Prod" -ItemType Directory; New-Item -Path "G:\Logs2\Prod" -ItemType Directory
内容的提问来源于stack exchange,提问作者Wanexa
相关产品推荐
相关产品推荐

