Home > Mobile >  How to save a Azure Function output as csv file to Blob storage
How to save a Azure Function output as csv file to Blob storage

Time:07-22

I'm running a PowerShell script in Azure function (Timer Trigger) which will fetch PowerBI workspace data from Azure and it will store to Blob storage.

I want the output data of the Azure function to be stored in csv format in Blob. Currently storing as .json

here is my query,

# Input bindings are passed in via param block.
    param($Timer)
    
    # Get the current universal time in the default string format.
    $currentUTCtime = (Get-Date).ToUniversalTime()
    
    # The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
    if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
    }
    
    # Write an information log with the current time.
    Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"
    
    $secret="********"
    $tenantId="********"
    $appId="********"
    
    
    $password= ConvertTo-SecureString $secret -AsPlainText -Force
    $credential= New-Object System.Management.Automation.PSCredential ($appId, $password)
    
    #Connecting to PowerBI
    Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential
    
    #Getting PowerBI Workspace data
    $body = Get-PowerBIWorkspace -Scope Organization
    
    #output to the blob file
    Push-OutputBinding -Name outputBlob -Value $body

CodePudding user response:

You can use convert from JSON to CSV to achieve this.

Workaround follows

# Convert your PowerBI JSON data into CSV 
Get-PowerBIWorkspace -Scope Organization | ConvertTo-Json | ConvertFrom-Json | Export-Csv -Path .\PowerBIData.csv -IncludeTypeInformation

# Fetch the content of the CSV file
$body = Get-Content -Path .\PowerBIData.csv 

#output to the blob file
Push-OutputBinding -Name outputBlob -Value $body

CodePudding user response:

I posted an example of how to do this using the Az module commands as you are not able to modify the properties of the blob using the output binding at present and all files get saved as application/octet-stream by default.

Output Azure Function powershell value to azure storage account

  • Related