Search code examples
azurepowershellazure-active-directoryazure-automation

Azure Automation unable to transfer files to storage account


I am attempting to have an Azure Automation account export a .CSV to a Storage Account however I am running into a significant amount of trouble doing so. As most documentation says to use AzureRM and Azure RM is being depreciated.

Here is how I'm attempting to transfer the .CSV

$SecureAppSecret = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential($AppId, $SecureAppSecret)
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
[String]$StorageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupID -Name $StorageAccountName

$azure_StorageContextSplat = @{
StorageAccountName = $StorageAccountName
StorageAccountKey = $StorageAccountKey
}
$storageContext = New-AzStorageContext @azure_StorageContextSplat

$Report | Export-CSV -Path "~\File.csv"

$azure_FileToUploadSplat = @{
Context = $storageContext
File = "~\File.csv"
Container = $StorageContainer
Force = $true
}

Set-AzStorageBlobContent @azure_FileToUploadSplat

$AppSecret, $AppId, $TenantId, $ResourceGroupID, $StorageAccountName, and $storageContext are all provided earlier in the script as plaintext (I know it's not secure but I want it to work then I can make it secure)

After running this in the Azure Automation test pane I get the following 2 errors:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. 


Could not get the storage context.  Please pass in a storage context or set the current storage context.

I believe the first one refers to StorageAccountKey but at this point I don't know. Any help at all would be appreciated. Thank you.


Solution

  • Simple way to transfer file is the below process and take it as an alternative as it is little less secure:

    I have reproduced in my environment and the below script worked for me and I followed Microsoft-Document:

    Import-Module -Name Azure.Storage
    
    $storageAccName = "rithwikstorage"
    $storageAccKey = "ohNJHBheHNb/gSSBWXUxt3dxrw+AStXNnJyQ=="
    
    $context = New-AzStorageContext -StorageAccountName $storageAccName -StorageAccountKey $storageAccKey
    
    $conName = "name of the conatiner"
    $Report =Get-AzResource | Export-CSV -Path "~\File.csv"
    
    Set-AzStorageBlobContent -Context $context  -Container $conName -File "~\File.csv"
    

    enter image description here

    Output: enter image description here

    Then in Storage Account:

    enter image description here

    Here I have given storage account key , name and container name directly and I got desired output, try to follow the same to get desired output as I have got.