Search code examples
azureazure-storage-accountazure-runbook

Get-AzStorageFileContent: Can not find your azure storage credential


I am using a automation account runbook to compare files within a storage account fileshare and have been trying to use Get-AzStorageFileContent to download them so I can then compare.

However, I get the error: "Get-AzStorageFileContent : Can not find your azure storage credential. Please set current storage account using "Set-AzSubscription" or set the "AZURE_STORAGE_CONNECTION_STRING" environment variable."

When I google "Set-AzSubscription" it doesn't appear to exist but I am directed to Set-Azcontext which I have tried to use to set the context to the subscription the storage account is in but this produces the either the same error when testing in powershell ISE or the erorr "please provide a valid tenant or a valid subscription" in the runbook (even though I am using the correct IDs for both)

I have noticed that the storage account is in a different subscription to the runbook could this be breaking it? It does allow me to save files to the storage in the same script so I'm not sure why it would break here.

I am authenticating with a managed identity if that's relevant.

My code to get the file looks like this:

try{ 
    write-output "get file"
    Set-Azcontext -Subscription "--storage account subscription--" -Tenant "--Our tenant--"
    Get-AzStorageFileContent -ShareName "--storage account name--" -Path "--path of file--"
}
catch{
    Write-Output "couldn't get file"
    Write-Warning $_.Exception.Message
    break
}

Solution

  • Get-AzStorageFileContent : Can not find your azure storage credential. Please set current storage account using "Set-AzSubscription" or set the "AZURE_STORAGE_CONNECTION_STRING" environment variable:

    I also got the same error when I tried in my environment.

    enter image description here

    This issue usually occurs if you do not create a storage context by specifying a storage account name and storage account key, which is required for storage account authentication.

    I tried below script in Azure runbook, and it worked successfully as detailed below:

    Connect-AzAccount
    Set-AzContext -Subscription "<SubscriptionID>"
    $context = New-AzStorageContext -StorageAccountName "<StorageAccountName>" -StorageAccountKey "<StorageAccountKey>"
    Get-AzStorageFile -ShareName <FileshareName> -context $context
    

    Output:

    enter image description here