PowerCLI

 View Only
  • 1.  Script to check NTP status on all host and check sync

    Posted Mar 03, 2023 11:36 AM

    Hi to all,

    I need to check to ntp service for all hosts in a cluster the following:

    • Service Running
    • Startup Policy
    • NTP Servers
    • Time sync between hosts

    If only one is not valid I need to exit from script and write-host which value is not correct.

    Thanks to all

    Regards



  • 2.  RE: Script to check NTP status on all host and check sync

    Posted Mar 03, 2023 11:45 AM

    How can the code check if the correct NTP servers are used?
    Is that info provided in some way?

    Also, have a look at Solved: Re: Checking NTP/DNS Report - VMware Technology Network VMTN



  • 3.  RE: Script to check NTP status on all host and check sync

    Posted Mar 03, 2023 12:10 PM

    Thanks  now I need to create a conditions:

    If service running is not True, throw

    if DNS Server are different between hosts, throw

    if time is different between hots, throw

    How can I do this?

     



  • 4.  RE: Script to check NTP status on all host and check sync

    Posted Mar 03, 2023 02:14 PM

    You could do something like this.

    But note that comparing the DateTime from the different ESXi nodes will most probably always show that they are different.
    You can limit the time to seconds, and exclude the second fractions, but even then the time stamp will probably not be the same.

    $clusterName = 'MyCluster'
    
    $obj = Get-Cluster -Name $clusterName |
    Get-VMHost |
    ForEach-Object -Process {
      $ntp = Get-VMHostService -VMHost $_ | Where-Object { $_.Key -eq 'ntpd' }
      if (-not $ntp.Running) {
        Write-Error "NTP Service is running on $($_.Name)"
      }
      New-Object -TypeName PSObject -Property @{
        Name = $_.Name
        NTPServiceRunning = $ntp.Running
        DNSServers = $_.Extensiondata.Config.Network.DnsConfig.Address -join ' | '
        Time = (Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).QueryDateTime()
      }
    }
    if(($obj.DNSServers | Get-Unique).Count -ne 1){
      Write-Error "Not all ESXi nodes have the same DNS servers defined"
    }
    if (( $obj.Time.ForEach{ $_.ToString('HH:mm:ss') } | Get-Unique).Count -ne 1){
      Write-Error "Not all ESXi nodes have exactly the same time"
    }
    $obj
    


  • 5.  RE: Script to check NTP status on all host and check sync

    Posted Mar 21, 2023 03:44 PM

     I use the following to check:

    ##################################################################################CHECK STATUS NTP Hosts
    Write-Host "==============================================================="
    Write-Host "--------------- CHECK STATUS NTP Hosts ---------------"
    foreach ($esxiHost in $esxiHosts) {
        $hostntp = Get-VMHost $esxiHost | Select Name,
        @{N='NTPServiceRunning';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},
        @{N='StartupPolicy';E={(Get-VMHostService -VMHost $_  | Where-Object {$_.key -eq 'ntpd'}).Policy}},
        @{N='NTPServers';E={$_  | Get-VMHostNtpServer}},
        @{N='Time';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).QueryDateTime()}}
        $ntpServiceRunning = $hostntp.NtpServiceRunning
        $ntpstartuppolicy = $hostntp.StartupPolicy
        $ntpServers = $hostntp.NTPServers
        $hostTime = $hostntp.Time
        if ($ntpServiceRunning -ne 'True') {
            Write-Host "NTP service is not running on $($esxiHost.Name)."
            throw "NTP service is not running on $($esxiHost.Name)."
        }
        if ($ntpstartuppolicy -ne 'on') {
            Write-Host "NTP service is not running on $($esxiHost.Name)."
            throw "NTP service is not running on $($esxiHost.Name)."
        }
        if ($ntpServers.Count -eq 0) {
            Write-Host "No NTP servers configured for $($esxiHost.Name)."
            throw "No NTP servers configured for $($esxiHost.Name)."
        }
        $ntpTime = (Get-Date).ToUniversalTime()
        if ([Math]::Abs(($hostTime - $ntpTime).TotalSeconds) -gt 5) {
            Write-Host "Time on $($esxiHost.Name) is not synchronized with the NTP local server."
            throw "Time on $($esxiHost.Name) is not synchronized with the NTP local server."
        }
        Write-Host "Time on $($esxiHost.Name) is synchronized with the NTP local server" -ForegroundColor Green
    }


  • 6.  RE: Script to check NTP status on all host and check sync

    Posted Mar 21, 2023 05:14 PM

    If that works for you, it is a good script