标签:Windows日志,系统日志,win32日志,windows event 参数,日志参数,任务计划传递日志参数,任务计划参数,计划任务参数,日志任务计划,Windows日志参数,EventLog参数,EventLog params。
最近搞了一点Windows上的东西,原理就是当产生指定类型的日志时就执行我的程序,因此给日志附加了任务计划。但是需要把日志内的相关内容传递给我的程序,正常情况下通过事件查看器添加任务,只能这么传递参数,怎么拿日志里的东西,完全没有说明。
百度吧,找了好久,没有任何有用的信息,也不知道是大佬们都不搞Windows程序了还是百度太不靠谱了。。。换google,第一条就是:
https://michlstechblog.info/blog/windows-passing-parameters-to-event-triggered-schedule-tasks/
大概意思就是说:
先创建一个任务计划,然后导出xml:
[D:\]schtasks /query /TN "Event Viewer Tasks\EventLog-Action-Drop-Packets-5152" /XML > C:\temp\Export-EventLog-Action-Drop-Packets-5152.xml
打开这个xml后应该看起来像这样:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2013-12-18T19:06:56.6561506</Date>
<Author>PC\localadm</Author>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="Security"><Select Path="Security">*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and EventID=5152]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<UserId>PC\localadm</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-Command D:\Event5152.ps1</Arguments>
</Exec>
</Actions>
</Task>
然后在系统事件查看器中查看你关注的那个日志,选择“详细信息”->“xml视图”,
看起来应该像这样:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-Security-Auditing" Guid="{54849625-5478-4994-A5BA-3E3B0328C30D}" />
<EventID>5152</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>12809</Task>
<Opcode>0</Opcode>
<Keywords>0x8010000000000000</Keywords>
<TimeCreated SystemTime="2013-12-18T17:14:01.325613300Z" />
<EventRecordID>156715054</EventRecordID>
<Correlation />
<Execution ProcessID="4" ThreadID="92" />
<Channel>Security</Channel>
<Computer>PC.local</Computer>
<Security />
</System>
<EventData>
<Data Name="ProcessId">0</Data>
<Data Name="Application">-</Data>
<Data Name="Direction">%%14592</Data>
<Data Name="SourceAddress">192.168.1.100</Data>
<Data Name="SourcePort">45345</Data>
<Data Name="DestAddress">192.168.1.1</Data>
<Data Name="DestPort">9100</Data>
<Data Name="Protocol">17</Data>
<Data Name="FilterRTID">79299</Data>
<Data Name="LayerName">%%14597</Data>
<Data Name="LayerRTID">13</Data>
</EventData>
</Event>
这些所有内容,都可以当做参数传递给你的程序。
在你导出的任务的xml中,<EventTrigger>节中添加一个新的子节,名字为<ValueQueries>,例如:
<ValueQueries>
<Value name="TimeCreated">Event/System/TimeCreated/@SystemTime</Value>
<Value name="SourceAddress">Event/EventData/Data[@Name='SourceAddress']</Value>
<Value name="SourcePort">Event/EventData/Data[@Name='SourcePort']</Value>
<Value name="DestAddress">Event/EventData/Data[@Name='DestAddress']</Value>
<Value name="DestPort">Event/EventData/Data[@Name='DestPort']</Value>
</ValueQueries>
然后在<Arguments>中传递参数,例如:
<Arguments>-Command D:\Event5152.ps1 '$(SourceAddress)' '$(SourcePort)' '$(DestAddress)' '$(DestPort)' '$(DestPort)' '$(TimeCreated)'</Arguments>
然后完整的任务xml看起来应该是:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2013-12-18T19:06:56.6561506</Date>
<Author>PC\localadm</Author>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="Security"><Select Path="Security">*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and EventID=5152]]</Select></Query></QueryList></Subscription>
<ValueQueries>
<Value name="TimeCreated">Event/System/TimeCreated/@SystemTime</Value>
<Value name="SourceAddress">Event/EventData/Data[@Name='SourceAddress']</Value>
<Value name="SourcePort">Event/EventData/Data[@Name='SourcePort']</Value>
<Value name="DestAddress">Event/EventData/Data[@Name='DestAddress']</Value>
<Value name="DestPort">Event/EventData/Data[@Name='DestPort']</Value>
</ValueQueries>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<UserId>PC\localadm</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-Command D:\Event5152.ps1 '$(SourceAddress)' '$(SourcePort)' '$(DestAddress)' '$(DestPort)' '$(DestPort)' '$(TimeCreated)'</Arguments>
</Exec>
</Actions>
</Task>
然后删除原任务,重新导入新任务即可。
删除任务:
C:\>schtasks /Delete /TN "Event Viewer Tasks\EventLog-Action-Drop-Packets-5152"
WARNING: Are you sure you want to remove the task "Event Viewer Tasks\EventLog-A
ction-Drop-Packets-5152" (Y/N)? y
SUCCESS: The scheduled task "Event Viewer Tasks\EventLog-Action-Drop-Packets-5152" was successfully deleted.
导入新任务:
C:\>schtasks /Create /XML c:\TEMP\Export-EventLog-Action-Drop-Packets-5152.xml /TN "Event Viewer Tasks\EventLog-Action-Drop-Packets-5152"
原贴比较详细,但是不知道会不会网站有一天打不开了,所以记录一下,主要方法就是这样了,自行搭配组合即可。
原贴时间很久了,是13年的,国内现在竟然还没有这些资料,转过来不知道算不算弥补了空白。
不管怎样,希望可以帮到大家。