繁体   English   中英

将 Powershell Object CSV 字符串作为文件保存到 Z3A580F142203677F1F0BC30898F

[英]Saving Powershell Object CSV String as a file to Azure Blob Storage

我们正在生成 Powershell object 到 Azure 运行手册,基本上将其转换为 ZCC8D68C531C4ADEAFEDE6D3 字符串。

We want to store this generated CSV String (generated from Azure Powershell Runbook) into Azure Blob Storage as a CSV File. 有人可以帮助我如何以及如何使用 powershell 命令将此 CSV 字符串作为文件保存到 Azure Blob 存储? 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会帮助我。

谢谢

尝试以下代码 - 进行一些修改。 本质上的想法是将 CSV 文件存储在本地,然后将其上传到 blob 存储(我在本地进行了测试,但不是从 Runbook 进行测试,但它也应该在那里工作):

#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

具有 Azure 功能的替代解决方案

尝试让您的 Runbook 调用您的 Http 触发 Azure function,并将字符串作为参数传递,或在正文中传递。 这只是一个简单的 REST API 调用。

In Azure function, you can have Python, NodeJS or C# code that would put your string to CSV file in the blob store. 这个主题有很多教程,但首先,您需要将字符串转换为 AF :)

看看下面的例子,并尝试类似的东西(我没有测试过)。 本质上,这个想法是调用简单的 REST API 调用并在请求正文中传递您的有效负载:

[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'.

我刚看到这个帖子。 我有同样的问题。

我想将 csv 中的查询结果直接导出到 blob 存储中。 我使用 Azure 运行手册。 你能找到解决办法吗?

谢谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM