我正在尝试让 Azure 自动化帐户将 .CSV 文件导出到存储帐户,但是我在这样做时遇到了很多麻烦。正如大多数文档所说,使用 AzureRM 和 Azure RM 正在贬值。
这是我尝试传输 .CSV 的方式
$SecureAppSecret = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential($AppId, $SecureAppSecret)
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
[String]$StorageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupID -Name $StorageAccountName
$azure_StorageContextSplat = @{
StorageAccountName = $StorageAccountName
StorageAccountKey = $StorageAccountKey
}
$storageContext = New-AzStorageContext @azure_StorageContextSplat
$Report | Export-CSV -Path "~\File.csv"
$azure_FileToUploadSplat = @{
Context = $storageContext
File = "~\File.csv"
Container = $StorageContainer
Force = $true
}
Set-AzStorageBlobContent @azure_FileToUploadSplat
$AppSecret、$AppId、$TenantId、$ResourceGroupID、$StorageAccountName 和 $storageContext 都在脚本的前面以明文形式提供(我知道它不安全,但我希望它能正常工作,然后我可以确保它安全)
在 Azure 自动化测试窗格中运行它后,我得到以下 2 个错误:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Could not get the storage context. Please pass in a storage context or set the current storage context.
我相信第一个是指 StorageAccountKey 但目前我不知道。任何帮助都将不胜感激。 谢谢。
传输文件的简单方法是以下过程并将其作为替代方法,因为它不太安全:
我已经在我的环境中复制,下面的脚本对我有用,我遵循了Microsoft-Document:
Import-Module -Name Azure.Storage
$storageAccName = "rithwikstorage"
$storageAccKey = "ohNJHBheHNb/gSSBWXUxt3dxrw+AStXNnJyQ=="
$context = New-AzStorageContext -StorageAccountName $storageAccName -StorageAccountKey $storageAccKey
$conName = "name of the conatiner"
$Report =Get-AzResource | Export-CSV -Path "~\File.csv"
Set-AzStorageBlobContent -Context $context -Container $conName -File "~\File.csv"
输出:
然后在存储帐户中:
这里我直接给出了存储帐户密钥、名称和容器名称,我得到了想要的输出,尝试按照相同的方式获得想要的输出。