我在Azure powershell(发布管道)中运行的这个powershell脚本是否有一些基本上缺少的东西?

我在Azure powershell(发布管道)中运行的这个powershell脚本是否有一些基本上缺少的东西?,powershell,azure-devops,Powershell,Azure Devops,我已经编写了一个PowerShell模块,我们在Azure管道中使用它将SSL证书绑定到自定义主机名。我能够使模块在本地工作,包括在管道中用于初始化模块的内联脚本。在本地运行脚本时,不会出现任何错误。当我在管道中使用相同的脚本时,我收到以下错误: 2019-07-16T23:00:40.3007294Z###[错误]无法使用指定的命名参数解析参数集。 我没有使用参数集,所以我不知道为什么它会抱怨。我能够在本地而不是在发布管道中执行代码的原因是什么?参数设置错误真的是对其他事情的抱怨吗 再一次,我

我已经编写了一个PowerShell模块,我们在Azure管道中使用它将SSL证书绑定到自定义主机名。我能够使模块在本地工作,包括在管道中用于初始化模块的内联脚本。在本地运行脚本时,不会出现任何错误。当我在管道中使用相同的脚本时,我收到以下错误:

2019-07-16T23:00:40.3007294Z###[错误]无法使用指定的命名参数解析参数集。

我没有使用参数集,所以我不知道为什么它会抱怨。我能够在本地而不是在发布管道中执行代码的原因是什么?参数设置错误真的是对其他事情的抱怨吗

再一次,我在本地进行了测试。我的所有参数都是必需的,所以我没有指定参数集,不过,我确实设置了一个参数集以防万一,但我得到了相同的错误

这是我在成功导入模块后使用Azure Powershell任务在发布管道中使用的脚本。我知道我的变量声明是正确的,因为我已经对它们进行了测试,它们输出良好(本地和管道中):

当任务点击脚本中的这一行时会发生错误:
启用SslStateOnWebapp-WebappName$WebappName-customWebappHostname$webAppCustomHostname-ExistingKeyVault名称$ExistingVault名称-ExistingKeyVault名称$certName

$myVars = '$(armDeploymentOutput)' | ConvertFrom-Json
Write-Host "The following variables are available: $($myVars)" -Verbose
$existingVaultName = $myVars.existingKeyVaultName.value
$webappName=$myVars.webappName.value
$webAppCustomHostname=$myVars.webAppCustomHostname.value
$certName=$myVars.existingKeyVaultCertificateName.value

if ($webappName -and $webAppCustomHostname -and $existingVaultName -and $certName) {
    Write-Host "Attempting to enable SSL Binding for $($webappName) on the hostname $($webAppCustomHostname) using cert $($certName) from the existing keyvault of $($existingVaultName)." -Verbose
    Enable-SslStateOnWebapp -WebappName $webappName -customWebappHostname $webAppCustomHostname -existingKeyvaultName $existingVaultName -existingKeyvaultCertName $certName
}
else {
    Write-Host "Insufficient parameters have been provided to complete SSL Binding. To enable SSL binding, please provide a custom hostname, an existing keyvault where your certificate is stored, and the name of your certificate. Please visit the readme for more information." -Verbose
}
这是已初始化的PowerShell模块:

function Enable-SslStateOnWebapp {
    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = 'A webapp name is required.')]
        [ValidateNotNullOrEmpty()]
        [string] $WebappName,

        [PARAMETER(
            Mandatory = $true,            
            HelpMessage = 'The FQDN of the custom hostname you want to bind.')]
        [ValidateNotNullOrEmpty()]
        [string] $customWebappHostname,

        [Parameter(
            Mandatory = $true,
            HelpMessage = 'A name for an existing Keyvault is required.')]
        [ValidateNotNullOrEmpty()]
        [string] $existingKeyvaultName,

        [PARAMETER(
            Mandatory = $true,            
            HelpMessage = 'A name of the pfx certificate stored in the pre-existing keyvault')]
        [ValidateNotNullOrEmpty()]
        [string] $existingKeyVaultCertName
    )
    #getting webapp resources
    $webapp = Get-AzureRmResource -Name $webappName
    #obtaining resource group resources through the use of resource group name tied to webapp
    $rg = Get-AzureRmResource -ResourceGroupName $webapp.ResourceGroupName 

