Greetings Community!
Hoping someone with the expertise would help me with the following:
I work in a medium-to-large environment of ESXi hosts managed from over 25 vCenters.
Discovered major time drifts on many hosts due to a number of reasons and corrected them.
To check time sync on a regular basis, did the usual Google thing and found a nice script (posted below) from LucD --Thanks Man--
It works great except that due to various reasons, we have a large number of hosts waiting to be decommissioned that are at the moment either in a Not Responding or Disconnected state.
These, of course, cause the "Unable to communicate with the remote host, since it is disconnected." error and I was wondering how I can make the script run ONLY against hosts "Connected" and in "Maintenance mode". (or ignore hosts NOT in these two connection states)
I could include here a list of things I have been trying for last week or two (without success) but that would make the post too long and risk not being read due to excessive length.
My PoweerCLI skills are not up to par yet hence my request for help.
Thanks a bunch for any and all suggestions!
========================================================================================================================================================
$allowedDifferenceSeconds = 1
get-view -ViewType HostSystem -Property Name, ConfigManager.DateTimeSystem | %{
#get host datetime system
$dts = get-view $_.ConfigManager.DateTimeSystem
#get host time
$t = $dts.QueryDateTime()
#calculate time difference in seconds
$s = ( $t - [DateTime]::UtcNow).TotalSeconds
#check if time difference is too much
if([math]::abs($s) -gt $allowedDifferenceSeconds){
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $_.Name
$row.Seconds = $s
$row
}
else{
Write-Host "Time on" $_.Name "within allowed range"
}
}
The Filter option of the Get-View cmdlet would allow you to do that.
PS: I used splatting to avoid a long line with Get-View
Something like this
$sView = @{
ViewType = 'HostSystem'
Property = 'Name', 'ConfigManager.DateTimeSystem', 'Runtime.ConnectionState'
Filter = @{
'Runtime.ConnectionState' = 'connected|maintenance'
}
}
Get-View @sView | % {
#get host datetime system
$dts = Get-View $_.ConfigManager.DateTimeSystem
#get host time
$t = $dts.QueryDateTime()
#calculate time difference in seconds
$s = ( $t - [DateTime]::UtcNow).TotalSeconds
#check if time difference is too much
if ([math]::abs($s) -gt $allowedDifferenceSeconds)
{
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $_.Name
$row.Seconds = $s
$row
}
else
{
Write-Host "Time on" $_.Name "within allowed range"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The Filter option of the Get-View cmdlet would allow you to do that.
PS: I used splatting to avoid a long line with Get-View
Something like this
$sView = @{
ViewType = 'HostSystem'
Property = 'Name', 'ConfigManager.DateTimeSystem', 'Runtime.ConnectionState'
Filter = @{
'Runtime.ConnectionState' = 'connected|maintenance'
}
}
Get-View @sView | % {
#get host datetime system
$dts = Get-View $_.ConfigManager.DateTimeSystem
#get host time
$t = $dts.QueryDateTime()
#calculate time difference in seconds
$s = ( $t - [DateTime]::UtcNow).TotalSeconds
#check if time difference is too much
if ([math]::abs($s) -gt $allowedDifferenceSeconds)
{
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $_.Name
$row.Seconds = $s
$row
}
else
{
Write-Host "Time on" $_.Name "within allowed range"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Happy to report it works as requested.
Truly amazing!
Thanks a million, LucD!!!