我的短期目标是Microsoft-Windows-TerminalServices-LocalSessionManager
从名为的日志中收集事件 ID 40 和 42,Microsoft-Windows-TerminalServices-**LocalSessionManager/Operational
然后根据 Session/SessionID 对它们进行排序。虽然我可以尝试解析消息属性,但我正在尝试使用EventLogRecord的GetPropertyValues方法,该方法带有一个System.Diagnostics.Eventing.Reader.EventLogPropertySelector
参数。此外,对于事件 40 的消息,会话 ID 首先显示标记为“会话”,对于事件 41 的消息显示第二个标记为“会话 ID”。
姓名 | 成员类型 | 定义 |
---|---|---|
获取属性值 | 方法 | System.Collections.Generic.IList[System.Object] |
我看到在 2018 年,Peter-Core和Mathias R. Jessen都给出了如何做到这一点的答案。按照他们的示例,我查看了事件提供者的事件模板。
(Get-WinEvent -ListProvider Microsoft-Windows-TerminalServices-LocalSessionManager).Events|Where-object{@(40,42) -contains $_.ID}
Id : 40
Version : 0
LogLink : System.Diagnostics.Eventing.Reader.EventLogLink
Level : System.Diagnostics.Eventing.Reader.EventLevel
Opcode : System.Diagnostics.Eventing.Reader.EventOpcode
Task : System.Diagnostics.Eventing.Reader.EventTask
Keywords : {}
Template : <template xmlns="http://schemas.microsoft.com/win/2004/08/events">
<data name="Session" inType="win:UInt32" outType="xs:unsignedInt"/>
<data name="Reason" inType="win:UInt32" outType="xs:unsignedInt"/>
</template>
Description : Session %1 has been disconnected, reason code %2
Id : 42
Version : 0
LogLink : System.Diagnostics.Eventing.Reader.EventLogLink
Level : System.Diagnostics.Eventing.Reader.EventLevel
Opcode : System.Diagnostics.Eventing.Reader.EventOpcode
Task : System.Diagnostics.Eventing.Reader.EventTask
Keywords : {}
Template : <template xmlns="http://schemas.microsoft.com/win/2004/08/events">
<data name="User" inType="win:UnicodeString" outType="xs:string"/>
<data name="SessionID" inType="win:UInt32" outType="xs:unsignedInt"/>
</template>
Description : End session arbitration:
User: %1
Session ID: %2
作为测试,我创建了以下内容来尝试从事件 ID 42 中获取会话 ID:
$strComputerName='PMV-SC01'
<# For those wanting to build xmlFilters try something like the following as it allow you to create them more dynamically
$xmlFilterBase=[xml]'<QueryList><Query Id="0"></Query></QueryList>'
## in a loop ##
$xmlSelect=$xmlFilter.CreateElement("Select")
$xmlSelect.SetAttribute("Path","$Logname")
$xmlSelect.set_InnerText("*[System[$($ProviderName)TimeCreated[@SystemTime>='$StartTime' and @SystemTime<='$EndTime']]]")
$xmlFilter.QueryList.Query.AppendChild($xmlSelect)|Out-Null
$xmlSelect=$Null
## end loop ##
#>
#I used the filter below to simplify the testing.
$xmlFilter=@"
<QueryList><Query Id="0" Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational"><Select Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational">*[System[TimeCreated[@SystemTime>='2021-06-28T22:00:00.000Z' and @SystemTime<='2021-06-28T23:00:00.000Z']]]</Select></Query></QueryList>
"@
$WinEvents=Get-WinEvent -ComputerName $strComputerName -FilterXml $xmlFilter -Oldest|Where-object{$_.Id -eq 42}
$WinEvents.count
# output is 10
$SelectorStrings= [String[]]@(
'Event/EventData/Data[@name="SessionID"]'
)
$PropertySelector= [System.Diagnostics.Eventing.Reader.EventLogPropertySelector]::new($SelectorStrings)
$WinEvents|ForEach-Object{$TheSessionIds=@();$TheSessionIds+=$_.GetPropertyValues($PropertySelector)}
$TheSessionIds.count
#output is 1
# All of the follow also return nothing:
$WinEvents[1].GetPropertyValues($PropertySelector)
$WinEvents.GetPropertyValues($PropertySelector)
$1WinEvent=$WinEvents[0]
$1WinEvent.GetPropertyValues($PropertySelector)
没有错误信息。GetPropertyValues() 方法不返回任何内容。PowerShell 版本:5.1.14393.2457
视窗 | 版 | 版本 | 操作系统构建 |
---|---|---|---|
视窗 | 服务器 2016 标准 | 1607 | 14393.2457 |
我的长期目标是产生类似以下输出的东西。这篇文章的请求是帮助从GetPropertyValues方法中获取值。我也想在其他项目中使用这种方法。
会话ID | 用户 | 创建时间 | ID | 代码 |
---|---|---|---|---|
12 | MYDOMAIN\User.Name | 2021-06-28 07:35:54.4321 | 42 | |
12 | 2021-06-28 15:35:54.4321 | 40 | 0 | |
13 | MYDOMAIN\Other.User | 2021-06-28 11:12:44.1234 | 42 | |
13 | 2021-06-28 13:01:12.5678 | 40 | 0 |