1

I searched through the answers already on here, but didn't find anything I could say definitively answered my question. I have a script that should reach out to several servers as defined by a text file and report details from the EventLog. It also writes out to a file, emails the resulting file as an attachment, and gives me the amount of time it took to process the command, just so I can monitor the overall performance and see if there are any changes over time. That part works just fine.

Write-Output ($Start = Get-Date) | Out-File -Append F:\PowerShell\UnExpectedShutdowns.txt
$Computers = Get-Content -Path F:\PowerShell\servers.txt
Get-EventLog -logname System -ComputerName $Computers | ? {$_.TimeWritten -gt $lasttime -and $_.EventID -eq "6008"} | Format-Table -Wrap -Property MachineName, Index, TimeGenerated, EntryType, Source, InstanceID, Message -AutoSize | Out-File -Append -Force F:\PowerShell\UnExpectedShutdowns.txt
Write-Output (($Stop = Get-Date) - $Start).TotalSeconds | Out-File -Append F:\PowerShell\UnExpectedShutdowns.txt
Send-MailMessage -From sender@emaildomain.com -Subject "Daily Check for Server Shutdowns" -To receiver@emaildomain.com -Attachments F:\PowerShell\UnExpectedShutdowns.txt -Body "<p style=""font-family: Verdana, Geneva, sans-serif; font-size: 12px;"">Please review the attached document for new unexpected server shutdowns. </p><p>&nbsp;</p><p style=""font-family: Verdana, Geneva, sans-serif; font-size: 12px;"">The original file is located on <a href=""file:///\\SERVER\F$\Powershell\UnExpectedShutdowns.txt"">\\SERVER\F$\Powershell\UnExpectedShutdowns.txt</a></p><p style=""font-family: Verdana, Geneva, sans-serif; font-size: 12px;"">It should be pared down from time to time to maintain its size</p>" -BodyAsHtml -Priority Normal -SmtpServer smtp.dept.domain.com

I've added this as a scheduled task. If I Run outside of Task Scheduler manually, I get the results I expect.

MachineName                      Index TimeGenerated          EntryType Source   InstanceId Message                                                                     
-----------                      ----- -------------          --------- ------   ---------- -------                                                                     
SERVERNAME9999.fqdn             123456 3/10/2016 11:11:46 AM      Error EventLog 1234567890 The previous system shutdown at 11:08:17 AM on ‎3/‎10/‎2016 was unexpected. 

However, when I let it run on the schedule, I get results minus the last 2 columns (InstanceID & Message)

MachineName                      Index TimeGenerated          EntryType Source 
-----------                      ----- -------------          --------- ------ 
SERVERNAME9999.fqdn             123456 3/10/2016 11:11:46 AM      Error EventLo
                                                                        g      

As a scheduled task, I run it with an account that is in the administrator's group for all servers and I have the "Run with highest privileges" option checked. Why are my last 2 columns missing?

knowbody
  • 29
  • 3
  • Try using `Select` instead of `Format-Table` strange if you are indeed running the same thing with the same parameters but worth a shot – nkasco Apr 11 '16 at 19:47
  • Must have something to do with the deserialized data. Perhaps those properties are not populated when it is running as a task. – Matt Apr 11 '16 at 19:56

1 Answers1

1

Like nkasco said, use Select and not Format-Table when outputting to file. This is likely due to Format-Table with -AutoSize or -Wrap since while running as a scheduled task under another user I don't think it can calculate the console size correctly and it's cutting off columns to fit. You shouldn't use Format-Table unless you are intending to output the result directly to the screen.

Also, use -After in your Get-EventLog so that each server only returns the date range needed instead of passing the entire event log across the wire and then forcing PowerShell to filter everything from there.

Get-EventLog -logname System -ComputerName $Computers -After $lasttime | ? {$_.EventID -eq "6008"} | Select MachineName, Index, TimeGenerated, EntryType, Source, InstanceID, Message | Out-File -Append -Force F:\PowerShell\UnExpectedShutdowns.txt
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • I am getting all of the output now. It looks like what happened this time is that each time an entry was encountered, it was written to file rather than waiting until all results were in and writing a table to file. In this format, the data is no longer tabular though. Would it be possible to get that format back? Instead of out-file, would export-csv work? I'm going to try that tomorrow and see if my results are different. – knowbody Apr 11 '16 at 20:58
  • 1
    If it were me I would include your start time and duration as a part of the email body, and output the error results as a CSV, and attach that to the email. – TheMadTechnician Apr 11 '16 at 21:00