Writing to files with Azure Automation

Azure Automation enables PowerShell (and more) to be executed as runbooks by runbook workers hosted in Azure. Additionally Azure Automation accounts bring capabilities such as credential objects to securely store credentials, variables, scheduling and more. When a runbook executes it runs in a temporary environment that does not have any persistent state and so if you want to work with files you need to save them somewhere, for example to an Azure storage account as a blob, before the runbook completes.

You can actually create and use files as normal using the default path within PowerShell during execution, just remember to save the files externally before the script completes.

For example create a file as usual:

$todaydate = Get-Date -Format MM-dd-yy 
$LogFull = "AzureScan-$todaydate.log" 
$LogItem = New-Item -ItemType File -Name $LogFull

"  Text to write" | Out-File -FilePath $LogFull -Append

Then before ending the PowerShell, copy it to a blob (as an example storage place):

#Get key to storage account
$acctKey = (Get-AzureRmStorageAccountKey -Name onemtceastusfs -ResourceGroupName EastUS-Infra-RG).Value[0]

#Map to the reports BLOB context
$storageContext = New-AzureStorageContext -StorageAccountName "onemtceastusfs" -StorageAccountKey $acctKey

#Copy the file to the storage account
Set-AzureStorageBlobContent -File $LogFull -Container "azurescan" -BlobType "Block" -Context $storageContext -Verbose



3 thoughts on “Writing to files with Azure Automation

  1. This was indeed very useful. Thank you . but how do we append the file instead of overwrite it? Appreciate your help


    1. One option would be to read the file, add things to it then write it back. Another option would be append blobs where you can just add to the end.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: