【问题标题】:Azure DevOps 'az batch node list' with PowerShell ConvertFrom-Json not workingAzure DevOps \'az batch node list\' with PowerShell ConvertFrom-Json not working
【发布时间】:2022-12-02 02:17:20
【问题描述】:

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.

【问题讨论】:

  • What if you wrap the command in parenthesis? i.e. (az batch node list --pool-id $poolId) | ConvertFrom-Json?
  • 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.
  • 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.

标签: powershell azure-devops azure-batch


【解决方案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:

Hope that could do some help.

【讨论】:

  • Hi @Jonathan Hansen, does the above steps could do some help? If not, please kindly tell and I will try to do further investigation.
【解决方案2】:

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])"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-02
    • 2020-12-27
    • 2014-11-21
    • 2022-12-28
    • 2019-07-09
    • 2022-11-20
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多