6

We are generating a Powershell object thru Azure Runbook and basically converting it into a CSV string.

We want to store this generated CSV String (generated from Azure Powershell Runbook) into Azure Blob Storage as a CSV File. Can someone help me how and whcih powershell command we can use to save this CSV String as File to Azure Blob Storage? I tried to look around and came across Push-OutputBindings function but not sure how I can use that one in Azure Powershell Runbook which module to import and not sure if it is part of Azure Functions V2 but any little basics on how I can use it will help me.

Thank you

user42012
  • 722
  • 12
  • 33

1 Answers1

7

Try the following code - with some modifications. Essentially idea is to store the CSV file locally and then, upload it to the blob storage(I tested it locally but not from the Runbook, but it should work there as well):

#file name will be a guid string to avoid overlapping     
$guid = New-Guid
$guidString =$guid.ToString()

# store csv string to random file with random name (guid) 
$LogFull = "$guidString.csv" 
$LogItem = New-Item -ItemType File -Name $LogFull

#ignore next two lines as you already have csv string
$Date = Get-Date
$csvstring = ConvertTo-Csv -InputObject $Date -Delimiter ';' -NoTypeInformation

#save csv string locally 
$csvstring | Out-File -FilePath $LogFull -Append

#and then upload it to blob storage

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

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

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

Alternative solution with Azure Functions

Try to have your Runbook calling your Http triggered Azure function, and pass the string as the parameter, or in the body. This is just a simple REST API call.

In Azure function, you can have Python, NodeJS or C# code that would put your string to CSV file in the blob store. There are plenty of tutorials for this topic, but first, you need to get your string to AF :)

Have a look at the example below, and try something similar(I have not tested it). Essentially the idea is to invoke simple REST API call and passing your payload in the request body:

[string]$Endpoint= "https://myfunction.azurewebsites.net/api/HttpTrigger?code=my_code_I_get_from_Azure_portal"
. . .
$payload =@" your payload "@
$Header = @{
   "Content-Type" = "text/plain";
}
$Result = Invoke-RestMethod -Uri $Endpoint -Method Post -body $Payload Headers $Header

Azure function URL+Code you get from Azure portal, if you click on Azure Function, you will see the button 'Get Function Url'.

kgalic
  • 2,441
  • 1
  • 9
  • 21
  • Thank you for you response, but we dont have any Azure function created. The ask is if we can directly do this from powershell (I understand u asking for calling function but which function? we dont have any that creates a file into blob..this is where I am stuck.. so sorry your response is not helping me here. We are stuck with the actual function needed to call and send stuff back to Blob storage as a CSV file from PS object. – user42012 Oct 23 '19 at 15:28
  • 1
    Thanks for the comment, fair enough. My initial answer was more in the direction of how to resolve it quickly, without spending too much time with PS and simply delegate the responsibility to Azure Function to do the dirty work. Anyway, I updated the answer, and new idea is to store the file locally and then upload it to the blob storage. You will have to modify the code maybe a bit, but I think that idea and concept is valid, and resolves your problem only in PS. – kgalic Oct 23 '19 at 19:54
  • 1
    Thank you @kgalic apprecaite it, this helps me. – user42012 Oct 24 '19 at 12:09