簡體   English   中英

powershell runbook中如何將json文件內容轉換為powershell object?

[英]How to convert json file content into powershell object in powershell runbook?

我正在嘗試將存儲帳戶數據中存在的 json 文件轉換為 powershell object。但我沒有得到正確的 output。Output 不包含正確的值。

我的代碼:

$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)

Output:

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[]}

Get-AzureStorageFileContent似乎缺少-Context參數。 應該是這樣的

$OutPath = "$($env:TEMP)\$([guid]::NewGuid())"
New-Item -Path $OutPath -ItemType Directory -Force

$storageContext = (Get-AzStorageAccount -ResourceGroupName xxxx -Name xxxx).Context

Get-AzStorageFileContent -ShareName "file-share-name" -Context $storageContext -Path 'Scheduled.json' -Destination $OutPath -Force
$newScheduledRules = Get-Content -Path "$OutPath\Scheduled.json" -Raw | ConvertFrom-Json
Write-Output ("########newScheduledRules::###########" + $newScheduledRules)

我已經在我的環境中復制並獲得如下預期結果並遵循以下過程並遵循Microsoft-Document

預定.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)

在此處輸入圖像描述

Output:

在此處輸入圖像描述

這里$out是目標變量。

-Path應該只有Get-AzStorageFileContent命令中的文件名Scheduled.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM