在PowerShell Runbook中将存储账户JSON转为PowerShell对象失败求助
问题解决步骤
1. 修复存储上下文传递错误
你的Get-AzureStorageFileContent命令未传入已创建的存储上下文,导致报错。需添加-Context参数指定上下文:
$storageAccountKey = "xxxx" $Context = New-AzStorageContext -StorageAccountName 'xxxx' -StorageAccountKey $storageAccountKey $b='C:\Temp\Scheduled.json' # 新增-Context参数传递上下文 Get-AzureStorageFileContent -Path $b -ShareName "file-share-name" -Context $Context $newScheduledRules = Get-Content -Raw $b | ConvertFrom-Json
2. 正确查看转换后的JSON对象内容
用字符串拼接输出对象时,PowerShell会默认显示对象的类型摘要而非具体内容。可通过以下方式查看:
- 直接输出对象(PowerShell会自动格式化展示):
Write-Output "########newScheduledRules::###########" $newScheduledRules - 查看特定数组的具体元素:
# 查看Scheduled数组内容 $newScheduledRules.Scheduled # 或转为带缩进的JSON格式输出 $newScheduledRules | ConvertTo-Json -Depth 10
完整修正后的代码
$storageAccountKey = "xxxx" $Context = New-AzStorageContext -StorageAccountName 'xxxx' -StorageAccountKey $storageAccountKey $localPath = 'C:\Temp\Scheduled.json' $shareName = "file-share-name" # 从存储文件共享下载文件,指定上下文 Get-AzureStorageFileContent -Path $localPath -ShareName $shareName -Context $Context # 转换为PowerShell对象 $newScheduledRules = Get-Content -Raw $localPath | ConvertFrom-Json # 输出完整对象内容 Write-Host "########newScheduledRules::###########" $newScheduledRules # 可选:查看具体数组元素 Write-Host "`n########Scheduled Rules Details::###########" $newScheduledRules.Scheduled
内容的提问来源于stack exchange,提问作者dev333
相关产品推荐
相关产品推荐

