首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何订阅Windows文件修改事件

如何订阅Windows文件修改事件
EN

Stack Overflow用户
提问于 2016-06-10 14:05:59
回答 2查看 594关注 0票数 0

我正在尝试使用WQL订阅使用此查询修改特定文件的事件:

SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA "CIM_DataFile" AND TargetInstance.Drive="C:" AND TargetInstace.Path="\\test\\filewatching\\"

我可以成功地注册事件(见底部的输出),并可以看到它与我注册的脚本(使用者)绑定在一起。

但是,当我在C:\test\filewatching\中修改一个文件时,脚本不会运行。

下面是在同一个文件夹中注册文件创建事件的代码,和File工作

代码语言:javascript
运行
复制
#WQL

$query = @"
SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA "Cim_DirectoryContainsFile" AND TargetInstance.GroupComponent="Win32_Directory.Name='C:\\test\\filewatching'"
"@
$instanceFilter = ([WMICLASS]"\\$Computername\root\subscription:__EventFilter").CreateInstance()
$instanceFilter.QueryLanguage = 'WQL'
$instanceFilter.Query = $query
$instanceFilter.Name = 'EventFilterNameHere'
$instanceFilter.EventNameSpace = 'root/CIMV2'
$result = $instanceFilter.Put()

# Consumer

$script = 
@"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\test\filewatching\Log.log", 8, True)
objFile.WriteLine "New File Created"
objFile.Close
"@
$instanceConsumer = ([wmiclass]"\\$Computername\root\subscription:ActiveScriptEventConsumer").CreateInstance()
$instanceConsumer.Name = 'ConsumerNameHere'
$instanceConsumer.ScriptingEngine = 'VBScript'
$instanceConsumer.ScriptFilename = '' 
$instanceConsumer.ScriptText = $script
$instanceConsumer.Put()

# Binding

[object]$Filter = (Get-WMIObject -Computername $Computername -Namespace root\Subscription -Class __EventFilter | Sort Name)
[object]$Consumer = (Get-WMIObject -Computername $Computername -Namespace root\Subscription -Class __EventConsumer | Sort Name)

$instanceBinding = ([wmiclass]"\\$Computername\root\subscription:__FilterToConsumerBinding").CreateInstance()
$instanceBinding.Filter = $Filter
$instanceBinding.Consumer = $Consumer
$instanceBinding.Put()

所以我知道我的PowerShell没有什么问题。这使我认为我的查询一定是错的。

我尝试过对上面的InstanceModificationEvent查询做一些调整,但没有成功。(这些调整包括将C:更改为C或从TargetInstace.Path中删除\s等)。

我需要这个订阅是永久的,它不能重置,如果PC是打开或关闭-这是为什么我使用WQL。如果有人能提出另一种方法来达到同样的目的,或者我如何解决我的疑问,我将永远感激!

运行脚本后的输出:

代码语言:javascript
运行
复制
Path          : \\WIN7-IT3\root\subscription:ActiveScriptEventConsumer.Name="Co
                nsumerNameHere"
RelativePath  : ActiveScriptEventConsumer.Name="ConsumerNameHere"
Server        : WIN7-IT3
NamespacePath : root\subscription
ClassName     : ActiveScriptEventConsumer
IsClass       : False
IsInstance    : True
IsSingleton   : False

Path          : \\WIN7-IT3\root\subscription:__FilterToConsumerBinding.Consumer
                ="\\\\WIN7-IT3\\ROOT\\Subscription:ActiveScriptEventConsumer.Na
                me=\"ConsumerNameHere\"",Filter="\\\\WIN7-IT3\\ROOT\\Subscripti
                on:__EventFilter.Name=\"EventFilterNameHere\""
RelativePath  : __FilterToConsumerBinding.Consumer="\\\\WIN7-IT3\\ROOT\\Subscri
                ption:ActiveScriptEventConsumer.Name=\"ConsumerNameHere\"",Filt
                er="\\\\WIN7-IT3\\ROOT\\Subscription:__EventFilter.Name=\"Event
                FilterNameHere\""
