mulliganesx
Enthusiast
Enthusiast

VMHost Maintenance, TimeInMaintenance, Annotations Script

Jump to solution

Working out the logic for a script to capture, VMHosts in Maintenance, a single property from Get-annotation, and the length of time a host has been in maintenance.  I seem to have most of it down, however broken in separate parts..  I am looking to combine the logic, and find the value needed for the length of time a host has been in maintenace..  This is what i have currently, I tried two separate ways.

## 1st Method:  Get VMHosts, with Annotations
$ESXHosts = Get-VMHost -Location Datacenter01 -State Maintenance

ForEach ($ESXHost in $ESXHosts){
Get-Annotation $ESXHost
}

## 2nd Method:  Lists VMHosts, and notes

Get-VMHost -Location Datacenter01 | Where-Object {$_.ConnectionState -eq "Maintenance"} | Get-Annotation -Name "Notes:" | ft AnnotatedEntity,Value

#Get ESXi Hosts in Maintenance

Get-VMHost -Location MIT01-CAI -State Maintenance | ft name,connectionstate

#Get ESXi Host Notes

$ESXHosts = Get-VMHost -Location MIT01-CAI -State Maintenance | Get-Annotation -Entity $ESXHosts -CustomAttribute "Name"

# Get VMHosts, with Annotations

$ESXHosts = Get-VMHost -Location MIT01-CAI -State Maintenance

ForEach ($ESXHost in $ESXHosts){

    Get-Annotation $ESXHost

    }

#Lists VMHosts, and notes

Get-VMHost -Location MIT01-CAI | Where-Object {$_.ConnectionState -eq "Maintenance"} | Get-Annotation -Name "Notes:" | ft AnnotatedEntity,Value
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

That's a pity, you can only go back as far as your events are retained.


For combining those two, you could do something like this

Get-VMHost -Location MIT01-CAI | Where-Object {$_.ConnectionState -eq "Maintenance"} |

Get-Annotation -Name 'Notes:' |

Select @{N='VMHost';E={$_.AnnotatedEntity.Name}},

    @{N='ConnectionState';E={$_.AnnotatedEntity.ConnectionState}},

    Name,Value


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

5 Replies
LucD
Leadership
Leadership

You can get Maintenance Mode duration from the events (provided you keep these long enough).

Something like this for example

$start = (Get-Date).AddDays(-1)

$esx = Get-VMHost

Get-VIEvent -Entity $esx -Start $start -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.EnteringMaintenanceModeEvent] -or $_ -is [VMware.Vim.ExitMaintenanceModeEvent]} |

Group-Object -Property {$_.Host.Name} |

ForEach-Object -Process {

    $esxName = $_.Name

    $maint = $_.Group | Sort-Object -Property CreatedTime

    if($maint.Count%2 -eq 1){

        $maint = $maint[1..-1]

    }

    0..($maint.Count/2-1) | %{

        $start = $maint[$_*2].CreatedTime

        $maint[$_*2+1].CreatedTime - $maint[$_*2].CreatedTime |

        select @{N='VMHost';E={$esxName}},

            @{N='Start';E={$start}},

            @{N='Duration(Secs)';E={[math]::Round($_.TotalSeconds,0)}}

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mulliganesx
Enthusiast
Enthusiast

Thanks LucD, I'll review as my event logs only go back 2 weeks. 

What would you suggest in order to combine output from each of the below one liners?    Example:  "HostName, ConnectionState, AnnotatedEntity, Value"

## Lists hosts in maintenance mode, connection state

Get-VMHost -Location MIT01-CAI -State Maintenance | ft name,connectionstate

## Lists member properties of Notes

Get-VMHost -Location Datacenter01 | Where-Object {$_.ConnectionState -eq "Maintenance"} | Get-Annotation -Name "Notes:" | ft AnnotatedEntity,Value

0 Kudos
LucD
Leadership
Leadership

That's a pity, you can only go back as far as your events are retained.


For combining those two, you could do something like this

Get-VMHost -Location MIT01-CAI | Where-Object {$_.ConnectionState -eq "Maintenance"} |

Get-Annotation -Name 'Notes:' |

Select @{N='VMHost';E={$_.AnnotatedEntity.Name}},

    @{N='ConnectionState';E={$_.AnnotatedEntity.ConnectionState}},

    Name,Value


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

mulliganesx
Enthusiast
Enthusiast

Hi LucD, quick question for you so I can understand how you were able to arrive at your answer.  For the below hash table answer you provided, how were you able to see or know that 'ConnectionState' is a property of AnnotatedEntity?

​Get-help does not show any information on "AnnotatedEntity" as a property.  and when I do a Get-member of "AnnotatedEntity" I do not see Connectionstate listed.  Looking to piece together how you exactly did it..

0 Kudos
LucD
Leadership
Leadership

Under the AnnotatedEntity property, the cmdlet returns the entity on which the annotation is set. See the Return Type, the Annotation object, of the Get-Annotation cmdlet.
You will see that the documentation mentions an InventoryItem object.

If you click through on that InventoryIte, you come via VIContainer to VMHost.

And in there we find the Name and ConnectionState.


It is always a good habit to use the Get-Member cmdlet on the output of a cmdlet, that way you can see what type of object is behind a specific property.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos