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.
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