hharold
Enthusiast
Enthusiast

PowerCLI: Detect incorrect Time Configuration

Jump to solution

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
1 Solution

Accepted Solutions
ykalchev
VMware Employee
VMware Employee

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, vSM Dev Team

View solution in original post

0 Kudos
48 Replies
nnedev
VMware Employee
VMware Employee

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
hharold
Enthusiast
Enthusiast

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

0 Kudos
ykalchev
VMware Employee
VMware Employee

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, vSM Dev Team

View solution in original post

0 Kudos
hharold
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

hharold
Enthusiast
Enthusiast

Thnaks Luc,

I knew there was some way to make it absolute, this solves all my questions on this topic.

Thanks guys !

0 Kudos
tomnelson
Contributor
Contributor

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

thanks for you help.

tomas!

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tomnelson
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tomnelson
Contributor
Contributor

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

tom.

0 Kudos
sushil_mudgil
Contributor
Contributor

HI ,

Apologies for bumping up a very old thread but is there any way i can run this script against a list of 200 EsxI server . say i enter the name of server in a simple text file, it read that text file and calls this script to give me the output . I just need to know what time the hosts are running . if there is a difference, it should just report that so tht i can correct it manually or if possible script be able to correct it.

I am currently runnig windows powershell ver 2.0 & running Vsphere 5 . Not sure if i can use the same for running .Ps1 ext scripts..

0 Kudos
LucD
Leadership
Leadership

You will need to have the PowerCLI snapin installed to run the script.

If I understand you correctly, you want to retrieve the names of the ESXi servers to check from a text file.

Is that correct ?


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

0 Kudos
sushil_mudgil
Contributor
Contributor

Yes, that;s Correct. Idea is to feed the name of EsxI server from a text file and Let the script get me time on the host. If required, it should synch a pre defined time on All esxI hosts .

Say ..i am running correct time on my Windows server from where i am running the script. I am trying to synch the time of all esxI host with this correct time . In the end, i want all my esxI hosts running correct time .

Apologies in advance for nit picking but i am new to Scripting & don ;t have even a slight idea fo scripts...

0 Kudos
LucD
Leadership
Leadership

Try something like this

$allowedDifferenceSeconds = 20 
$esxnames
=  [string]::Join('|',(Get-Content -Path C:\vmhosts.txt | %{"^" + $_}))   get-view -ViewType HostSystem -Property Name, ConfigManager.DateTimeSystem -Filter @{"Name"=$esxnames} | %{     #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 vmhosts.txt file contains a hostname per line.


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

0 Kudos
sushil_mudgil
Contributor
Contributor

Thanks Lucd .

I will test Out the script and feedback.

Just a curious question : - the script is not asking for any authentication, so i beleive it will use logged in user credentials to connect to Esx hosts .

0 Kudos
sushil_mudgil
Contributor
Contributor

tested the script . I am getting below error ..

PS D:\sushil> .\time.ps1

The term 'get-view' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp

elling of the name, or if a path was included, verify that the path is correct and try again.

At D:\sushil\time.ps1:4 char:9

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

    + CategoryInfo          : ObjectNotFound: (get-view:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Not sure where it went wrong...

0 Kudos
LucD
Leadership
Leadership

Do you have the PowerCLI snapin installed ?

Get-PSSnapin | Select Name,Version

Do you see an entry that says VMware.VimAutomation.Core ?


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

0 Kudos
sushil_mudgil
Contributor
Contributor

No Below is the output...

PS D:\> Get-PSSnapin | Select name,version

Name                                                        Version
----                                                        -------
Microsoft.PowerShell.Diagnostics                            1.0.0.0
Microsoft.WSMan.Management                                  1.0.0.0
Microsoft.PowerShell.Core                                   1.0.0.0
Microsoft.PowerShell.Utility                                1.0.0.0
Microsoft.PowerShell.Host                                   1.0.0.0
Microsoft.PowerShell.Management                             1.0.0.0
Microsoft.PowerShell.Security                               1.0.0.0


PS D:\>

Edit : - enabled the Plug in and I get a error "As unable to connect to Server" which is correct as i didn;t connect to VC server.

0 Kudos