0

I am working in Azure DevOps, creating an automated build process.

Doing the PowerShell command:

$batchNodes = az batch node list --pool-id $poolId

...results in a json structure complete with all Nodes in my Pool, and all Node details. I am only looking for a list of node ids though, because I need to restart each Node in the Pool, which requires a Node id.

I expected this PowerShell command to work, but it doesn't. ($batchNodes is blank)

$batchNodes = az batch node list --pool-id $poolId | ConvertFrom-Json

Is there a fancier or simpler method I can use to get an array of Node id values from the 'az batch node list' command results?

I'm using PowerShell 7.2. If you know of some way to restart all of the Nodes in a Pool, please share this information.

Much thanks.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Jonathan Hansen
  • 423
  • 1
  • 7
  • 14
  • What if you wrap the command in parenthesis? i.e. `(az batch node list --pool-id $poolId) | ConvertFrom-Json`? – Daniel Mann Nov 02 '22 at 00:19
  • 1
    if `az batch node list --pool-id $poolId` outputs actual JSON text (that isn't an empty array or object) and `ConverFrom-Json` doesn't report any errors (make sure that `$ErrorActionPrreference` is at its default, `'Continue'`), `$batchNodes` cannot plausibly be blank. – mklement0 Nov 02 '22 at 02:12
  • You are correct mklement0. I was trying to output the entirety of $batchNodes with a Write-Output, and it was blank. But when I changed it to $batchNodes[0], I was able to see the data within it. – Jonathan Hansen Nov 02 '22 at 17:54
  • So it sounds like the problem was simply _visualizing_ the results, which can indeed be tricky with object graphs parsed from JSON with `ConvertFrom-Json`. The linked duplicate discusses solutions. – mklement0 Dec 01 '22 at 18:58

2 Answers2

1

For using Azure Cli, you could use Azure Cli task in Azure DevOps services to run the Azure Cli.

This is my sample:

steps:
- task: AzureCLI@2
  displayName: 'Azure CLI '
  inputs:
    azureSubscription: 'internalsub-wsupport'
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
     $batchNodes = az batch node list --pool-id mypoolwutong --account-name vwutong196420 --account-endpoint vwutong196420.eastasia.batch.azure.com | ConvertFrom-Json

This is the output: enter image description here

Hope that could do some help.

Antonia Wu-MSFT
  • 499
  • 2
  • 4
0

It turns out my code was working. I was trying to output the result incorrectly. This works:

$batchNodes = (az batch node list --pool-id $poolId) | ConvertFrom-Json

Write-Output "batchNodes:"
Write-Output "Index 0: $($batchNodes[0])"
Jonathan Hansen
  • 423
  • 1
  • 7
  • 14