在脚本中使用变量的输出作为输入有问题。
当我运行这个:
$listoflogs = Get-EventLog -List | Select "Log"
$listoflogss | % { Get-EventLog -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }我得到了以下错误,据我所知,它没有将对象作为输入处理。
Get-EventLog : The event log '@{Log=System}' on computer '.' does not exist. At line:5 char:31
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand
Get-EventLog : The event log '@{Log=ThinPrint Diagnostics}' on computer '.' does not exist. At line:5 char:31
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand
Get-EventLog : The event log '@{Log=Windows PowerShell}' on computer '.' does not exist. At line:5 char:31
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand然后,我修改了将对象转换为字符串的脚本,但仍然无法获得我想要的输出。
$listoflogs = Get-EventLog -List | Select "Log"
$listoflogss = ($listoflogs | Out-String)
$listoflogss | % { Get-EventLog -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }
Get-EventLog : Event log names must consist of printable characters and cannot contain \, *, ?, or space s At line:7 char:32
+ $listoflogss | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetEventLogCommand我不知道我做错了什么,完成这项任务会更好吗?不过,我还是不介意理解将对象输出转换为脚本的可读字符串的概念。
发布于 2016-10-30 22:01:21
经过一些测试,我想我理解你想要实现的目标:
Get-EventLog -List | ForEach-Object {
$logName = $_.Log
Get-EventLog $logName |
Where-Object { $_.EntryType -Match "Error" } |
Select-Object *, @{ n = "LogName"; e = { $logName } }
}Select-Object cmdlet允许动态创建一个属性来显示它(这里需要将日志名添加到显示的结果中)。
*意味着所有原始属性,n和e是name和expression的缩写,并描述我们想要的附加属性。这叫做计算性质。
在您的原始代码中,-ExpandProperty提供了帮助,因为您没有指定您是在Log属性之后,而是将一组对象传输到Get-EventLog。但是,属性的Select也意味着从正在收集的对象中移除所有其他的对象。
您也可以这样抑制错误:
$listoflogss |
ForEach-Object {
Get-EventLog -LogName $_.Log |
Where-Object { $_.EntryType -Match "Error" }
}发布于 2016-10-30 20:21:07
试试像这样的东西
Get-EventLog -list | where {$_.Entries.Count -gt 0} | %{ $_.Entries } | where EntryType -EQ "Error" https://stackoverflow.com/questions/40331593
复制相似问题