0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ritwik .. Can you please look into this query ? stackoverflow.com/questions/75136560/…
0

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)

2 Comments

Hi Duc Nguyen, able to sort out the context issue. But the json content is coming in the incorrect format while converting to powershell object. Output have been mentioned in the question itself
Look at your code, I think you haven't specified the destination of the file that you want to download. See here for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.