Skip navigation
VMware

This Question is Answered (go to answer)

1,748 Views 11 Replies Last post: Apr 8, 2011 10:22 AM by tomnelson RSS
hharold Hot Shot 195 posts since
Oct 18, 2004
Currently Being Moderated

Sep 7, 2010 6:14 AM

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

 

Thanks for your help.

 

Regards,

Harold

nnedev Hot Shot VMware Employees 82 posts since
Mar 5, 2008
Currently Being Moderated
1. Sep 7, 2010 7:44 AM in response to: hharold
Re: PowerCLI: Detect incorrect Time Configuration

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

Regards, Nedko Nedev PowerCLI Development Team
ykalchev Expert VMware Employees 203 posts since
Mar 5, 2008
Currently Being Moderated
3. Sep 7, 2010 8:38 AM in response to: hharold
Re: PowerCLI: Detect incorrect Time Configuration

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

Yasen Kalchev, PowerCLI Dev Team
LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
5. Sep 7, 2010 9:35 AM in response to: hharold
Re: PowerCLI: Detect incorrect Time Configuration

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

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
tomnelson Lurker 5 posts since
Aug 1, 2006
Currently Being Moderated
7. Apr 7, 2011 5:34 PM in response to: hharold
Re: PowerCLI: Detect incorrect Time Configuration

Someone could tell me how can I run this script I have VMware 4.1 Update 1 vSphere PowerCLI

 

thanks for you help.

tomas!

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
8. Apr 7, 2011 6:05 PM in response to: tomnelson
Re: PowerCLI: Detect incorrect Time Configuration

Have you installed the latest PowerrCLI build on a Windows box ?

If yes,

  • save the script in a file with a .ps1 extension
  • start the PowerCLI prompt
  • check that the execution policy is set to remotesigned.
    • execute Get-ExecutionPolicy from the prompt
    • if not set correctly, execute Set-ExecutionPolicy RemoteSigned
  • connect to the vSphere server with Connect-VIServer -Server <servername>
  • run the script by typing .\<name-of-the-script>.ps1 at the prompt
Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
tomnelson Lurker 5 posts since
Aug 1, 2006
Currently Being Moderated
9. Apr 8, 2011 8:37 AM in response to: LucD
Re: PowerCLI: Detect incorrect Time Configuration

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.!!

LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
10. Apr 8, 2011 9:13 AM in response to: tomnelson
Re: PowerCLI: Detect incorrect Time Configuration

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"
    }
}
Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
tomnelson Lurker 5 posts since
Aug 1, 2006
Currently Being Moderated
11. Apr 8, 2011 10:22 AM in response to: LucD
Re: PowerCLI: Detect incorrect Time Configuration

Thanks a lot; this version is very helpful to me,  thank you very much.

tom.

Bookmarked By (0)

Share This Page

Communities