对于__InstanceCreationEvent WMI订阅,应如何将CIM_DataFile文件名传递给内联ActiveScriptEventConsumer VBScript?

时间:2019-01-16 21:18:39

标签: vbscript wmi

从一个目录创建文件时,我需要自动移动文件,而仅触发事件的文件...而不是目录中的每个文件。

我正在尝试使用Powershell和ActiveScriptEventConsumer以及内联VBScript设置WMI订阅,可以在其中将文件名传递给内联VBScript。

    PS> $evtConsumer.ScriptText = "WITH CreateObject(""Scripting.FileSystemObject"") 
    >> .MoveFile """ $EventArgs.NewEvent.Name """, ""[target path here]""
    >> END WITH"

当我请求$ evtConsumer.ScriptText时,控制台中将返回以下内容

    PS> $evtConsumer.ScriptText
    WITH CreateObject("Scripting.FileSystemObject")
    .MoveFile "", "[target path]"
    END WITH

毫不奇怪,当我在目标目录中创建文件时,什么也没发生。

1 个答案:

答案 0 :(得分:0)

这将使用VBScript设置WMI订阅。

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set MonitoredEvents = WMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""C:\\\\Scripts""'")
Do
    WMIPath = Split(MonitoredEvents.NextEvent.TargetInstance.PartComponent, "=")(1)
    FilePath = Replace(WMIPath, "\\", "\")
'   FSO.CopyFile  filepath, "C:\", vbtrue 
    wscript.echo filepath
Loop

如果您希望您的程序像服务一样运行。在Windows中,您使用“任务计划程序”,您可以选择您或其他用户。请注意,如果将其配置为在未登录时运行,则在登录时将对您不可见。Windows为此类程序/服务内置了安全帐户。

关于任务计划程序 https://docs.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-start-page

关于服务帐户 https://docs.microsoft.com/en-us/windows/desktop/services/service-user-accounts