如何使用 PowerShell 将导出的 CSV 文件动态写入 Azure blob 容器?
How to Dynamically write a export CSV file to Azure blob container using PowerShell?
我有一个导出 VM 指标信息的 Azure Powershell 命令。
(Get-AzMetric -ResourceID $id -StartTime $s -EndTime -MetricName $metric).Data |Export-Csv -Path $blobpath
$blobpath 我已经给出了 blob 容器的 SAS URL。
我想为 -Path 参数添加 blobpath,但收到错误“找不到驱动器”。
[![
]1]1
感谢您的建议Gaurav Mantri
解决方案1:首先需要将导出的csv文件保存在本地系统中,然后需要将其上传到Azure存储中的容器中。我在我的系统中试过了
使用 Set-AzStorageBlobContent cmdlet 将此 .csv 文件上传到 blob 存储
试试这些步骤
1) 使用 New-AzStorageContext
生成上下文
$sas_token="SAS”
$account_name = "your_storage_account_name"
$context = New-AzStorageContext -StorageAccountName $account_name -SasToken $sas_token
2) 使用 Set-AzStorageBlobContent 将 csv 文件上传到 blob 存储
Set-AzStorageBlobContent -Container "the container name" -File " file path Ex: c:\myfolder\test.csv" " -Context $context
输出
上传到Azure容器的文件
方案二:使用sas直接上传内容到Azure存储Url.
试试这个命令
$accountName = "storage-account-name"
$accountKey = "storage-account-key"
$containerName = "blob-container-name"
$blobName = "blob-name.csv"
# Get storage context
$context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey
# Get Shared Access Signature (SAS) Token expiration time. I have set it to expire after 1 hour.
$sasExpiry = (Get-Date).AddHours(1).ToUniversalTime()
# Get a SAS Token with "write" permission that will expire after one hour.
$sasToken = New-AzStorageBlobSASToken -Context $context -Container $containerName -Blob $blobName -Permission "w" -ExpiryTime $sasExpiry
# Create a SAS URL
$sasUrl = "https://$accountName.blob.core.windows.net/$containerName/$blobName$sasToken"
# Set request headers
$headers = @{"x-ms-blob-type":"BlockBlob"}
# Set request content (body)
$body = "This is the content I wish to upload"
#Invoke "Put Blob" REST API
Invoke-RestMethod -Method "PUT" -Uri $sasUrl -Body $body -Headers $headers -Content-Type "text/csv
"
我有一个导出 VM 指标信息的 Azure Powershell 命令。
(Get-AzMetric -ResourceID $id -StartTime $s -EndTime -MetricName $metric).Data |Export-Csv -Path $blobpath
$blobpath 我已经给出了 blob 容器的 SAS URL。
我想为 -Path 参数添加 blobpath,但收到错误“找不到驱动器”。 [![
]1]1
感谢您的建议Gaurav Mantri
解决方案1:首先需要将导出的csv文件保存在本地系统中,然后需要将其上传到Azure存储中的容器中。我在我的系统中试过了
使用 Set-AzStorageBlobContent cmdlet 将此 .csv 文件上传到 blob 存储
试试这些步骤
1) 使用 New-AzStorageContext
生成上下文$sas_token="SAS”
$account_name = "your_storage_account_name"
$context = New-AzStorageContext -StorageAccountName $account_name -SasToken $sas_token
2) 使用 Set-AzStorageBlobContent 将 csv 文件上传到 blob 存储
Set-AzStorageBlobContent -Container "the container name" -File " file path Ex: c:\myfolder\test.csv" " -Context $context
输出
上传到Azure容器的文件
方案二:使用sas直接上传内容到Azure存储Url.
试试这个命令
$accountName = "storage-account-name"
$accountKey = "storage-account-key"
$containerName = "blob-container-name"
$blobName = "blob-name.csv"
# Get storage context
$context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey
# Get Shared Access Signature (SAS) Token expiration time. I have set it to expire after 1 hour.
$sasExpiry = (Get-Date).AddHours(1).ToUniversalTime()
# Get a SAS Token with "write" permission that will expire after one hour.
$sasToken = New-AzStorageBlobSASToken -Context $context -Container $containerName -Blob $blobName -Permission "w" -ExpiryTime $sasExpiry
# Create a SAS URL
$sasUrl = "https://$accountName.blob.core.windows.net/$containerName/$blobName$sasToken"
# Set request headers
$headers = @{"x-ms-blob-type":"BlockBlob"}
# Set request content (body)
$body = "This is the content I wish to upload"
#Invoke "Put Blob" REST API
Invoke-RestMethod -Method "PUT" -Uri $sasUrl -Body $body -Headers $headers -Content-Type "text/csv
"