skip to Main Content

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:TempScheduled.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


  1. 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 "$OutPathScheduled.json" -Raw | ConvertFrom-Json
    Write-Output ("########newScheduledRules::###########" + $newScheduledRules)
    
    Login or Signup to reply.
  2. 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 "$outScheduled.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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search