Hi all,
I am looking for PowerCLI code to detect "Incorrect Date&Time Settings"
We have some ESXi 4.0 U1 hosts (Dell blades) of which the BIOS time has a too large offset to the NTP server.
Therefore it will never sync the time.
In vCenter the Date & Time Configuration is shown as red (differs too much from client).
Is there any Powershell code to detect which host has a misconfigured date / time setting?
(I don't like going through 200 hosts manually.....
)
Thanks for your help.
Regards,
Harold
Hi,
Here is simple that can do that for you:
$allowedDifferenceSeconds = 20
get-vmhost | %{
#get host datetime system
$dts = get-view $_.ExtensionData.configManager.DateTimeSystem
#get host time
$t = $dts.QueryDateTime()
#calculate time difference in secconds
$s = ( $t - [DateTime]::UtcNow).TotalSeconds
#check if time difference is too much
if( $s -gt $allowedDifferenceSeconds){
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $_.Name
$row.Seconds = $s
$row
}
}
Regards,
Nedko Nedev
PowerCLI Development Team
Hello Nedko,
Thanks for your quick reply.
This code does look like what I am looking for, but it does not seem to work.
Is this by any chance PowerCLI 4.1 code?
We are still on 4.0....
Thanks again.
Regards,
Harold
Hi,
VMHost.ExtensionData property is new to PowerCLI 4.1 and in order to run this script with PowerCLI 4.0 you should replace it with get-view
invocation:
Get-VMhost | Get-view |% {
#get host datetime system
$dts = get-view $_.ConfigManager.DateTimeSystem
...
Here is the final version of the script:
$allowedDifferenceSeconds = 20
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( $s -gt $allowedDifferenceSeconds){
#print host and time difference in seconds
$row = "" | select HostName, Seconds
$row.HostName = $_.Name
$row.Seconds = $s
$row
}
}
It's using Get-View -ViewType HostSystem since it's the fastest way to get view for each VMhost object
Regards,
Yasen Kalchev
PowerCLI Dev Team
Thanks all, this works fine!
I just made one change, to also check hosts having a negative time offset:
if( $s -gt $allowedDifferenceSeconds -or $s -lt (0 - $allowedDifferenceSeconds) ){
Regards,
Harold
You could also use the Absolute value mathematical function and use a shorter condition
if([math]::abs($s) -gt $allowedDifferenceSeconds){
____________
Blog: LucD notes
Twitter: lucd22
Thnaks Luc,
I knew there was some way to make it absolute, this solves all my questions on this topic.
Thanks guys !
Someone could tell me how can I run this script I have VMware 4.1 Update 1 vSphere PowerCLI
thanks for you help.
tomas!
Have you installed the latest PowerrCLI build on a Windows box ?
If yes,
Thanks thanks for answering ...
I did the steps described, and the console will not mark any error, but does not return any results. Any ideas?
We appreciate your help.!!
The script tests if there is a time drift greater than 20 seconds.
If not, there is no output.
The following version adds a positive message to the script.
$allowedDifferenceSeconds = 20
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"
}
}
Thanks a lot; this version is very helpful to me, thank you very much.
tom.