PandzicM
Contributor
Contributor

Hosts in Maintenance Mode -Date

Hello

I am looking for powershell/powerCLI command/script that will gather ESXI hosts in maintenance mode with dates when they are placed to .

I will run across multiple vcenters and command/script out should have properties: HostName, vCenter and Date when placed in MM in excel

would you be able to help me here?

Thanks a bunch

18 Replies
a_p_
Leadership
Leadership

Discussion moved from VMware vSphere™ to VMware PowerCLI

0 Kudos
LucD
Leadership
Leadership

You could do something like this.
Adapt the Start date to your requirement.
This requires the ImportExcel module to be installed.

$global:DefaultViServers |

ForEach-Object -Process {

   $vc = $_

   Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) -Server $vc.Name |

   where{$_ -is [VMware.Vim.EnteredMaintenanceModeEvent]} |

   Sort-Object -Property {$_.CreatedTime.ToString('ddMMyyyy HH:mm:ss')},{$_.Host.Name} -Unique |

  Select CreatedTime,@{N='vCenter';E={$vc.Name}},@{N='VMHost';E={$_.Host.Name}}

} | Export-Excel -Path .\report.xlsx -WorksheetName 'MaintenanceMode' -Show


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

PandzicM
Contributor
Contributor

Hi LucD,

thanks you for your reply.

just to confirm - if I want to gather hosts in MM that they dates are older than 30 days I should set .AddDays(-30) right?

Thanks

0 Kudos
LucD
Leadership
Leadership

Not really, if you want MM older than 30 days, you should use the Finish parameter, and set that one to (Get-Date).AddDays(-30).
You can leave the Start parameter out in that case.


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

0 Kudos
PandzicM
Contributor
Contributor

Hi LicD

code modified as per your instructions:

connect-viserver -server vcenter1,vcenter2,vcenter3 -user XXXXX -password XXXXXX

$global:DefaultCisServers |

ForEach-Object -Process {

   $vc = $_

   Get-VIEvent -MaxSamples ([int]::MaxValue) -Finish (Get-Date).AddDays(-30) -Server $vc.Name |

   where{$_ -is [VMware.Vim.EnteredMaintenanceModeEvent]} |

   Sort-Object -Property {$_.CreatedTime.ToString('ddMMyyyy HH:mm:ss')},{$_.Host.Name} -Unique |

  Select CreatedTime,@{N='vCenter';E={$vc.Name}},@{N='VMHost';E={$_.Host.Name}}

} | Export-Excel -Path .\report.xlsx -WorksheetName 'MaintenanceMode' -Show

script run for some time and then excel opens with no data in it

am I missing something?

Thanks

0 Kudos
LucD
Leadership
Leadership

I had a typo.

The line

$global:DefaultCisServers

should say

$global:DefaultViServers


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

0 Kudos
PandzicM
Contributor
Contributor

no problem

I will make correction and shortly start script execution

will update asap

0 Kudos
PandzicM
Contributor
Contributor

correction has been made

ran against two vcenters that I know we have few hosts in maintenance mode for some time.

however once script started ran for about few minutes and created create empty excel file. Tried by changing to export-csv file...same outcome

remote signed policy already set

can you help?

0 Kudos
LucD
Leadership
Leadership

The script checks when an ESXi node was switched to maintenance mode, not if it currently is.
If the maintenance mode was entered more than 30 days, it will not show in the report.

If you want to see which nodes are in maintenance mode and since when, you could do.

Get-VMHost | where{$_.ConnectionState -eq 'Maintenance'} |

Select Name,

   @{N='Date';E={

   Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) |

   where{$_ -is [VMware.Vim.EnteredMaintenanceModeEvent]} |

   Sort-Object -Property CreatedTime -Descending |

  Select -First 1 -ExpandProperty CreatedTime

   }}

Note that since there is no Start parameter, the Get-ViEvent cmdlet will look at all events for that ESXi node.

You might add a Start parameter and limit how far back it looks.


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

0 Kudos
MuhamadEldan
Contributor
Contributor

Hi LucD,

I have tried your script howeveri am getting the following error


What i am searching for is a script that can show me the Hosts that are in maintenance mode and to know the date that they accessed maintenance mode.

0 Kudos
LucD
Leadership
Leadership

I don't see any error in your reply


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

0 Kudos
MuhamadEldan
Contributor
Contributor

Hi LucD,

The Error Disspared however i can't get the  date

please see the output

PowerCLI C:\Users\Muhamad.ElDanA\Desktop> & '.\Maintenance Mode.ps1'

