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

Reply
0 Kudos
48 Replies
LucD
Leadership
Leadership
Jump to solution

That is a possible solution if you experience large drift.

But you could also monitor the drift and send a 'ntpd -q' when the drift exceeds a threshold


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

Reply
0 Kudos
vmvn01
Contributor
Contributor
Jump to solution

Hi Luc,

I'm getting an error with get-view command as below, have or don't have "ExtensionData". We're using ESXi5 and running from powerCLI 4.1. Pls help me.

Get-View : Cannot validate argument on parameter 'Id'. The argument cannot be null or empty.
At line:1 char:19
+ $dts=Get-View <<<<  $esx.ExtensionData.ConfigManager.dateTimeSystem
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

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

Get-View : Cannot validate argument on parameter 'Id'. The argument cannot be null or empty.
At line:1 char:19
+ $dts=Get-View <<<<  $esx.ConfigManager.dateTimeSystem
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should consider upgarding your PowerCLI version if possible.

If not possible, you will have to do this in 2 steps

$esxObj = Get-View $esx

$dts = Get-View $esxObj.ConfigManager.dateTimeSystem


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

Reply
0 Kudos
vmvn01
Contributor
Contributor
Jump to solution

Hi Luc,

Thanks for your recommended to upgrade PowerCLI version. Now I am using powerCLI 5.0 and already passed the command: "$dts = Get-View $esx.Extensiondata.ConfigManager.dateTimeSystem".

But another issue has appeared with the command: "$dts.UpdateDateTime(Get-Date)". Error as below:

Missing ')' in method call.
At line:1 char:26
+ $dts.UpdateDateTime( <<<< get-date)
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

$dtSystem = Get-View $esx.Extensiondata.ConfigManager.dateTimeSystem 
$dtSystem
.UpdateDateTime((Get-Date))

With the double parenthesis we force Get-Date to execute first


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

Reply
0 Kudos
vmvn01
Contributor
Contributor
Jump to solution

Woa, that's running! So all that we needed!

Thank you very much, Smiley Happy

Reply
0 Kudos
rlboyd
Contributor
Contributor
Jump to solution

I found a couple of cases where $dts = get-view $_.ConfigManager.DateTimeSystem needs to be refined.   It can occasionally return more than 1 value.

For these cases I modified the line after that to read:

$t = $dts[0].QueryDateTime()


This makes sure that the subtraction operation only has a single value to work on.


Here is the full copy of the version I'm using:


Function CheckHostDateTime ( $testHost ) {

    #get host datetime system

    $dts = get-view $testHost.ExtensionData.configManager.DateTimeSystem

   

    #get host time (sometimes DateTimeSystem returns more than 1 value.  Only use the 1st one)

    $t = $dts[0].QueryDateTime()

  Write-Host "Time on " $testHost $t "(UTC)"

   

    #calculate time difference in seconds

    Try { $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 = $testHost

        $row.Seconds = $s

       Write-Host "Time on" $testHost "outside allowed range"

        $row

      }

   else{

        Write-Host "Time on" $testHost "within allowed range"

      }

  }

  Catch { Debug-Output 0 "`$testHost: $testHost"

  Write-Debug "`$t: $t "

  $dts}

}

Function Debug-Output 

  {

   Param(

  [Parameter(Mandatory=$True,Position=1)]

  [ValidateRange(0,10)][int]$Level,

  [Parameter(Mandatory=$True,Position=2)]

  [string]$Message

  )

#

# Print Debug output

#

  if ( $DebugFlag ) { if ($Level -le $DebugLevel ) {

  if ( ! $DebugBG ) {

  $a = (Get-Host).PrivateData

  $DebugFG = $a.DebugForegroundColor

  $DebugBG = $a.DebugBackgroundColor

  }

  Write-Host -ForegroundColor $DebugFG -BackgroundColor $DebugBG "Debug L${Level}($DebugLevel) $Message"

  # -WarningAction Continue -ErrorAction Continue

  $WhereFrom = $MyInvocation.ScriptName

  $WhereAt = $Myinvocation.ScriptLineNumber

  Write-Host -ForegroundColor $DebugFG -BackgroundColor $DebugBG "From: $WhereFrom Line: $WhereAt "

  }

  }

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Aren't you getting multiple values returned because you are running in "Multiple" mode (see the Set-PowerCLIConfiguration cmdlet) ?

Instead of doing $dts[0], it would perhaps be better to select 1 connection (and use the Server parameter).


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

Reply
0 Kudos
rlboyd
Contributor
Contributor
Jump to solution

Luc you are correct. I am using this in a context checking NTP settings across multiple datacenters and clusters.  Will have to try and see if that will still work with multiple mode off.

Reply
0 Kudos