How should the CIM_DataFile file name be passed to an inline ActiveScriptEventConsumer VBScript for a __InstanceCreationEvent WMI subscription?

本秂侑毒 提交于 2019-12-12 01:24:37

问题


I need to automate moving a file when created from one directory and only the file that triggered the event...not every file in the directory.

I am trying to setup a WMI subscription using powershell and the ActiveScriptEventConsumer with an inline VBScript where I can pass the name of the file to the inline VBScript.

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

When I request the $evtConsumer.ScriptText the below is returned in the console

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

Not surprisingly, nothing happens when I create a file in the targeted directory.


回答1:


This sets up a WMI subscription using VBScript.

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

If you want your program to act service like. In Windows you use Task Scheduler, which you choose you or another user. Note if you configure it to run when you are not logged in it will be invisible to you when you are logged in. Windows has inbuilt security accounts for programs/services like this.

About Task Scheduler https://docs.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-start-page

About Service Accounts https://docs.microsoft.com/en-us/windows/desktop/services/service-user-accounts



来源:https://stackoverflow.com/questions/54225532/how-should-the-cim-datafile-file-name-be-passed-to-an-inline-activescripteventco

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!