如何将Azure Timer Trigger输出的PowerBI数据保存为CSV至Blob存储?
解决方案:将PowerBI工作区数据以CSV格式保存到Azure Blob存储
修改后的PowerShell脚本
# 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 and select required properties $workspaces = Get-PowerBIWorkspace -Scope Organization | Select-Object Id, Name, Type, State, IsReadOnly, IsOrphaned, CreatedBy, LastUpdated # Convert workspace data to CSV format (remove type info, use comma delimiter) $csvContent = $workspaces | ConvertTo-Csv -Delimiter "," -NoTypeInformation # Join CSV lines into a single string (ensure proper line endings) $csvOutput = $csvContent -join "`r`n" # Output to the blob file as CSV Push-OutputBinding -Name outputBlob -Value $csvOutput
关键改动说明
- 筛选核心属性:
Get-PowerBIWorkspace返回的对象包含大量嵌套属性,直接转CSV会导致数据混乱,所以用Select-Object提取需要的字段(可根据需求调整字段列表)。 - 转换为CSV格式:使用
ConvertTo-Csv将PowerShell对象转换为CSV字符串,-NoTypeInformation移除开头的类型注释行,-Delimiter ","指定逗号作为分隔符。 - 格式化CSV字符串:将CSV数组用
-join "rn"合并为单字符串,保证Blob存储中的文件有正确的换行格式。 - 输出CSV内容:直接将CSV字符串传给
Push-OutputBinding,避免默认的JSON序列化。
注意事项
- 确保Azure Function的输出绑定(
outputBlob)配置正确,目标Blob的后缀应为.csv,避免格式混淆。 - 如果需要处理特殊字符或非英语内容,可在转换后添加编码处理,例如:
$csvOutput = [System.Text.Encoding]::UTF8.GetBytes($csvOutput),需确认输出绑定支持字节数组类型。
内容的提问来源于stack exchange,提问作者Jishnu G S
相关产品推荐
相关产品推荐

