繁体   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