is it possible to save a PowerShell script output in azure storage blob?

RADical 6 Reputation points
2022-11-06T23:59:56.62+00:00

Is it possible to run a cmdlet like Get-AzureADUser in PowerShell and then store the returned output in Azure Storage Blob?

I can use the cmdlets below to upload files from my local machine to my storage account

$localDirectory = "C:\Users\User\Documents\Custom"  
Get-ChildItem -File $localDirectory | Set-AzStorageBlobContent -Container $containerName -Blob $name -Context $storageContext  

The idea however is to have a runbook in Azure Automate that will save the output of Get-AzureADUser to my Storage Account.

Any help would be greatly appreciated! Did some research on this, but unable to find anything that helps.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,353 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Maxim Sergeev 6,586 Reputation points Microsoft Employee
    2022-11-07T01:33:16.193+00:00

    Hi there,

    Yes, you can do it by many ways. I recommend a most modern and most secured option which is using Managed Identities.

    Enable Managed Identity in your Azure Automation Account or Azure Functions, grant required permissions to the MI and do whatever you want.

    https://learn.microsoft.com/en-us/azure/automation/automation-security-overview
    https://learn.microsoft.com/en-us/samples/azure-samples/functions-storage-managed-identity/using-managed-identity-between-azure-functions-and-azure-storage/

    This is an example of a script that uploads data to a storage account using a certificate, actually the logic is exactly the same, you just need to switch an authentication from SAS tokens to Managed Identity.

    https://techgenix.com/storage-accounts-automation-accounts/

    1 person found this answer helpful.
    0 comments No comments

  2. Andre++ 21 Reputation points
    2025-04-23T09:24:44.89+00:00

    @Maxim Sergeev How would this answer the OP;s question? By now, the techgenix link is dead as well.

    It should be more like

    # $Result holds the output to be written
    
    $context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""
    
    $container=Get-AzStorageContainer -Name "input" -Context $context
    
    $content = [system.Text.Encoding]::UTF8.GetBytes($Result)
    
    $container.CloudBlobContainer.GetBlockBlobReference("my.json").UploadFromByteArray($content,0,$content.Length)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.