我正在尝试将存储帐户数据中存在的 JSON 文件转换为 Powershell 对象。但我没有得到正确的输出。输出不包含正确的值。
我的代码:
$storageAccountKey = "xxxx"
$Context = New-AzStorageContext -StorageAccountName 'xxxx' -StorageAccountKey $storageAccountKey
$b='C:\Temp\Scheduled.json'
Get-AzureStorageFileContent -Path $b -ShareName "file-share-name"
$newScheduledRules = Get-Content -Raw $b | ConvertFrom-Json
Write-Output ("########newScheduledRules::###########" + $newScheduledRules)
输出:
Could not get the storage context. Please pass in a storage context or set the current storage context.
########newScheduledRules::###########@{Scheduled=System.Object[]; Fusion=System.Object[]; MLBehaviorAnalytics=System.Object[]; MicrosoftSecurityIncidentCreation=System.Object[]}
最佳答案
我已经在我的环境中重现并得到了如下预期结果,并遵循以下过程并遵循 Microsoft-Document :
Scheduled.json:
{
"Rithwik":"Hello",
"Chotu":"Bojja"
}
首先,我创建了存储帐户,然后在文件共享中添加了 Scheduled.json,如下所示:
现在我已经创建了一个 Runbook 并在 Runbook 中执行了以下脚本,如下所示:
$storageAccountKey = "PFHxFbVmAEvwBM6/9kW4nORJYA+AStA2QQ1A=="
$Context = New-AzStorageContext -StorageAccountName 'rithwik' -StorageAccountKey $storageAccountKey
$out = "$($env:TEMP)\$([guid]::NewGuid())"
New-Item -Path $out -ItemType Directory -Force
Get-AzStorageFileContent -ShareName "rithwik" -Context $Context -Path 'Scheduled.json' -Destination $out -Force
$newScheduledRules = Get-Content -Path "$out\Scheduled.json" -Raw | ConvertFrom-Json
Write-Output ("########newScheduledRules::###########" + $newScheduledRules)
输出:
这里$out是目标变量。
-Path 应该仅是文件名 Scheduled.json
Get-AzStorageFileContent
命令。
关于azure - 如何在Powershell Runbook中将JSON文件内容转换为Powershell对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75027703/