...

}
我的预期结果是,构建管道将无错误地执行脚本。相反,我在本地获得了传递结果(这显然使调试变得困难,然后在管道中获得以下调试日志记录:

2019-07-16T23:00:39.1496157Z ##[debug]Caught exception from task script.
2019-07-16T23:00:39.1536232Z ##[debug]Error record:
2019-07-16T23:00:39.1993172Z ##[debug]Enable-SslStateOnWebapp : Parameter set cannot be resolved using the specified named parameters.
2019-07-16T23:00:40.2927954Z ##[debug]At D:\agent3\_work\_temp\7be280f0-5905-4e05-99e0-972c90739a12.ps1:13 char:5
2019-07-16T23:00:40.2930113Z ##[debug]+     Enable-SslStateOnWebapp -WebappName $webappName -customWebappHost ...
2019-07-16T23:00:40.2940404Z ##[debug]+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2019-07-16T23:00:40.2940886Z ##[debug]    + CategoryInfo          : InvalidArgument: (:) [Enable-SslStateOnWebapp],     ParameterBindingException
2019-07-16T23:00:40.2941287Z ##[debug]    + FullyQualifiedErrorId : AmbiguousParameterSet,Enable-SslStateOnWebapp
2019-07-16T23:00:40.2941647Z ##[debug] 
2019-07-16T23:00:40.2941968Z ##[debug]Script stack trace:
2019-07-16T23:00:40.2942368Z ##[debug]at Enable-SslStateOnWebapp, D:\agent3\_work\r6\a\_infra-grs-referenceapp\drop\pwsh-modules\grs-arm\enable-ssl_state_on_webapp\enable-ssl_state_on_webapp.ps1: line 64
我检查了是否有隐藏的unix字符,因为我在Mac上本地开发,所以没有:


TLDR;此问题是由于本地开发环境中使用的版本与AzureDevops生成代理使用的版本不兼容造成的。请检查生成代理版本,并确保它们与您在中开发的版本相同

这个问题的答案启发了我

从脚本的第64行(脚本的第一个可执行行)抛出错误:

假设它是脚本的第一个可执行行,我假设错误是函数上的参数绑定工作不正确造成的,特别是因为第63行没有写入主机

Write-Host "Attempting to get webapp resource..." -Verbose
我发现事实并非如此。我观察到,如果后续行失败,有时可能无法调用写主机

在本地开发时,我使用的是AzureRM的v6.13.1。如果我查看在第64行调用的函数,该函数是:

$webapp = Get-AzureRmResource -Name $webappName
正在查看此cmdlet的配置文件:

Get-AzureRmResource
   [[-Name] <String>]
   [-ResourceType <String>]
   [-ODataQuery <String>]
   [-ResourceGroupName <String>]
   [-TagName <String>]
   [-TagValue <String>]
   [-ExpandProperties]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]
