VMware Cloud Community
hharold
Enthusiast
Enthusiast
Jump to solution

PowerCLI: Detect incorrect Time Configuration

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..... Smiley Wink )

Thanks for your help.

Regards,

Harold

0 Kudos
48 Replies
sushil_mudgil
Contributor
Contributor
Jump to solution

Excellent. Got the script to Run now and It compares the time with my loged in server and shows me output as time within range.

I have bought down the tolerance limit to 2 sec and perfect.

Just wondering why i am unable to give you point on the excellent help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can only give points when you start the thread.

No problem, glad your problem got solved.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc

This script works great! How could I allow for the different time zones?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing

[DateTime]::UtcNow

into

[DateTime]::Now


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I'm getting the folloing error regardless if I'm using [DateTime]::UTCNow or [DateTime]::Now...

Method invocation failed because [System.Object[]] doesn't contain a method nam
ed 'QueryDateTime'.
At D:\VPO\test\test.ps1:30 char:27
+    $t = $dts.QueryDateTime <<<< ()
    + CategoryInfo          : InvalidOperation: (QueryDateTime:String) [], Run
   timeException
    + FullyQualifiedErrorId : MethodNotFound

I found an article on the web that suggested using another ]

[DateTime]]::Now

but this didn't work

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you are are running in "multiple" mode (see Set-PowerCLIConfiguration) and that you are connected to more than 1 vSphere server.

You can check with

$defaultVIServers.

As an effect the variable $dts will be an array, which doesn't have the QueryDateTime method.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

It's running better now with only one error...

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At D:\VPO\test\test.ps1:31 char:19
+    $dts = get-view <<<<  $_.ConfigManager.DateTimeSystem
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

I ran Set-PowerCLIConfiguration -DefaultVIServerMode 'Single' -Confirm:$false to cahnge the DefaultVIServerMode.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the $_ is empty.

Are you sure the previous 'Get-View -ViewType HostSystem ...' returns anything ?

Is the Filter correct ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

The filter does return reults. The script starts out with no errors then somewhere in between the the output I get the error.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the servers returned by

get-view -ViewType HostSystem -Property Name, ConfigManager.DateTimeSystem -Filter @{"Name"=$esxnames}

is there perhaps a host that is powered off ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

there was one host that was powered off but its still came back with

esx01 within allowed range

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And is that the host that is causing the error message on the

$dts = get-view $_.ConfigManager.DateTimeSystem

line ?

If you don't have a debugger, you can show the name of the host on screen.

Just add the following line before the line above (inside the code block)

Write-Host $_.Name

I suspect the test succeeds because the script uses the value in $t from the previous host.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

OK here are my findings:

When I ran the script in my prodcution environment I got the following error:

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At D:\VPO\test\test.ps1:32 char:19
+    $dts = get-view <<<<  $_.ConfigManager.DateTimeSystem
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

When I ran the script in my test environment of 2 hosts I got this error:

esx02.flotech.lab.org
Exception calling "QueryDateTime" with "0" argument(s): "Unable to communicate
with the remote host, since it is disconnected."
At E:\Scripts\Test\test.ps1:35 char:27
+    $t = $dts.QueryDateTime <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

The operation '[$null] - [System.DateTime]' is not defined.
At E:\Scripts\Test\test.ps1:38 char:15
+    $s = ( $t - <<<<  [DateTime]::UtcNow).TotalSeconds
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NotAdefinedOperationForTypeType

Time on esx02.flotech.lab.org is within allowed range
esx01.flotech.lab.org
Time on esx01.flotech.lab.org is within allowed range

The common factor in both tests are that the hosts in both environments were disconnected.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, that makes sense.

The solution is to test if the host is powered on and connected, if not display a message.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc

How would you change the $allowedDifference to return minutes instead of seconds?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would add a line and change the 1st line, something like this

$allowedDifferenceMinutes = 2
$allowedDifferenceSeconds = $allowedDifferenceMinutes * 60


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Still sending output in seconds:

HostName                                       Seconds

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

ESX5Host1                                   -341.7298868 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see, you mean the output.

Then you can change this line

$s = ( $t - [DateTime]::UtcNow).TotalSeconds

into

$s = ( $t - [DateTime]::UtcNow).TotalMinutes

And change the property name of course.

Note that this will return the minutes as a fraction if the difference is not a multiple of 60 seconds.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

That worked. Thanks!

0 Kudos
bszweda83
Contributor
Contributor
Jump to solution

Wouldn't setting the NTP.CONF file with tinker panic 0 solve the issue with a large drift in time causing the Host not to be able to sync with the NTP server? My environment has this issue and I am trying to determine the best way to solve it.

Thanks,

Brad

0 Kudos