Name                           Port  User

----                           ----  ----

VOVC01VW.eito-dublin.local     443   VSPHERE.LOCAL\Administrator

Name : vmhde025.dc-ratingen.de

Date :

Name : vmhde588.dc-ratingen.de

Date :

Name : vmhde333.dc-ratingen.de

Date :

Name : vmhde635.dc-ratingen.de

Date :

Name : vmhde467.dc-ratingen.de

Date :

Name : vmhde634.dc-ratingen.de

Date :

Name : vmhde447.dc-ratingen.de

Date :

Name : vmhde379.dc-ratingen.de

Date :

Do you think this because the host has rebooted and the logs was lost ?

0 Kudos
LucD
Leadership
Leadership

That indicates that the events aren't kept long enough in your environment to find the corresponding event.


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

0 Kudos
MuhamadEldan
Contributor
Contributor

Hi LucD,

I have just placed a host in MM and the log is present and i still can't see the date

PowerCLI C:\Users\Muhamad.ElDanA\Desktop> & '.\Maintenance Mode.ps1'

Name                           Port  User

----                           ----  ----

cidevcssrv12.admnet.vodafon... 443   VF-ROOT\Muhamad.ElDanA

Name : vmhdeaz11r03s23.admnet.vodafone.com

Date :

Name : vmhdeaz11r03s35.admnet.vodafone.com

Date :

Name : vmhdeaz11r01s11.admnet.vodafone.com

Date :

PowerCLI C:\Users\Muhamad.ElDanA\Desktop>

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership

Try adding a Start parameter (with a time before you placed that ESXi node in maintenance).

Just did a similar test, and it works for me.

Like this

Get-VMHost | where { $_.ConnectionState -eq 'Maintenance' } |

Select Name,

@{N = 'Date'; E = {

   Get-VIEvent -Entity $_ -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddMinutes(-10) |

   where { $_ -is [VMware.Vim.EnteredMaintenanceModeEvent] } |

   Sort-Object -Property CreatedTime -Descending |

  Select -First 1 -ExpandProperty CreatedTime

   }

   }


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

0 Kudos
MuhamadEldan
Contributor
Contributor

Thank you very much , it Worked now

Can you please tell me how the (Get-Date).AddMinutes(-10)  is working  , Because if i put (Get-Date).AddMinutes(-60)
which i suppose it means a start time that Current time - 60 Min , it's not showing the date

First Output is with (Get-Date).AddMinutes(-10)

PowerCLI C:\Users\Muhamad.ElDanA\Desktop> & '.\Maintenance Mode.ps1'

Name                           Port  User

----                           ----  ----

cidevcssrv12.admnet.vodafon... 443   VF-ROOT\Muhamad.ElDanA

Name : vmhdeaz11r03s26.admnet.vodafone.com

Date : 04.09.2019 16:37:55

Name : vmhdeaz11r03s35.admnet.vodafone.com

Date :

Name : vmhdeaz11r01s11.admnet.vodafone.com

Date :

Second Output with (Get-Date).AddMinutes(-60)

PowerCLI C:\Users\Muhamad.ElDanA\Desktop> & '.\Maintenance Mode.ps1'

Name                           Port  User

----                           ----  ----

cidevcssrv12.admnet.vodafon... 443   VF-ROOT\Muhamad.ElDanA

Name : vmhdeaz11r03s26.admnet.vodafone.com

Date :

Name : vmhdeaz11r03s35.admnet.vodafone.com

Date :

Name : vmhdeaz11r01s11.admnet.vodafone.com

Date :

PowerCLI C:\Users\Muhamad.ElDanA\Desktop>

0 Kudos
LucD
Leadership
Leadership

Yes, that should give the events from 1 hour ago till now.

Could it be that you are not archiving events in your environment?
Check with

Get-AdvancedSetting -Entity $global:DefaultVIServer -Name 'event.maxAge'


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

0 Kudos
MuhamadEldan
Contributor
Contributor

This is the output i got

PowerCLI C:\Users\Muhamad.ElDanA\Desktop> Get-AdvancedSetting -Entity $global:DefaultVIServer -Name 'event.maxAge'

Name                 Value                Type                 Description

----                 -----                ----                 -----------

event.maxAge         32                   VIServer             Maximum event age

Based on this article , this 32 should means 32 Days so i don't know why i am getting this behavior

Retention of Events in the vCenter Server Database ..

Anyway thank you very much for assistance.

0 Kudos