1 2 3 4 Previous Next 48 Replies Latest reply on Jul 8, 2013 10:43 PM by rlboyd

    PowerCLI: Detect incorrect Time Configuration

    hharold Hot Shot
    vExpert

      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

        • 1. Re: PowerCLI: Detect incorrect Time Configuration
          nnedev Hot Shot
          VMware Employees

          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

          1 person found this helpful
          • 2. Re: PowerCLI: Detect incorrect Time Configuration
            hharold Hot Shot
            vExpert

            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

            • 3. Re: PowerCLI: Detect incorrect Time Configuration
              ykalchev Expert
              VMware Employees

              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

              • 4. Re: PowerCLI: Detect incorrect Time Configuration
                hharold Hot Shot
                vExpert

                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

                • 5. Re: PowerCLI: Detect incorrect Time Configuration
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  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

                  1 person found this helpful
                  • 6. Re: PowerCLI: Detect incorrect Time Configuration
                    hharold Hot Shot
                    vExpert

                    Thnaks Luc,

                     

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

                     

                    Thanks guys !

                    • 7. Re: PowerCLI: Detect incorrect Time Configuration
                      tomnelson Novice

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

                       

                      thanks for you help.

                      tomas!

                      • 8. Re: PowerCLI: Detect incorrect Time Configuration
                        LucD Guru
                        vExpertCommunity WarriorsUser Moderators

                        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
                        • 9. Re: PowerCLI: Detect incorrect Time Configuration
                          tomnelson Novice

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

                          • 10. Re: PowerCLI: Detect incorrect Time Configuration
                            LucD Guru
                            vExpertCommunity WarriorsUser Moderators

                            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"
                                }
                            }
                            • 11. Re: PowerCLI: Detect incorrect Time Configuration
                              tomnelson Novice

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

                              tom.

                              • 12. Re: PowerCLI: Detect incorrect Time Configuration
                                sushil.mudgil Novice

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

                                • 13. Re: PowerCLI: Detect incorrect Time Configuration
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  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 ?

                                  • 14. Re: PowerCLI: Detect incorrect Time Configuration
                                    sushil.mudgil Novice

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

                                    1 2 3 4 Previous Next