Azure powershell runbooks with Azure storage

Arjun Dn 26 Reputation points
2020-09-09T15:13:58.76+00:00

Hi All,

I have few PowerShell scripts that do output files. The Powershell scripts are scheduled over Task scheduler on-premise server and the Output is being placed on remote shared path.

I am new to Azure runbooks and wanted to know if the same Powershell scripts be run in a automation account and output be placed into Blob storage. If yes, what are the components of the Azure required for accomplishment. Please guide me and thanks in advance.

Regards,
Arjun DN

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,177 questions
0 comments No comments
{count} vote

Accepted answer
  1. olufemia-MSFT 2,861 Reputation points
    2020-09-09T22:57:56.817+00:00

    Hello @Arjun Dn - Welcome to Azure Automation and Runbook execution. :)
    To answer your question: Yes, your existing Powershell scripts can be executed via a Runbook imported to your Automation account.

    I recommend checking out Ivan Yang's detailed answer on using Azure Blob storage sdk in your Automation Runbook - copied steps below for your reference.

    Also sharing the Azure Automation Team's Powershell Gallery of ready-to-deploy Runbook solutions for common Azure Ops scenarios. This is a useful reference to have as as you explore Azure Automation's many capabilities.

    Hope this helps but dont hesitate to ping if you have any followup questions.

    First, you need to import the Microsoft.WindowsAzure.Storage.dll in Azure blob storage sdk as a module in the runbook. Follow the steps below:

    1.Get the Microsoft.WindowsAzure.Storage.dll. If you don't know how to get it, just open the visual studio -> create a .net framework console project -> then right click the console project -> Manage Nuget Packages, then search and download the azure blob storage sdk WindowsAzure.Storage

    2.When the package installed, build the console project, then in the bin -> debug folder, you can see Microsoft.WindowsAzure.Storage.dll.

    3.Put the Microsoft.WindowsAzure.Storage.dll into a zip file named Microsoft.WindowsAzure.Storage.zip

    4.Go to azure portal -> your automation account -> in the left pane, click "Modules" -> Add a module -> select the .zip file in step 2. You need wait for a few minutes for the module completes uploading(when it completes uploading, you can find the status of Microsoft.WindowsAzure.Storage module is Available ), see the screenshot below:

    23591-yyauto1.png

    Secondly, create a powershell runbook, and write the code like below. Here, I just upload a string using UploadText("your_string_text") method. Note that since the sdk provides many upload method, you should check the return value from the api is text / stream / byte format, then select the proper upload method like UploadFromStream(your_stream) / UploadFromByteArray(byte[] buffer, int index, int count):

    Write-Output "start the test"  
    
    Add-Type -Path "C:\Modules\User\Microsoft.WindowsAzure.Storage\Microsoft.WindowsAzure.Storage.dll"  
    
    $access_token ="Access_Token"  
    $URI =  "https://XXXXX"  
    $headers = @{“authorization” = “Bearer $access_token”}   
    [Net.ServicePointManager]::SecurityProtocol =   
    [Net.SecurityProtocolType]::Tls12  
    $result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json  
    $blob_content_to_upload = $Result|ConvertFrom-Json| Select -ExpandProperty Forms  
    
    $account_name = "xxx"  
    $account_key = "xxx"  
    $container_name = "test1"  
    $blob_name = "testfile3.txt"  
    
    Write-Output "start communicate with blob storage"  
    
    $creds = New-Object "Microsoft.WindowsAzure.Storage.Auth.StorageCredentials" -ArgumentList $account_name,$account_key  
    
    $storageAccount = New-Object "Microsoft.WindowsAzure.Storage.CloudStorageAccount" -ArgumentList $creds,$true  
    $cloudBlobClient = $storageAccount.CreateCloudBlobClient()  
    $cloudBlobContainer = $cloudBlobClient.GetContainerReference($container_name)  
    $myblob = $cloudBlobContainer.GetBlockBlobReference($blob_name)  
    
    #note that the sdk also provides other methods like UploadFromStream(your_stream) / UploadFromByteArray(byte[] buffer, int index, int count), you can choose the proper method for your purpose.  
    $myblob.UploadText($blob_content_to_upload)  
    
    Write-Output("***the test is completed***")  
    

    Then you can run the runbook, after it completes, you can see the blob is created on blob storage with proper content. Note that during the running, there maybe a error says the modules loading issue, but it doesn't matter.

    The test result, blob is created on blob storage.

    23601-yyauto2.png

    If you want to specify the content-type, just add this line of code: $myblob.Properties.ContentType = "your_content_type"

    Cheers.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful