Hi can anybody help me to get this script to check on host ntp time and send an email of the results . at the moment it doesn't have any thing in the results as i think the time is correct on all the host but i would like to schedule this to run and email letting us know that all host are reporting the correct time
$allowedDifferenceSeconds = 20
$VCServerName = "xxxxxxxxx"
$VC = Connect-VIServer $VCServerName
$TimeStamp = Get-Date -format "yyyyMMdd-HH.mm"
$Csvfile = "c:\temp\$VCServerName-HostDateQuery.csv"
$HostDateQuery = @()
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
$HostDateQuery += $row
}
else{
$row = "Time on" $_.Name "within allowed range"
}
}
$HostDataQuery | Export-Csv $Csvfile -NoTypeInformation -UseCulture
# SMTP email details
$smtpServer = "xxxxxxxxxx"
$att = new-object Net.Mail.Attachment($Csvfile)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object ($smtpServer)
$msg.From = "xxxxxxxxxxxx"
$msg.To.Add("xxxxxxxxxxxxxx")
$msg.Subject = "HostTimeQuery" + $VCServerName
$msg.Body = "Attached is the Host Time query report" + $VCServerName + " which was created on " + $TimeStamp
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()