【发布时间】:2016-08-02 03:05:27
【问题描述】:
我搜索了此处已有的答案,但没有找到任何我可以说明确回答我的问题的东西。我有一个脚本,它应该连接到由文本文件定义的多个服务器,并从 EventLog 报告详细信息。它还写出一个文件,将生成的文件作为附件通过电子邮件发送,并给我处理命令所花费的时间,这样我就可以监控整体性能并查看是否有任何变化随着时间的推移。这部分工作得很好。
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> </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
我已将此添加为计划任务。如果我在任务计划程序之外手动运行,我会得到预期的结果。
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.
但是,当我让它按计划运行时,我得到的结果减去最后 2 列(InstanceID 和消息)
MachineName Index TimeGenerated EntryType Source
----------- ----- ------------- --------- ------
SERVERNAME9999.fqdn 123456 3/10/2016 11:11:46 AM Error EventLo
g
作为计划任务,我使用所有服务器的管理员组中的帐户运行它,并且选中了“以最高权限运行”选项。为什么我的最后 2 列不见了?
【问题讨论】:
-
尝试使用
Select而不是Format-Table奇怪,如果你确实在运行相同的参数,但值得一试 -
一定和反序列化的数据有关。当它作为任务运行时,可能不会填充这些属性。
标签: powershell scheduled-tasks get-eventlog