通过BICEP执行带外部参数的AzurePowerShell脚本遇param错误求助
我需要通过Bicep为自定义域名启用CDN托管HTTPS,只能在Bicep代码中嵌入Azure PowerShell命令实现。但编写传递Bicep参数的PowerShell命令时出现错误,执行脚本返回:
The provided script failed with the following error:
System.Management.Automation.CommandNotFoundException: The term 'param' is not recognized as a name of a cmdlet, function, script file, or executable program...
我参考微软官方示例使用param语法,但问题依然存在,我的代码如下:
var ResourceGroupName = 'xx-Bicep-xxx-Build-Work' var ProfileName = profile.name var EndpointName = endpoint.name var CustomDomainName = dnsCnameRecordCnd var ScriptContent = ''' param([string] $ResourceGroupName) param([string] $ProfileName) param([string] $EndpointName) param([string] $CustomDomainName) $customDomainHttpsParameter = New-AzCdnManagedHttpsParametersObject -CertificateSourceParameterCertificateType Dedicated -CertificateSource Cdn -ProtocolType ServerNameIndication Enable-AzCdnCustomDomainCustomHttps -ResourceGroupName $ResourceGroupName -ProfileName $ProfileName -EndpointName $EndpointName -CustomDomainName $CustomDomainName -CustomDomainHttpsParameter $customDomainHttpsParameter -SubscriptionId xx-xx-xx-xx-xxx ''' resource SetServicesCertificates 'Microsoft.Resources/deploymentScripts@2020-10-01' = { name: 'SetServicesCertificates' location: location kind: 'AzurePowerShell' properties: { arguments: '-ProfileName ${ProfileName} -EndpointName ${EndpointName} -ResourceGroupName ${ResourceGroupName} -CustomDomainName ${CustomDomainName}' azPowerShellVersion: '8.3' scriptContent: ScriptContent cleanupPreference: 'OnSuccess' retentionInterval: 'P1D' timeout: 'PT3M' } dependsOn:[ UpdateExistingDnsZoneCdn endPointCustomDomain ] }
问题原因及解决方法
错误根源:PowerShell不允许使用多个独立的
param()语句,所有参数必须定义在同一个param()代码块内。你当前的脚本把每个参数单独放在一个param()里,PowerShell会将这些独立的param识别为未定义的命令,从而触发报错。修复方案:
将所有参数合并到一个param()块中,修改后的ScriptContent如下:param( [string] $ResourceGroupName, [string] $ProfileName, [string] $EndpointName, [string] $CustomDomainName ) $customDomainHttpsParameter = New-AzCdnManagedHttpsParametersObject -CertificateSourceParameterCertificateType Dedicated -CertificateSource Cdn -ProtocolType ServerNameIndication Enable-AzCdnCustomDomainCustomHttps -ResourceGroupName $ResourceGroupName -ProfileName $ProfileName -EndpointName $EndpointName -CustomDomainName $CustomDomainName -CustomDomainHttpsParameter $customDomainHttpsParameter -SubscriptionId xx-xx-xx-xx-xxx额外验证项:
确认deploymentScripts资源指定的azPowerShellVersion: '8.3'支持你使用的New-AzCdnManagedHttpsParametersObject和Enable-AzCdnCustomDomainCustomHttpscmdlet,避免因PowerShell模块版本过低导致的cmdlet不存在问题。
内容的提问来源于stack exchange,提问作者Damo

