首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell Get-Eventlog对象输出问题

Powershell Get-Eventlog对象输出问题
EN

Stack Overflow用户
提问于 2016-10-30 17:18:01
回答 2查看 825关注 0票数 1

在脚本中使用变量的输出作为输入有问题。

当我运行这个:

代码语言:javascript
复制
$listoflogs = Get-EventLog -List | Select "Log"


$listoflogss | % { Get-EventLog -LogName $_ | Where-Object {$_.EntryType -Match "Error"} }

我得到了以下错误,据我所知,它没有将对象作为输入处理。

代码语言:javascript
复制
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

然后,我修改了将对象转换为字符串的脚本,但仍然无法获得我想要的输出。

代码语言:javascript
复制
$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

我不知道我做错了什么,完成这项任务会更好吗?不过,我还是不介意理解将对象输出转换为脚本的可读字符串的概念。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-30 22:01:21

经过一些测试,我想我理解你想要实现的目标:

代码语言:javascript
复制
Get-EventLog -List | ForEach-Object {

    $logName = $_.Log

    Get-EventLog $logName |
        Where-Object { $_.EntryType -Match "Error" } |
        Select-Object *, @{ n = "LogName"; e = { $logName } }
}

Select-Object cmdlet允许动态创建一个属性来显示它(这里需要将日志名添加到显示的结果中)。

*意味着所有原始属性,nenameexpression的缩写,并描述我们想要的附加属性。这叫做计算性质

在您的原始代码中,-ExpandProperty提供了帮助,因为您没有指定您是在Log属性之后,而是将一组对象传输到Get-EventLog。但是,属性的Select也意味着从正在收集的对象中移除所有其他的对象。

您也可以这样抑制错误:

代码语言:javascript
复制
$listoflogss |
    ForEach-Object {
        Get-EventLog -LogName $_.Log |
        Where-Object { $_.EntryType -Match "Error" }
    }
票数 0
EN

Stack Overflow用户

发布于 2016-10-30 20:21:07

试试像这样的东西

代码语言:javascript
复制
  Get-EventLog -list | where {$_.Entries.Count -gt 0} | %{ $_.Entries } | where EntryType -EQ "Error"  
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40331593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档