使用函数包装器Invoke-AzResourceAction cmdlet的问题。

问题描述 投票:0回答:1

我想为Azure HDInsight设置证书,用于Azure DataLake访问。我的做法如下 文章 到并使用Invoke-AzResourceAction cmdlet。

我想知道,为什么代码在没有函数包装器的情况下可以工作,而在有函数包装器的情况下会失败。我没有更改 azure 订阅。完全没有改变,代码只是失败了。

下面的代码可以正常工作。

### Example Preparation​
# see $HDInsightDefinition.ResourceGroupName​
$resourceGroupName = "PL-Pilot"
​
# see $HDInsightDefinition.ClusterName​
$clusterName = "plhd01-hdi"
​
# see $HDInsightDefinition.ServicePrincipalObject.ApplicationId.Guid​
$appId = "280f3a19-ed77-45c"
​
### get cert details from common keyvault​
# common key vault is available using $HDInsightDefinition.CommonKeyVaultId​
$certStringSecret = Get-AzKeyVaultSecret -Name ("spuucert-{0}" -f $appId) -ResourceId /subscriptions/49e1-a2a6-58ee4a3dd124/resourceGroups/coe-eu-commons/providers/Microsoft.KeyVault/vaults/eu-commons
$certPasswordSecret = Get-AzKeyVaultSecret -Name ("sppwd-{0}" -f $appId) -ResourceId /subscriptions/49e1-a2a6-58ee4a3dd124/resourceGroups/coe-eu-commons/providers/Microsoft.KeyVault/vaults/eu-commons
​
​
### update hdi cluster, if cluster already exists and wasn't created immediately before (as in this case, it has the correct identity certificate)​
# => $HDInsightDefinition.ClusterAlreadyExists​
# see https://docs.microsoft.com/en-us/azure/hdinsight/hdinsight-hadoop-use-data-lake-store#refresh-the-hdinsight-certificate-for-data-lake-storage-gen1-access​
Invoke-AzResourceAction `
    -ResourceGroupName $resourceGroupName `
    -ResourceType 'Microsoft.HDInsight/clusters' `
    -ResourceName $clusterName `
    -ApiVersion '2015-03-01-preview' `
    -Action 'updateclusteridentitycertificate' `
    -Parameters @{ ApplicationId = $appId; Certificate = $certStringSecret.SecretValueText; CertificatePassword = $certPasswordSecret.secretvaluetext } `
    -Force

但是,当我在我们的框架中把上述代码包装在一个函数中时,代码就会失败。我可以确认参数没有问题。'$HDInsightDefinition'对象将包含传递给上面代码的相同的参数值(没有函数包装)。

function Update-XtoHDInsightCertificate {
    param 
    (
        [Parameter (ValueFromPipeline = $true, Mandatory = $true)]
        $HDInsightDefinition
    )

    $appid = $HDInsightDefinition.ServicePrincipalObject.ApplicationId.Guid
    $certStringSecret = Get-AzKeyVaultSecret -Name ("spuucert-{0}" -f $appid) -ResourceID $HDInsightDefinition.CommonKeyVaultId
    $certPasswordSecret = Get-AzKeyVaultSecret -Name ("sppwd-{0}" -f $appid) -ResourceID $HDInsightDefinition.CommonKeyVaultId
    $resourceGroupName = $HDInsightDefinition.ResourceGroupName.tostring()
    $ClusterName = $HDInsightDefinition.ClusterName.tostring()

    # Update the certificate identity of HDInsight Cluster for DataLake Access
    Invoke-AzResourceAction `
    -ResourceGroupName $resourceGroupName `
    -ResourceType 'Microsoft.HDInsight/clusters' `
    -ResourceName $ClusterName `
    -ApiVersion '2015-03-01-preview' `
    -Action 'updateclusteridentitycertificate' `
    -Parameters @{ ApplicationId = $appid; Certificate = $certStringSecret.SecretValueText; CertificatePassword = $certPasswordSecret.SecretValueText } `
    -Force

    write-CoeLog ("Successfully updated certificate for the HDINsight Cluster: {0}" -f $ClusterName)
}

我得到了下面的错误。我使用'Resolve-AZError'cmdlet得到了详细信息。

 Resolve-AzError
Breaking changes in the cmdlet 'Resolve-AzError' :
 - The `Resolve-Error` alias will be removed in a future release.  Please change any scripts that use this alias to use `Resolve-AzError` instead.


NOTE : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other information on breaking changes in Azure PowerShell.


   HistoryId: 4


Message        : {"Message":"An error has occurred."}
StackTrace     :    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.HandleException(ExceptionDispatchInfo capturedException)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.ExecuteCmdlet()
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception      : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses.ErrorResponseMessageException
InvocationInfo : {Invoke-AzResourceAction}
Line           :     Invoke-AzResourceAction `

Position       : At C:\Users\manjug\source\repos\eXtollo_April_23_2\coe-df\Continuous Deployment\Deployment\eXtolloInstance\HDInsight_1\HDInsightTools.psm1:17 char:5
                 +     Invoke-AzResourceAction `
                 +     ~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 4



   HistoryId: 1


RequestId      : d6579b01-bb55-4249-b555-1e0d462a7c6e
Message        : Resource group 'non-existent-rg-12345' could not be found.
ServerMessage  : ResourceGroupNotFound: Resource group 'non-existent-rg-12345' could not be found. (System.Collections.Generic.List`1[Microsoft.Rest.Azure.CloudError])
ServerResponse : {NotFound}
RequestMessage : {GET https://management.azure.com/subscriptions/3e959272-bef3-49e1-a2a6-58ee4a3dd124/resourceGroups/non-existent-rg-12345/resources?api-version=2019-07-01}
InvocationInfo : {Get-AzResource}
Line           :             $null = Get-AzResource -ResourceGroupName non-existent-rg-12345 -Name non-existent-resource-32541 -ErrorAction:SilentlyContinue

Position       : At C:\Users\manjug\source\repos\eXtollo_April_23_2\coe-df\Continuous Deployment\Framework\CoreDeployment.psm1:6957 char:21
                 + ...     $null = Get-AzResource -ResourceGroupName non-existent-rg-12345 - ...
                 +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StackTrace     :    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.HandleException(ExceptionDispatchInfo capturedException)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManagerCmdletBase.ExecuteCmdlet()
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
HistoryId      : 1


The Azure PowerShell team is listening, please let us know how we are doing: https://aka.ms/azpssurvey?Q_CHL=ERROR.
azure certificate azure-resource-manager azure-powershell hdinsight
1个回答
1
投票

我能够解决它。我为变量做了一个类型转换。[String]$appid = $HDInsightDefinition.ServicePrincipalObject.ApplicationId.Guid [String]$resourceGroupName = $HDInsightDefinition.ResourceGroupName [String]$ClusterName = $HDInsightDefinition.ClusterName

© www.soinside.com 2019 - 2024. All rights reserved.