0

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
    }
Matt
  • 45,022
  • 8
  • 78
  • 119
WR7500
  • 417
  • 1
  • 3
  • 12
  • Does [Which should I use: "write-host", "write-output", or "writeline"?](http://stackoverflow.com/q/8755497/62576) help? – Ken White Jun 10 '15 at 20:57

1 Answers1

1

You are using Out-File piped to Write-Host but Write-Host cmdlet doesn't pass anything down the pipeline. You can check it by a simple example

Write-Host "Something" | Write-Host

"Something" will be output only once.

You should avoid using Write-Host in your scripts, that's a rule of thumb, that's what Don Jones says all the time. See here what you'd better replace the Write-Host with.

To correct your script you can simply omit the Write-Host, like this

"Time at $server is $SUTC. Time difference of $Difference." | Out-File $logfile -Append
Varvara Kalinina
  • 2,043
  • 19
  • 29
  • So should I just replace `Write-Host` with `Out-File`? I don't mind loosing the display of the text, so long as it writes to the file. – WR7500 Jun 10 '15 at 20:52
  • 3
    @user3492006 You probably want to use `Tee-Object`. This allows you to both write to the console and write to a text file. – David Jun 10 '15 at 20:56
  • @David Using `Tee-Object` is a good idea for a simple case like this one, but `Out-File` cmdlet supports additional property that might be helpful - `-Encoding` while `Tee-Object` always uses Unicode – Varvara Kalinina Jun 10 '15 at 21:02
  • Just create a function that would do what you want – n0rd Jun 10 '15 at 23:05