Search code examples
azurepowershellvirtual-machine

How to alter script to list VMAgent_Status as Not Ready


I have some virtual machines that are Running, but the Agent Status is showing Not Ready. I have a Powershell script that lists all the VMs in my Azure Subscription with VM_Name, ResourceGroup_Name, VMAgent_Status and VM_Agent_Version.

This is the script:

Set-AzContext -Subscription "My-AZ-Sub"
$WarningPreference = 'SilentlyContinue'
$output = @()
$VMs_running = Get-AzVM  -status | ?{$_.PowerState -eq 'VM running'}

 
foreach($VM in $VMs_running)
{
    $VM_status = Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status

    $Row = "" | select Name, ResourceGroupName, VMAgent_Status, VMAgent_Version
    $Row.Name = $VM_status.Name
    $Row.ResourceGroupName = $VM_status.ResourceGroupName
    $Row.VMAgent_Status = $VM_status.VMAgent.Statuses.DisplayStatus
    $Row.VMAgent_Version = $VM_status.VMAgent.VmAgentVersion
    
 
    $output += $Row
}
 
$output

This is an example of the output:

Name              : My-VM
ResourceGroupName : My-RG
VMAgent_Status    : Ready
VMAgent_Version   : 2.7.41491.1239

Is there a way I can add to the script to only list VMs that have VMAgent_Status to equal Not ready?


Solution

  • Is there a way I can add to the script to only list VMs that have VMAgent_Status to equal Not ready?

    To list out the Virtual machines that has VMAgent_Status is not ready, use below script.

    $output = @()
    $VMs_running = Get-AzVM  -status | ?{$_.PowerState -eq 'VM running'}
    foreach($VM in $VMs_running)
    {
        $VM_status = Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status
    
        $Row = "" | select Name, ResourceGroupName, VMAgent_Status, VMAgent_Version
        $Row.Name = $VM_status.Name
        $Row.ResourceGroupName = $VM_status.ResourceGroupName
        $Row.VMAgent_Status = $VM_status.VMAgent.Statuses.DisplayStatus
        $Row.VMAgent_Version = $VM_status.VMAgent.VmAgentVersion
        if ($Row.VMAgent_Status -ne "Ready"){
          $output += $Row
        }
    }
     $output
    

    Output:

    In my environment, all the virtual machine agents are in a ready status, as shown in the below output. Hence, I have received empty results.

    enter image description here

    Below is the output if I have given Agent status is in ready state.

    enter image description here