You can use the netcat command on each ESXi node, but you should be able to start/stop SSH on each ESXi node.
This snippet uses the VICredentialStore to retrieve the credentials for each ESXi node, but you can replace that with any other method you use to store credentials.
The snippet requires the Posh-SSH module to be installed.
The snippet also allows testing against multiple NTP servers, each NTP server will have a column in the output.
$ntpServers = '192.168.1.11','192.168.1.12'
$cmdSub = @'
nc -w 1 -z $ntpServer 123
'@
Get-VMHost -Name esx1* -PipelineVariable esx |
ForEach-Object -Process {
$ssh = Get-VMHostService -VMHost $esx | where{$_.Key -eq 'TSM-SSH'}
if(-not $ssh.Running){
Start-VMHostService -HostService $ssh -Confirm:$false | Out-Null
}
$viCred = Get-VICredentialStoreItem -Host $esx.Name
$cred = New-Object -TypeName PSCredential -ArgumentList $viCRed.User,(ConvertTo-SecureString -String $viCred.Password -AsPlainText -Force)
$session = New-SSHSession -ComputerName $esx.Name -Credential $cred
$obj = [ordered]@{
VMHost = $esx.Name
}
foreach($ntpServer in $ntpServers){
$result = Invoke-SSHCommand -SSHSession $session -Command $ExecutionContext.InvokeCommand.ExpandString($cmdSub)
$obj.Add($ntpServer,($result.Output[0] -match 'Succeeded'))
}
Remove-SSHSession -SSHSession $session | Out-Null
New-Object -TypeName PSObject -Property $obj
if (-not $ssh.Running) {
Stop-VMHostService -HostService $ssh -Confirm:$false | Out-Null
}
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture