应如何将 CIM_DataFile 文件名传递给 __InstanceCreationEvent WMI 订阅的内联 ActiveScriptEventConsumer VBScript? [英] How should the CIM_DataFile file name be passed to an inline ActiveScriptEventConsumer VBScript for a __InstanceCreationEvent WMI subscription?
问题描述
当从一个目录创建文件时,我需要自动移动文件,并且只移动触发事件的文件......不是目录中的每个文件.
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.
我正在尝试使用 powershell 和 ActiveScriptEventConsumer 设置 WMI 订阅以及内联 VBScript,我可以在其中将文件名传递给内联 VBScript.
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"
当我请求 $evtConsumer.ScriptText 时,控制台会返回以下内容
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.
推荐答案
这将使用 VBScript 设置 WMI 订阅.
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
如果你想让你的程序像服务一样.在 Windows 中,您使用任务计划程序,您可以选择您或其他用户.请注意,如果您将它配置为在您未登录时运行,则在您登录时它将对您不可见.Windows 为此类程序/服务内置了安全帐户.
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.
关于任务计划程序 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
这篇关于应如何将 CIM_DataFile 文件名传递给 __InstanceCreationEvent WMI 订阅的内联 ActiveScriptEventConsumer VBScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!