繁体   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