【问题标题】:PS Get-WinEvent throw 'The Handle is invalid'PS Get-WinEvent 抛出“句柄无效”
【发布时间】:2016-06-15 12:32:01
【问题描述】:

我有一个主机名列表,我想从中提取所有与 AppLocker 相关的事件日志,尤其是带有警告和/或错误级别的事件日志。 我制作了这个脚本:

$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
 }    
}

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

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

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

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 作为参数传递给它。

说实话,我一直在纠结这个问题,不确定是什么原因造成的。

如果有人有想法,请与我分享:)

【问题讨论】:

    标签: powershell applocker get-winevent


    【解决方案1】:

    尝试捕获对失败主机和空对象的引用。您可以编写收到的异常,但我没有将其包含在其中以使 failedhosts 文件易于阅读。希望我做对了,因为我没有正确地测试它。

    $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 
     }    
    }
    

    【讨论】:

    • 嗨,很遗憾,它没有用。但是现在我有一个漂亮的离线主机列表。就是这样:) 尽管如此,问题仍然存在。在我看来,每次生成不同数量的日志时都很随机,但也许它不相关。目前我是盲人,没有更多的想法。
    • 它是否抛出错误或以何种方式不起作用?我猜当您说 FailedHosts.txt 文件填充的离线主机的漂亮列表时?我只能假设它不是您在 ListWithTheNames.txt 文件中运行的计算机的包含性列表。我会在 FailedList 中的一些机器上手动尝试Get-WinEvent 查询,以查看响应是什么,我们可以围绕结果的变化包装一些逻辑。
    • 前几个是:Get-WinEvent : The interface is unknown At A:\AppLocker_ExtractBlockedExes (2).ps1:9 char:28 + $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EX ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-WinEvent], EventLogException + FullyQualifiedErrorId : The interface is unknown,Microsoft.PowerShell.Commands.GetWinEventCommand
    • 剩下的99%都是:Get-WinEvent : The handle is invalid At A:\AppLocker_ExtractBlockedExes (2).ps1:9 char:28 + $EventCollection = Get-WinEvent -LogName "Microsoft-Windows-AppLocker/EX ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-WinEvent], EventLogException + FullyQualifiedErrorId : The handle is invalid,Microsoft.PowerShell.Commands.GetWinEventCommand
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多