I want to formalise the ownership of an ESXi host once placed into Maintenance Mode as we have a sprawling global team who manage multiple global vCenters and ESXi clusters.
I currently have:
1. A loop that identifies all hosts across multiple vCenters that have been placed in Maintenance Mode.
2. A command that queries the hostd log of an ESXi host for a line that confirms the host was successfully placed into Maintenance Mode.
3. If an entry in the ESXi host's hostd log isn't found (The ESXi logs persistence problem), then I'd like to give the User a default value of "Unknown", or an empty string.
I'm looking for any assistance possible to either combine the 2 together to output a table along the lines of the following columns:
| vCenter Name| Datcenter Name | Cluster Name | ESXi Host Name | ConnectionState | User Who Placed Host in MM |
1. Code: The Loop that identifies all hosts in Maintenance Mode
Connect-viserver -Server (Get-Content C:\Scripts\vCList.txt) > $null
$report = Foreach($vc in $global:DefaultVIServers){
foreach($dc in Get-Datacenter -Server $vc){
Get-Cluster -Location $dc -Server $vc |
Get-VMHost | Where {$_.ConnectionState -like "Maintenance"} |
Select @{N='VC';E={$vc.Name}},
@{N='Datacenter';E={$dc.Name}},
@{N='Cluster';E={$_.Name}},
@{N='ConnectionState';E={$_.ConnectionState}}
}
}
$report | Export-Csv C:\Scripts\List-Hosts-in-MM.csv
2. Code: The Hostd log query identifying the user who placed the host in MM
(Get-Log -VMHost (Get-VMHost) hostd).Entries | Where {$_ -like "*enterMaintenanceMode* Status success*"}
Sample Output of above command:
2018-11-07T14:53:59.066Z info hostd[ACGBGH20] [Originator@1236 sub=Vimsvc.TaskManager opID=B26B8B63-00000012-f1-b7-1e63 user=vpxuser:MyDOM\UserA] Task Completed : haTask-ha-host-vim.HostSystem.enterMaintenanceMode-83345579 Status success