首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PS Get-WinEvent抛出‘该句柄无效’

PS Get-WinEvent抛出‘该句柄无效’
EN

Stack Overflow用户
提问于 2016-03-02 23:04:09
回答 1查看 1K关注 0票数 0

我有一个主机名列表,我想从其中提取所有与AppLocker相关的事件日志,特别是那些具有级别警告和/或错误的事件日志。我精心设计了这个脚本:

代码语言:javascript
复制
$ComputersToCheck = Get-Content 'X:\ListWithTheNames.txt'
foreach($OneHost in $ComputersToCheck)
{
try
{
    $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EXE and DLL" -ComputerName $OneHost -Credential $CredentialFromUser
    foreach ($SingelEvent in $EventCollection)
    {
        if($SingelEvent.LevelDisplayName -ne "Information")
        {
            $pathtosaveto = 'SomeFileName.txt'
            $ResultString += $SingelEvent | Select Message,MachineName,UserId | Export-Csv -Path $pathtosaveto -Append                
            }
        }
    }
catch 
{
    //handling exceptions
 }    
}

这工作了一段时间,但过了一段时间后,我得到了一个错误:

代码语言:javascript
复制
Get-WinEvent : The remote procedure call failed
At X:\FileName.ps1:22 char:28
+         $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EX ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-WinEvent], EventLogException
+ FullyQualifiedErrorId : The remote procedure call failed,Microsoft.PowerShell.Commands.GetWinEventCommand

在脚本开始给出这样的错误之后:

代码语言:javascript
复制
Get-WinEvent : The handle is invalid
At X:\FileName.ps1:22 char:28
+         $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EX ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-WinEvent], EventLogException
+ FullyQualifiedErrorId : The handle is invalid,Microsoft.PowerShell.Commands.GetWinEventCommand

我的第一个想法是,它与脚本试图到达的主机相关,但列表中的下一个是与前一个相同的类型(Os,甚至相同的模型)。

我运行了该脚本3次,每次输出大小都不同(可能是因为不同的主机在线时有相同数量的日志)。该脚本应该针对700多个主机运行,其中需要一个特殊的帐户,我通过Get-Credential提示该帐户,将其存储在一个变量中并将Get-WinEvent作为参数传递给它。

老实说,我坚持这个问题,不是真的确定这是什么原因和为什么。

如果谁有一个想法,请与我分享:)

EN

回答 1

Stack Overflow用户

发布于 2016-03-03 06:53:45

尝试这个方法,尝试捕获对失败主机和空对象的引用。您可以编写接收到的异常,但我没有将其包含在本文中,以使failedhosts文件易于阅读。希望我是正确的,因为我没有一个真实的案例来测试。

代码语言:javascript
复制
$ComputersToCheck = Get-Content 'X:\ListWithTheNames.txt'
foreach($OneHost in $ComputersToCheck) {
try {
    $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EXE and DLL" -ComputerName $OneHost -Credential $CredentialFromUser -ErrorAction Stop

    if($EventCollection) { 
        foreach ($SingelEvent in $EventCollection) {
            if($SingelEvent.LevelDisplayName -ne "Information") {
                $pathtosaveto = 'SomeFileName.txt'
                $ResultString += $SingelEvent | Select Message,MachineName,UserId | Export-Csv -Path $pathtosaveto -Append                
                }
            }
        } else {
            Out-File -InputObject $($OneHost + " Empty Event Collection") -FilePath "C:\FailedHosts.txt" -Append -Encoding ascii 
        }
    } 
catch {
    Out-File -InputObject $($OneHost + " Failed Connection") -FilePath "C:\FailedHosts.txt" -Append -Encoding ascii 
 }    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35750885

复制
相关文章

相似问题

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