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()
This will work and just outputs to the screen as expected but when I try to add it to an array it fails? not sure why
$allowedDifferenceSeconds = 20
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
}
else{
Write-Host "Time on" $_.Name "within allowed range"
}
}
sorry error I am getting is
At C:\Users\jsmith484\Documents\PowershellScripts\hostdatquery.ps1:29 char:26
+ $row = "Time on" $_.Name "within allowed range"
+ ~~
Unexpected token '$_' in expression or statement.
At C:\Users\jsmith484\Documents\PowershellScripts\hostdatquery.ps1:29 char:34
+ $row = "Time on" $_.Name "within allowed range"
+ ~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '"within allowed range"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
This worked
$VCServerName = "xxxxxx"
$VC = Connect-VIServer $VCServerName
$allowedDifferenceSeconds = 20
$body = $null
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
$body += ("On " + $_.Name + " the time difference is " + $s + " seconds`r" | Out-String)
}
else{
$body += ("Time on " + $_.Name + " within allowed range`r" | Out-String)
}
}
$smtpSrv = "xxxxxxx"
$from = "xxxxxxxxx"
$to = "xxxxxxxxxx"
$subject = "VM Host Time Report"
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$smtp = new-object Net.Mail.SMTPclient($smtpSrv)
$smtp.send($msg)