Server        : WIN7-IT3
NamespacePath : root\subscription
ClassName     : __FilterToConsumerBinding
IsClass       : False
IsInstance    : True
IsSingleton   : False

查询WMI订阅:

代码语言:javascript
运行
复制
Get-WmiObject -Namespace root\Subscription -Class __Eventfilter
Get-WMIObject -Namespace root\Subscription -Class __EventConsumer
Get-WMIObject -Namespace root\Subscription -Class __FilterToConsumerBinding
Get-WMIObject -Namespace root\Subscription -Class ActiveScriptEventConsumer

查询后的输出:

代码语言:javascript
运行
复制
__GENUS          : 2
__CLASS          : __EventFilter
__SUPERCLASS     : __IndicationRelated
__DYNASTY        : __SystemClass
__RELPATH        : __EventFilter.Name="EventFilterNameHere"
__PROPERTY_COUNT : 6
__DERIVATION     : {__IndicationRelated, __SystemClass}
__SERVER         : WIN7-IT3
__NAMESPACE      : ROOT\Subscription
__PATH           : \\WIN7-IT3\ROOT\Subscription:__EventFilter.Name="EventFilterNameHere"
CreatorSID       : {1, 5, 0, 0...}
EventAccess      : 
EventNamespace   : root/CIMV2
Name             : EventFilterNameHere
Query            : SELECT * FROM __InstanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 
                   "CIM_DataFile" AND TargetInstance.Drive="C:" AND 
                   TargetInstace.Path="\\test\\filewatching\\"
QueryLanguage    : WQL
PSComputerName   : WIN7-IT3

__GENUS          : 2
__CLASS          : ActiveScriptEventConsumer
__SUPERCLASS     : __EventConsumer
__DYNASTY        : __SystemClass
__RELPATH        : ActiveScriptEventConsumer.Name="ConsumerNameHere"
__PROPERTY_COUNT : 8
__DERIVATION     : {__EventConsumer, __IndicationRelated, __SystemClass}
__SERVER         : WIN7-IT3
__NAMESPACE      : ROOT\Subscription
__PATH           : \\WIN7-IT3\ROOT\Subscription:ActiveScriptEventConsumer.Name="ConsumerNameHere"
CreatorSID       : {1, 5, 0, 0...}
KillTimeout      : 0
MachineName      : 
MaximumQueueSize : 
Name             : ConsumerNameHere
ScriptFilename   : 
ScriptingEngine  : VBScript
ScriptText       : Set objFSO = CreateObject("Scripting.FileSystemObject")
                   Set objFile = objFSO.OpenTextFile("c:\test\filewatching\Log.log", 8, True)
                   objFile.WriteLine "New File Created"
                   objFile.Close
PSComputerName   : WIN7-IT3

__GENUS                 : 2
__CLASS                 : __FilterToConsumerBinding
__SUPERCLASS            : __IndicationRelated
__DYNASTY               : __SystemClass
__RELPATH               : __FilterToConsumerBinding.Consumer="\\\\WIN7-IT3\\ROOT\\Subscription:ActiveS
                          criptEventConsumer.Name=\"ConsumerNameHere\"",Filter="\\\\WIN7-IT3\\ROOT\\Su
                          bscription:__EventFilter.Name=\"EventFilterNameHere\""
__PROPERTY_COUNT        : 7
__DERIVATION            : {__IndicationRelated, __SystemClass}
__SERVER                : WIN7-IT3
__NAMESPACE             : ROOT\Subscription
__PATH                  : \\WIN7-IT3\ROOT\Subscription:__FilterToConsumerBinding.Consumer="\\\\WIN7-IT
                          3\\ROOT\\Subscription:ActiveScriptEventConsumer.Name=\"ConsumerNameHere\"",F
                          ilter="\\\\WIN7-IT3\\ROOT\\Subscription:__EventFilter.Name=\"EventFilterName
                          Here\""
