How to get powerstate of vmss...?

Abhishek P 20 Reputation points
2025-03-21T12:41:14.85+00:00

I am working with Azure Virtual Machine Scale Sets (VMSS) and I need to retrieve the PowerState of instances within a scale set using PowerShell.

I am currently using the following script:



$resourceGroupName = "Mbdajshbcsc"
$vmssName = "cscs"
$vmInstances = Get-AzVmssVM -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssName
$vmInstances | ForEach-Object {
    $instanceId = $_.InstanceId
    $powerStateStatus = $_.InstanceView.Statuses | Where-Object { $_.Code -like "PowerState/*" }
    if ($powerStateStatus) {
        $powerState = $powerStateStatus.DisplayStatus
    } else {
        $powerState = "PowerState info not available"
    }
    Write-Output "Instance ID: $instanceId, Power State: $powerState"
}

But when I run the script, I am getting "PowerState info not available". The VMSS instances are running, but it seems like the PowerState info isn't showing up.

Could anyone help me with the correct approach to get the PowerState for VMSS instances or explain why the power state might not be available?

Thanks in advance!

Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
443 questions
0 comments No comments
{count} votes

Accepted answer
  1. RithwikBojja 2,240 Reputation points Microsoft External Staff Moderator
    2025-03-24T05:15:44.5066667+00:00

    Hi @Abhishek P,

    Below script works which gives the Power State of VMSS:

    
    $rsgrpname = "rithwik"
    
    $vmssperu = "test67"
    
    $vmssInstances = Get-AzVmssVM -ResourceGroupName $rsgrpname -VMScaleSetName $vmssperu -InstanceView
    
    $vmssInstances | ForEach-Object {
    
        $instId = $_.InstanceId
    
        $vmName = $_.Name
    
        $instanceDetails = Get-AzVmssVM -ResourceGroupName $rsgrpname -VMScaleSetName $vmssperu -InstanceId $instId -InstanceView
    
        $rith_power = ($instanceDetails.Statuses | Where-Object { $_.Code -match "PowerState" })?.DisplayStatus
    
        [PSCustomObject]@{
    
            VMName            = $vmName
    
            InstanceID        = $instId
    
            ProvisioningState = $_.ProvisioningState
    
            PowerState        = $rith_power
    
        }
    
    }
    
    

    I have VMSS Powers state as below:

    enter image description here

    Output:

    enter image description here

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Luis Arias 8,601 Reputation points Moderator
    2025-03-21T15:48:12.9633333+00:00

    Hello Abhishek,

    Welcome to Q&A, For some reason the API for Azure VMSS isn't receiving the -instance-view parameter correctly, so here a workaround using the Virtual Machine Cmdlet , I've already tested and this works perfectly fine.

    $resourceGroupName = "<your-resource-group>"
    $vmssName = "your-vmss-name"
    $vmInstances = Get-AzVmssVM -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssName 
    
    $vmInstances | ForEach-Object {
        $instanceId = Get-AzVM -ResourceGroupName $resourceGroupName -name $_.Name -Status
        $powerStateStatus = $instanceId.Statuses | Where-Object { $_.code -like "PowerState/*" }
        if ($powerStateStatus) {
            $powerState = $powerStateStatus.DisplayStatus
        } else {
            $powerState = "PowerState info not available"
        }
        Write-Output "Instance ID: $($instanceId.Name), Power State: $powerState"
    }
    
    
    

    There another alternative using the API but it looks like require much more effot :

    If the information helped address your question, please Accept the answer.

    Luis

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.