【问题标题】:Powershell script gives different results when run manually手动运行时,Powershell 脚本会给出不同的结果
【发布时间】: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>&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

我已将此添加为计划任务。如果我在任务计划程序之外手动运行,我会得到预期的结果。

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


【解决方案1】:

就像 nkasco 说的,输出到文件时使用Select 而不是Format-Table。这可能是由于Format-Table-AutoSize-Wrap 导致在另一个用户下作为计划任务运行时,我认为它不能正确计算控制台大小并且它会切断列以适应。除非您打算将结果直接输出到屏幕上,否则不应使用 Format-Table

另外,在您的Get-EventLog 中使用-After,这样每个服务器只返回所需的日期范围,而不是通过网络传递整个事件日志,然后强制 PowerShell 从那里过滤所有内容。

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

【讨论】:

  • 我现在得到了所有的输出。看起来这次发生的事情是,每次遇到一个条目时,都会将其写入文件,而不是等到所有结果都输入后才将表写入文件。在这种格式下,数据不再是表格。是否有可能恢复该格式? export-csv 可以代替输出文件吗?我明天要试试,看看我的结果是否不同。
  • 如果是我,我会将您的开始时间和持续时间作为电子邮件正文的一部分,并将错误结果输出为 CSV,并将其附加到电子邮件中。
猜你喜欢
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2018-02-10
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多