Consumer                : \\WIN7-IT3\ROOT\Subscription:ActiveScriptEventConsumer.Name="ConsumerNameHer
                          e"
CreatorSID              : {1, 5, 0, 0...}
DeliverSynchronously    : False
DeliveryQoS             : 
Filter                  : \\WIN7-IT3\ROOT\Subscription:__EventFilter.Name="EventFilterNameHere"
MaintainSecurityContext : False
SlowDownProviders       : False
PSComputerName          : WIN7-IT3

__GENUS          : 2
__CLASS          : ActiveScriptEventConsumer
__SUPERCLASS     : __EventConsumer
__DYNASTY        : __SystemClass
__RELPATH        : ActiveScriptEventConsumer.Name="ConsumerNameHere"
__PROPERTY_COUNT : 8
__DERIVATION     : {__EventConsumer, __IndicationRelated, __SystemClass}
__SERVER         : WIN7-IT3
__NAMESPACE      : ROOT\Subscription
__PATH           : \\WIN7-IT3\ROOT\Subscription:ActiveScriptEventConsumer.Name="ConsumerNameHere"
CreatorSID       : {1, 5, 0, 0...}
KillTimeout      : 0
MachineName      : 
MaximumQueueSize : 
Name             : ConsumerNameHere
ScriptFilename   : 
ScriptingEngine  : VBScript
ScriptText       : Set objFSO = CreateObject("Scripting.FileSystemObject")
                   Set objFile = objFSO.OpenTextFile("c:\test\filewatching\Log.log", 8, True)
                   objFile.WriteLine "New File Created"
                   objFile.Close
PSComputerName   : WIN7-IT3
EN

Stack Overflow用户

回答已采纳

发布于 2016-06-10 14:29:00

在发布这个问题后不久,我偶然发现了此页,从这个问题中我发现我需要的查询是:

SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Name='c:\\test\\filewatching\\tester.txt'

注意,与其他几个在线帖子相反,对我起作用的实际上是省略了PathDrive部分的TargetInstance,并且只包含了我想要查看的文件的完整路径和名称。

这为我工作,因为我将只看一个特定的文件。如果需要使用此方法查看多个文件,则需要注册多个订阅。

注册文件修改监视程序的完整代码:

代码语言:javascript
运行
复制
#WQL

$query = @"
SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Name='c:\\test\\filewatching\\tester.txt'
"@
$instanceFilter = ([WMICLASS]"\\$Computername\root\subscription:__EventFilter").CreateInstance()
$instanceFilter.QueryLanguage = 'WQL'
$instanceFilter.Query = $query
$instanceFilter.Name = 'EventFilterNameHere'
$instanceFilter.EventNameSpace = 'root/CIMV2'
$result = $instanceFilter.Put()

# Consumer

$script = 
@"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\test\filewatching\Log.log", 8, True)
objFile.WriteLine "New File Created"
objFile.Close
"@
$instanceConsumer = ([wmiclass]"\\$Computername\root\subscription:ActiveScriptEventConsumer").CreateInstance()
$instanceConsumer.Name = 'ConsumerNameHere'
$instanceConsumer.ScriptingEngine = 'VBScript'
$instanceConsumer.ScriptFilename = '' 
$instanceConsumer.ScriptText = $script
$instanceConsumer.Put()

# Binding

[object]$Filter = (Get-WMIObject -Computername $Computername -Namespace root\Subscription -Class __EventFilter | Sort Name)
[object]$Consumer = (Get-WMIObject -Computername $Computername -Namespace root\Subscription -Class __EventConsumer | Sort Name)

$instanceBinding = ([wmiclass]"\\$Computername\root\subscription:__FilterToConsumerBinding").CreateInstance()
$instanceBinding.Filter = $Filter
$instanceBinding.Consumer = $Consumer
$instanceBinding.Put()
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37750329

复制
相关文章

相似问题

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