Powershell脚本在手动运行时提供不同的结果

Powershell脚本在手动运行时提供不同的结果,powershell,scheduled-tasks,get-eventlog,Powershell,Scheduled Tasks,Get Eventlog,我在这里搜索了所有的答案,但没有找到任何我能说的能肯定回答我问题的答案。我有一个脚本,它应该可以连接到由文本文件定义的多个服务器,并从事件日志报告详细信息。它还向文件中写入内容,将生成的文件作为附件发送电子邮件,并向我提供处理命令所需的时间,这样我就可以监控总体性能,并查看随着时间的推移是否有任何更改。那部分很好用 Write-Output ($Start = Get-Date) | Out-File -Append F:\PowerShell\UnExpectedShutdowns.txt $

我在这里搜索了所有的答案,但没有找到任何我能说的能肯定回答我问题的答案。我有一个脚本,它应该可以连接到由文本文件定义的多个服务器,并从事件日志报告详细信息。它还向文件中写入内容,将生成的文件作为附件发送电子邮件,并向我提供处理命令所需的时间,这样我就可以监控总体性能,并查看随着时间的推移是否有任何更改。那部分很好用

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
但是,当我让它按计划运行时,我得到的结果减去最后两列(InstanceID&Message)


作为计划任务,我使用所有服务器的管理员组中的帐户运行它,并且选中了“以最高权限运行”选项。为什么我的最后两列不见了?

就像恩卡斯科说的那样,在输出到文件时使用
选择
而不是
格式化表格
。这可能是由于使用了
-AutoSize
-Wrap
格式化表格
造成的,因为在另一个用户下作为计划任务运行时,我认为它无法正确计算控制台大小,并且它会根据需要截断列。除非您打算将结果直接输出到屏幕,否则不应使用
格式表

另外,在
获取事件日志
中使用
-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

如果您确实使用相同的参数运行相同的东西,但值得一试,请尝试使用
Select
而不是
Format Table
strange。必须与反序列化数据有关。当它作为任务运行时,可能没有填充这些属性。我现在正在获取所有输出。看起来这次发生的情况是,每次遇到条目时,都会将其写入文件,而不是等到所有结果都输入并将表写入文件。在这种格式中,数据不再是表格形式。有没有可能恢复这种格式?不导出文件,导出csv是否有效?我明天会试试看我的结果是否不同。如果是我,我会将你的开始时间和持续时间作为电子邮件正文的一部分,并将错误结果输出为CSV,并将其附加到电子邮件中。
MachineName                      Index TimeGenerated          EntryType Source 
-----------                      ----- -------------          --------- ------ 
SERVERNAME9999.fqdn             123456 3/10/2016 11:11:46 AM      Error EventLo
                                                                        g      
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