2
votes

I am trying to run this command within an Azure Run book

(Get-AzureRmStorageAccountKey -Name $defaultStorageAccountName -ResourceGroupName $defaultResourceGroupName).Value[0]

It runs fine on my local machine and I can see the Storage Account Key. However, when I run the same command in Azure Runbook it throws the following error

Cannot index into a null array

If I just run Get-AzureRmStorageAccountKey -Name $defaultStorageAccountName -ResourceGroupName $defaultResourceGroupName in the Azure Runbook, it runs fine and I can see the Keys. It is just when I am selecting an element from an Array that it fails and throws an error.

The PowerShell version is 5 on my local machine and on Azure Runbook.

Update:

My issue was resolved after updating the Modules from the Gallery in my automation account.

1
try doing get-member -inputobject (Get-AzureRmStorageAccountKey -Name $defaultStorageAccountName -ResourceGroupName $defaultResourceGroupName).Value and post the result - 4c74356b41
I have run the command you suggested and it is throwing this error get-member : You must specify an object for the Get-Member cmdlet - jane
well, there's no property named value try doing get-member -inputobject (Get-AzureRmStorageAccountKey -Name $defaultStorageAccountName -ResourceGroupName $defaultResourceGroupName) and see all the properties that exist - 4c74356b41
the output i get is Equals Method bool Equals(System.Object obj) tem.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Key1 Property string Key1 {get;set;} Key2 Property string Key2 {get;set;} - jane

1 Answers

1
votes

According to your description, I test your cmdlets in my runbook, I could get storage key. enter image description here

You could use the following commands to login your subscriptions.

Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
$connectionName = Get-AutomationConnection -Name "AzureRunAsConnection"
$AzureCredentialAssetName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $AzureCredentialAssetName        

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

$key=(Get-AzureRmStorageAccountKey -Name <storage account name> -ResourceGroupName <resource group name>).Value[0]
"The storage account key is $key"

More information please refer to this link. Update

You need update Azure Storage account module version in automation account, then you could solve this issue.