如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

huangapple go评论68阅读模式
英文:

How to convert JSON file content into Powershell object in Powershell runbook?

问题

我试图将存储账户数据中存在的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)

输出:

无法获取存储上下文。请传递存储上下文或设置当前存储上下文。
########newScheduledRules::###########@{Scheduled=System.Object[]; Fusion=System.Object[]; MLBehaviorAnalytics=System.Object[]; MicrosoftSecurityIncidentCreation=System.Object[]}

希望这对您有所帮助。

英文:

I'm trying to convert JSON file which is present in storage account data into Powershell object. But I'm not getting the proper output. Output does not contain proper values.

My code:

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

答案1

得分: 1

我在我的环境中重现并获得了如下预期结果,并按照以下过程操作,参考了Microsoft文档

Scheduled.json:

{
    "Rithwik": "Hello",
    "Chotu": "Bojja"
}

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

首先,我创建了一个存储帐户,然后按照以下方式在文件共享中添加了Scheduled.json:

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

现在,我创建了一个运行簿,并在运行簿中执行了以下脚本:

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

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

输出:

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

这里的 $out 是目标变量。

Get-AzStorageFileContent 命令中,-Path 应该只是文件名 Scheduled.json

英文:

I have reproduced in my environment and got expected results as below and followed below process and followed Microsoft-Document:

Scheduled.json:

{
"Rithwik":"Hello",
"Chotu":"Bojja"
}

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

Firstly, I have created Storage account and then added a Scheduled.json in file share as below:

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

Now i have created a runbook and excuted below script in runbook as below:

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

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

Output:

如何将JSON文件内容转换为PowerShell运行簿中的PowerShell对象?

Here $out is the Destination Variable.

-Path should be Only the file name Scheduled.json in
Get-AzStorageFileContent command.

答案2

得分: 0

似乎缺少 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)
英文:

It seems the Get-AzureStorageFileContent is missing -Context parameter. It should be something like this

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

huangapple
  • 本文由 发表于 2023年1月6日 14:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定