簡體   English   中英

如何將 Azure 函數輸出作為 csv 文件保存到 Blob 存儲

[英]How to save a Azure Function output as csv file to Blob storage

我在 Azure 函數(計時器觸發器)中運行 PowerShell 腳本,該腳本將從 Azure 獲取 PowerBI 工作區數據並將其存儲到 Blob 存儲。

我希望 Azure 函數的輸出數據以 csv 格式存儲在 Blob 中。 當前存儲為 .json

這是我的查詢,

# 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

您可以使用從 JSON 轉換為 CSV來實現此目的。

解決方法如下

# 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

我發布了一個如何使用 Az 模塊命令執行此操作的示例,因為您目前無法使用輸出綁定修改 blob 的屬性,並且默認情況下所有文件都保存為application/octet-stream

將 Azure Function powershell 值輸出到 Azure 存儲帳戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM