Home > Net >  How to convert json file content into powershell object in powershell runbook?
How to convert json file content into powershell object in powershell runbook?

Time:01-09

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

CodePudding user response:

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)

CodePudding user response:

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"
}

enter image description here

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

enter image description here

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)

enter image description here

Output:

enter image description here

Here $out is the Destination Variable.

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

  • Related