Working on a script, and trying to see why the Out-File
command is not working in the below code. It will create the file, but not write anything to it. Am I missing something?
###################### Set base variables ###############################################
$servers_cst = gc "D:\Server Times Check\cstlist.txt"
$servers_est = gc "D:\Server Times Check\estlist.txt"
$server_pst = gc "D:\Server Times Check\pstlist.txt"
$logfile = "D:\ServerTimeReport\logs\STR_$(get-date -Format "yyyyMMdd_hhmmss").txt"
$time_server_cst = 'server1'
$time_server_pst = 'server2'
$time_server_est = 'server3'
#########################################################################################
########################### Gather times at hosts and convert to UTC ######################################
$now_cst = Get-WmiObject -Class Win32_LocalTime -ComputerName $time_server_cst
$now_dt_cst = (Get-Date -Day $now_cst.Day -Month $now_cst.Month -Year $now_cst.Year -Minute $now_cst.Minute -Hour $now_cst.Hour -Second $now_cst.Second)
$NUTC_cst = $now_dt_cst.ToUniversalTime()
$now_pst = Get-WmiObject -Class Win32_LocalTime -ComputerName $time_server_pst
$now_dt_pst = (Get-Date -Day $now_pst.Day -Month $now_pst.Month -Year $now_pst.Year -Minute $now_pst.Minute -Hour $now_pst.Hour -Second $now_pst.Second)
$NUTC_pst = $now_dt_pst.ToUniversalTime()
$now_est = Get-WmiObject -Class Win32_LocalTime -ComputerName $time_server_est
$now_dt_est = (Get-Date -Day $now_est.Day -Month $now_est.Month -Year $now_est.Year -Minute $now_est.Minute -Hour $now_est.Hour -Second $now_est.Second)
$NUTC_est = $now_dt_est.ToUniversalTime()
############################################################################################################
$errors = @()
######################## Start foreach section #########################################
Write-Host "Server Time Report - $(get-date -Format "MM-dd-yyy")`n" | Out-File $logfile
foreach ($server in $servers_cst) {
$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $server
$DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
$SUTC = $DateTime.ToUniversalTime()
$Difference = (New-TimeSpan -Start ($NUTC_cst) -End ($SUTC))
Write-Host "Time at $server is $SUTC. Time difference of $Difference." | Out-File $logfile -Append
if ($Difference -ge '00:03:00') {
$errors += "Time difference of $Difference detected on $server compared to $time_server_cst."
}
}
foreach ($server in $servers_pst) {
$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $server
$DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
$SUTC = $DateTime.ToUniversalTime()
$Difference = (New-TimeSpan -Start ($NUTC_pst) -End ($SUTC))
Write-Host "Time at $server is $SUTC. Time difference of $Difference." | Out-File $logfile -Append
if ($Difference -ge '00:03:00') {
$errors += "Time difference of $Difference detected on $server compared to $time_server_pst."
}
}
foreach ($server in $servers_est) {
$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $server
$DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
$SUTC = $DateTime.ToUniversalTime()
$Difference = (New-TimeSpan -Start ($NUTC_est) -End ($SUTC))
Write-Host "Time at $server is $SUTC. Time difference of $Difference." | Out-File $logfile -Append
if ($Difference -ge '00:03:00') {
$errors += "Time difference of $Difference detected on $server compared to $time_server_est."
}
}
###################### End of foreach section ###############################################
if($errors.Count -gt 0){
Send-MailMessage -Subject "Server Time Report - $(get-date -Format "MM-dd-yyy")" -Body ($errors -join "`n") -SmtpServer "SMTPSERVER" -From "admin@firm.com" -To "user@firm.com" -UseSsl
}