VMware Cloud Community
lorried82
Enthusiast
Enthusiast

ESXi Host NTP Info

I am looking to write a script to tell me the Host Name, NTP Server, Service Running (yes or no), Policy (start with host, manual, automatic) and the time on the server. Is this possible? I have the one liner below that is pulling half of what I am looking for - although the policy piece is not pulling and I need to get the time to display.

Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}},@{N=“Policy”;E={(Get-VmHostService -policy $_ |Where-Object {$_.key-eq “ntpd“})}}

Thanks

3 Replies
LucD
Leadership
Leadership

Try like this

Get-VMHost |Sort Name|

Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}},

    @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}},

    @{N=“Policy”;E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq “ntpd“} | Select -ExpandProperty Policy)}}


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

lorried82
Enthusiast
Enthusiast

This is great!

So I added to this to also pull the current time on the server as well

Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}},@{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}},@{Name="Time";Expression={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}},@{N=“Policy”;E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq “ntpd“} | Select -ExpandProperty Policy)}}

My question though is if I want to get this report e-mailed with the condition if the Service is not running or policy is off or time is not current - can I add that?


Thanks

0 Kudos
LucD
Leadership
Leadership

You can do something like this

$ntpService = Get-VMHost | Sort Name |

Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}},

    @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}},

    @{Name="Time";Expression={(get-view $_.ExtensionData.configManager.DateTimeSystem).QueryDateTime()}},

    @{N=“Policy”;E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq “ntpd“} | Select -ExpandProperty Policy)}}

$ntpError = $ntpService |

    where{$_.ServiceRunning -eq $false -or $_.Policy -ne 'automatic' -or

        ((Get-Date).AddSeconds(-5) -lt $_.Time -and $_.Time -lt (Get-Date).AddSeconds(5))}

Send-MailMessage -From me@domain.com -To you@domain.com -Subject 'NTP issues' -Body ($ntpError | Out-String) -SmtpServer mail.domain.com

But for deciding on the time off, you will eventually have to adapt the interval (I used + and - 5 seconds).

If your ESXi nodes are in different timezones you should be comparing UTC times.


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