簡體   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