use*_*012 6 azure azure-powershell azure-blob-storage
我们通过 Azure Runbook 生成 Powershell 对象,并将其基本上转换为 CSV 字符串。
我们希望将此生成的 CSV 字符串(从 Azure Powershell Runbook 生成)作为 CSV 文件存储到 Azure Blob 存储中。有人可以帮助我如何以及使用 whcih powershell 命令将此 CSV 字符串作为文件保存到 Azure Blob 存储吗?我尝试环顾四周,发现了 Push-OutputBindings 函数,但不确定如何在 Azure Powershell Runbook 中使用该函数来导入哪个模块,也不确定它是否是 Azure Functions V2 的一部分,但了解如何使用它的一些基础知识会帮助我的。
谢谢
尝试以下代码 - 进行一些修改。本质上的想法是在本地存储 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
Run Code Online (Sandbox Code Playgroud)
使用 Azure Functions 的替代解决方案
尝试让 Runbook 调用 Http 触发的 Azure 函数,并将字符串作为参数或在正文中传递。这只是一个简单的 REST API 调用。
在 Azure 函数中,您可以使用 Python、NodeJS 或 C# 代码将字符串放入 Blob 存储中的 CSV 文件中。关于这个主题有很多教程,但首先,您需要将字符串设置为 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
Run Code Online (Sandbox Code Playgroud)
从 Azure 门户获取的 Azure 函数 URL+代码,如果单击 Azure 函数,您将看到“获取函数 Url”按钮。
归档时间: |
|
查看次数: |
4626 次 |
最近记录: |