You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Automation无法将CSV传输至存储账户问题求助

问题分析与解决方案

错误根源

  • 第一个Base64格式错误:Get-AzStorageAccountKey返回的是存储账户密钥对象数组,直接赋值给[String]$StorageAccountKey会得到对象的字符串描述(而非实际密钥值),导致传入New-AzStorageContext的不是有效的Base64密钥。
  • 第二个存储上下文错误:由于密钥无效,New-AzStorageContext创建失败,后续Set-AzStorageBlobContent无法获取有效上下文,因此报错。

修正后的脚本

$SecureAppSecret = ConvertTo-SecureString -String $AppSecret -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential($AppId, $SecureAppSecret)
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential

# 修正:获取存储账户密钥的实际值(默认取Key1)
$StorageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupID -Name $StorageAccountName)[0].Value

$azure_StorageContextSplat = @{
    StorageAccountName = $StorageAccountName
    StorageAccountKey  = $StorageAccountKey
}
$storageContext = New-AzStorageContext @azure_StorageContextSplat

# 修正:使用Azure Automation临时目录(避免~路径权限问题)
$csvPath = Join-Path -Path $env:TEMP -ChildPath "File.csv"
$Report | Export-CSV -Path $csvPath -NoTypeInformation

$azure_FileToUploadSplat = @{
    Context   = $storageContext
    File      = $csvPath
    Container = $StorageContainer
    Force     = $true
}

Set-AzStorageBlobContent @azure_FileToUploadSplat

关键修改说明

  1. 存储账户密钥获取:
    Get-AzStorageAccountKey返回的对象包含KeyName和Value属性,通过[0].Value提取第一个密钥的实际值,也可通过Where-Object { $_.KeyName -eq "Key1" }指定具体密钥。
  2. CSV文件路径:
    Azure Automation环境中~指向的目录可能无写入权限,改用$env:TEMP系统临时目录更可靠;添加-NoTypeInformation避免CSV开头的类型注释干扰后续使用。

安全性优化建议(功能验证后)

将AppSecret、AppId、TenantId等敏感信息存储为Azure Automation的凭据资产,通过Get-AzAutomationCredential获取,避免明文硬编码泄露数据。

内容的提问来源于stack exchange,提问作者BPengu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 21:48:19