然后使用我的脚本在后续任务中调用
Get Module
,并在任务的preferredAzurePowerShellVersion部分中指定版本
6.13.1
(如果您想要的版本在代理上不可用,这将导致
version not available
错误,因此您运行上一个PowerShell任务的原因如下:

- task: AzurePowerShell@3
 inputs:
 azureSubscription: '<subscription>'
 ScriptType: 'InlineScript'
 Inline: 'Get-Module AzureRm;
 Import-Module $(System.DefaultWorkingDirectory)\drop\pwsh-modules\grs-arm;
 $myVars = '$(armDeploymentOutput)' | ConvertFrom-Json
 Write-Host "The following variables are available: $($myVars)" -Verbose

 $existingVaultName = $myVars.existingKeyVaultName.value
 $webappName=$myVars.webappName.value
 $webAppCustomHostname=$myVars.webAppCustomHostname.value
 $certName=$myVars.existingKeyVaultCertificateName.value

 Write-Host "Setting an access policy for the SPN to $($existingVaultName)" -Verbose
 Set-AzureRmKeyVaultAccessPolicy -VaultName $existingVaultName -ServicePrincipalName <spnClientId> -PermissionsToCertificates get -PermissionsToSecrets get

 if ($webappName -and $webAppCustomHostname -and $existingVaultName -and $certName) 
 {Write-Host "Attempting to enable SSL Binding for $($webappName) on the hostname 
 $($webAppCustomHostname) using cert $($certName) from the existing keyvault of 
 $($existingVaultName)." -Verbose
 Enable-SslStateOnWebapp -WebappName $webappName -customWebappHostname 
 $webAppCustomHostname -existingKeyvaultName $existingVaultName - 
 existingKeyvaultCertName $certName -Verbose}
 else {Write-Host "Insufficient parameters have been provided to complete SSL 
 Binding. To enable SSL binding, please provide a custom hostname, an existing 
 keyvault where your certificate is stored, and the name of your certificate. 
 Please visit the readme for more information." -Verbose}

 Write-Host "Removing SPN access to $($existingVaultName)" -Verbose
 Remove-AzureRmKeyVaultAccessPolicy -VaultName $existingVaultName - 
 ServicePrincipalName <spnClientId>
 preferredAzurePowerShellVersion: '6.13.1'
-任务:AzurePowerShell@3
投入:
azureSubscription:“”
脚本类型:“InlineScript”
内联:'获取模块AzureRm;
导入模块$(System.DefaultWorkingDirectory)\drop\pwsh modules\grs arm;
$myVars='$(armDeploymentOutput)|从Json转换
Write Host“以下变量可用:$($myVars)”-详细
$existingVaultName=$myVars.existingKeyVaultName.value
$webappName=$myVars.webappName.value
$webAppCustomHostname=$myVars.webAppCustomHostname.value
$certName=$myVars.existingKeyVaultCertificateName.value
写入主机“将SPN的访问策略设置为$($existingVaultName)”-详细
设置AzureRMKeyVault访问策略-Vault名称$ExistingVault名称-ServicePrincipalName-PermissionsToCertificates获取-PermissionsToSecrets获取
if($webappName-和$webAppCustomHostname-和$existingVaultName-和$certName)
{Write Host“正在尝试为主机名上的$($webappName)启用SSL绑定
$($webAppCustomHostname)使用的证书$($certName)来自的现有keyvault
$($existingVaultName)。“-详细
启用SslStateOnWebapp-WebappName$WebappName-customWebappHostname
$webAppCustomHostname-ExistingKeyVault名称$ExistingVault名称-
existingKeyvaultCertName$certName-Verbose}
否则{Write Host“提供的参数不足,无法完成SSL
绑定。要启用SSL绑定,请提供自定义主机名、现有
存储证书的密钥库,以及证书的名称。
有关详细信息,请访问自述文件。“-Verbose}
写入主机“删除对$($existingVaultName)的SPN访问”-详细
删除AzureRmKeyVaultAccessPolicy-VaultName$existingVaultName-
服务主名称
preferredAzurePowerShellVersion:'6.13.1'

这完全解决了我的问题。

TLDR;此问题是由于本地开发环境中使用的版本与AzureDevops生成代理使用的版本不兼容造成的。请检查生成代理版本,确保它们与您在中开发的版本相同

这个问题的答案启发了我

从脚本的第64行(脚本的第一个可执行行)抛出错误:

假设它是脚本的第一个可执行行,我假设错误是函数上的参数绑定工作不正确造成的,特别是因为第63行没有写入主机

Write-Host "Attempting to get webapp resource..." -Verbose
我发现事实并非如此。我观察到,如果后续行失败,有时可能无法调用写主机

在本地开发中,我使用的是AzureRM的v6.13.1
- task: PowerShell@2
 inputs:
 targetType: 'inline'
 script: 'Install-PackageProvider -Name NuGet -Force -Scope CurrentUser;
 Install-Module -Name AzureRM -RequiredVersion 6.13.1 -Force -Scope CurrentUser -AllowClobber;'

- task: AzurePowerShell@3
 inputs:
 azureSubscription: '<subscription>'
 ScriptType: 'InlineScript'
 Inline: 'Get-Module AzureRm;
 Import-Module $(System.DefaultWorkingDirectory)\drop\pwsh-modules\grs-arm;
 $myVars = '$(armDeploymentOutput)' | ConvertFrom-Json
 Write-Host "The following variables are available: $($myVars)" -Verbose

 $existingVaultName = $myVars.existingKeyVaultName.value
 $webappName=$myVars.webappName.value
 $webAppCustomHostname=$myVars.webAppCustomHostname.value
 $certName=$myVars.existingKeyVaultCertificateName.value

 Write-Host "Setting an access policy for the SPN to $($existingVaultName)" -Verbose
 Set-AzureRmKeyVaultAccessPolicy -VaultName $existingVaultName -ServicePrincipalName <spnClientId> -PermissionsToCertificates get -PermissionsToSecrets get

 if ($webappName -and $webAppCustomHostname -and $existingVaultName -and $certName) 
 {Write-Host "Attempting to enable SSL Binding for $($webappName) on the hostname 
 $($webAppCustomHostname) using cert $($certName) from the existing keyvault of 
 $($existingVaultName)." -Verbose
 Enable-SslStateOnWebapp -WebappName $webappName -customWebappHostname 
 $webAppCustomHostname -existingKeyvaultName $existingVaultName - 
 existingKeyvaultCertName $certName -Verbose}
 else {Write-Host "Insufficient parameters have been provided to complete SSL 
 Binding. To enable SSL binding, please provide a custom hostname, an existing 
 keyvault where your certificate is stored, and the name of your certificate. 
 Please visit the readme for more information." -Verbose}

 Write-Host "Removing SPN access to $($existingVaultName)" -Verbose
 Remove-AzureRmKeyVaultAccessPolicy -VaultName $existingVaultName - 
 ServicePrincipalName <spnClientId>
 preferredAzurePowerShellVersion: '6.13.1'