[英]How to subscribe to an event through Get-WinEvent in powershell?
我正在尝试侦听 windows 事件日志中的特定日志条目,这表示计算机已连接到互联网:
LogName: Microsoft-Windows-NetworkProfile/Operational
Event ID: 10000
我从现有的 SO question中找到了一些代码,并尝试根据我的目的对其进行调整。 问题是我似乎只能使用Get-WinEvent
而不是建议的(已弃用的) Get-EventLog
找到我的事件。
WinEvent
条目似乎不存在EntryWritten
事件,是否有其他事件我可以“订阅”以侦听我的事件的新条目? 还是我应该尝试其他方法?
$networklog = Get-WinEvent -LogName 'Microsoft-Windows-NetworkProfile/Operational'
Register-ObjectEvent -InputObject $networklog -SourceIdentifier NewEventLogEntry -EventName EntryWritten -Action {
$entry = $event.SourceEventArgs.Entry
if($entry.EventID -eq 10000) {
# check_url($url)
Write-Host "Connected"
}
else {
Write-Host "Something else"
}
}
我的最终目标是让一个简单的脚本连续运行,只要与特定 url 的连接可用,它就会调用 function。 任何其他实现这一目标的建议都将不胜感激,因为我觉得我之前没有接触过 powershell 。
对于提到的 cmdlet,您可以通过执行以下操作来获取信息。
经测试
(Get-CimInstance -ClassName Win32_OperatingSystem).Version
# Results
<#
10.0.19041
#>
这适用于获取信息
(Get-WinEvent -LogName 'Microsoft-Windows-NetworkProfile/Operational').Count
# Results
<#
2054
#>
Get-WinEvent -LogName 'Microsoft-Windows-NetworkProfile/Operational' |
Select-Object -First 3 |
Format-Table -AutoSize
# Results
<#
ProviderName: Microsoft-Windows-NetworkProfile
TimeCreated Id LevelDisplayName Message
----------- -- ---------------- -------
09-Jul-20 08:54:25 4004 Information Network State Change Fired...
09-Jul-20 08:54:22 4004 Information Network State Change Fired...
09-Jul-20 08:54:18 20002 Information NSI Set Category Result...
#>
您只能获取当前的属性,然后您必须在属性中询问文本
(Get-WinEvent -LogName 'Microsoft-Windows-NetworkProfile/Operational' |
Select-Object -First 1) |
Sort-Object -Property Name |
Get-Member
# Results
<#
TypeName: System.Diagnostics.Eventing.Reader.EventLogRecord
Name MemberType Definition
---- ---------- ----------
...
Message NoteProperty string Message=Network State Change Fired...
...
Id Property int Id {get;}
Keywords Property System.Nullable[long] Keywords {get;}
KeywordsDisplayNames Property System.Collections.Generic.IEnumerable[string] KeywordsDisplayNames {get;}
Level Property System.Nullable[byte] Level {get;}
LevelDisplayName Property string LevelDisplayName {get;}
LogName Property string LogName {get;}
MachineName Property string MachineName {get;}
...
ProcessId Property System.Nullable[int] ProcessId {get;}
Properties Property System.Collections.Generic.IList[System.Diagnostics.Eventing.Reader.EventProperty] Properties {get;}
...
ProviderName Property string ProviderName {get;}
...
RecordId Property System.Nullable[long] RecordId {get;}
RelatedActivityId Property System.Nullable[guid] RelatedActivityId {get;}
Task Property System.Nullable[int] Task {get;}
TaskDisplayName Property string TaskDisplayName {get;}
ThreadId Property System.Nullable[int] ThreadId {get;}
TimeCreated Property System.Nullable[datetime] TimeCreated {get;}
...
#>
事件日志将详细信息存储在 Message 属性中,您可以从那里 select 。
(Get-WinEvent -LogName 'Microsoft-Windows-NetworkProfile/Operational').Message |
Select-Object -First 1
# Results
<#
Network State Change Fired
New Internet Connection Profile: false
Connection Cost Changed: false
Domain Connectivity Level Changed: false
Network Connectivity Level Changed: false
Host Name Changed: true
Wwan Registration State Changed: false
Tethering Operational State Changed: false
Tethering Client Count Changed: false
#>
您可以使用 hash 表过滤您的 ID
Get-WinEvent -FilterHashTable @{
LogName = 'Microsoft-Windows-NetworkProfile/Operational'
ID = 10000
}
# Results
<#
TimeCreated Id LevelDisplayName Message
----------- -- ---------------- -------
09-Jul-20 08:54:18 10000 Information Network Connected...
09-Jul-20 08:54:14 10000 Information Network Connected...
09-Jul-20 08:54:08 10000 Information Network Connected...
...
#>
然后就是将该消息解析为您选择的格式的问题。 或者按原样询问整个消息
(Get-WinEvent -FilterHashTable @{
LogName = 'Microsoft-Windows-NetworkProfile/Operational'
ID = 10000
}).Message
或者
(Get-WinEvent -FilterHashTable @{
LogName = 'Microsoft-Windows-NetworkProfile/Operational'
ID = 10000
}) | Select-Object -ExpandProperty Message
也可以看看:
更新
正如 LeeDaily 指出的那样,在 TaskScheduler 中设置它可能会更好。 如果您在 EventViewer 中进行了直接过滤器配置,您会得到类似的东西。
<QueryList>
<Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational">
<Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[(Level=1 or Level=2 or Level=3 or Level=4 or Level=0 or Level=5) and (EventID=10000)]]</Select>
</Query>
</QueryList>
所以,你可以看到你可以原生返回的限制,然后具体说,如果你想要更多,你必须挖掘 Message Property 的值。 更深入地查看提供的链接中的 XML 信息,当你最终得到如下代码时,所有可能的 output。
# Collect the filtered events
$Events = Get-WinEvent -FilterHashTable @{
LogName = 'Microsoft-Windows-NetworkProfile/Operational'
ID = 10000
} -MaxEvents 1
# Parse out the event message data
ForEach ($Event in $Events)
{
# Convert the event to XML
$eventXML = [xml]$Event.ToXml()
# Iterate through each one of the XML message properties
For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++)
{
# Append these as object properties
$AddMemberSplat = @{
InputObject = $Event
MemberType = 'NoteProperty'
Force = $true
Name = $eventXML.Event.EventData.Data[$i].Name
Value = $eventXML.Event.EventData.Data[$i].'#text'
}
Add-Member @AddMemberSplat
}
}
# View the results
$Events | Select-Object * | Format-List
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.