Hi to all,
I need to check to ntp service for all hosts in a cluster the following:
If only one is not valid I need to exit from script and write-host which value is not correct.
Thanks to all
Regards
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks @LucD 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?
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD 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
}
If that works for you, it is a good script
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference