我可以使用WMI VB脚本创建子进程吗?

时间:2022-01-13 03:21:07

Using WMI VB scripting, I would like to create/attach multiple child processes to a parent process, such as the explorer process.

使用WMI VB脚本,我想创建/附加多个子进程到父进程,例如资源管理器进程。

When an app is started by clicking on it, it becomes a child process of the explorer process. The same is true for all apps that are loaded when Windows starts up.

通过单击启动应用程序时,它将成为资源管理器进程的子进程。对于Windows启动时加载的所有应用程序也是如此。

If you kill the explorer process using the "End Process Tree" context menu option in the task manager, it kills all child processes of the explorer process as well (a quick, brute force way to clean up memory without restarting).

如果使用任务管理器中的“结束进程树”上下文菜单选项终止资源管理器进程,它也会杀死资源管理器进程的所有子进程(一种快速,强力的方式来清理内存而不重新启动)。

I have two scripts - one that kills a bunch of specific processes, and another that restarts those processes.

我有两个脚本 - 一个可以杀死一堆特定进程,另一个可以重启这些进程。

Most of the processes/apps in my scripts are loaded at start-up thus they are children of the explorer process. When I kill the explorer process tree, all these process die, as explained earlier.

我的脚本中的大多数进程/应用程序都是在启动时加载的,因此它们是资源管理器进程的子代。当我杀死资源管理器进程树时,所有这些进程都会死掉,如前所述。

When I restart these apps using a script, they are no longer children of the explorer process. When I kill the kill the explorer process tree, the apps started by the script do not die.

当我使用脚本重新启动这些应用程序时,它们不再是资源管理器进程的子代。当我杀死浏览器进程树时,脚本启动的应用程序不会死。

Now, I know I can kill each process individually using a script. But it would be nice to just kill the explorer processes tree in a script without having to specify the individual apps I want to kill.

现在,我知道我可以使用脚本单独杀死每个进程。但是,只需在脚本中杀死资源管理器进程树而不必指定我想要杀死的各个应用程序就好了。

So, if I have one script that can start my apps as children of the explorer process, my other script just has to kill the explorer processes tree.

因此,如果我有一个脚本可以启动我的应用程序作为资源管理器进程的子项,我的其他脚本只需要杀死资源管理器进程树。

I have a script that does just that. It loops through and kills all the child processes of the explorer process. However it only works on apps that load at start up or are are clicked on.

我有一个脚本可以做到这一点。它遍历并杀死资源管理器进程的所有子进程。但是它仅适用于在启动时加载或被单击的应用程序。

Also, by preventing these apps from loading at start-up, Windows loads MUCH faster. Later, I click on my script icon to load my apps when needed.

此外,通过阻止这些应用程序在启动时加载,Windows加载速度更快。稍后,我点击我的脚本图标以在需要时加载我的应用程序。

That's why I want to create a script that can start apps as children of the explorer process.

这就是为什么我想创建一个脚本,可以启动应用程序作为资源管理器进程的子级。

An interesting side note: I have to postpone killing any command/console processes, otherwise the script may kill itself before getting the rest of the processes.

一个有趣的旁注:我必须推迟杀死任何命令/控制台进程,否则脚本可能会在获得其余进程之前自行终止。

Any ideas how this can be done?

有什么想法可以做到这一点?

Below is my code that fails.

以下是我失败的代码。

Option Explicit
dim wmi, rootProcessName, rootProcess, objStartup, objConfig, objProcess, strComputer, dropbox, itunes, skype
strComputer = "."

dropbox="C:\Program Files\Dropbox\Dropbox.exe"
itunes="C:\Program Files\iTunes\iTunes.exe"
skype="C:\Program Files\Skype\Phone\Skype.exe"

Const NORMAL = 32
Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objStartup =  wmi.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.PriorityClass = NORMAL

rootProcessName = "'explorer.exe'"
set rootProcess = wmi.ExecQuery("Select * from Win32_Process Where Name = " & rootProcessName )
For Each objProcess in rootProcess
    objProcess.Create dropbox, null, objConfig
    objProcess.Create itunes, null, objConfig
    objProcess.Create skype, null, objConfig
Next

WScript.Quit

1 个解决方案

#1


1  

A window's process keeps track of the Process ID of who created it, this is how the relationships are being managed. To get what you want, you either have to change the parent PID stored in the child process, or inject code into the process you want to be the parent and have it create the new child process. Neither of these are really doable.

窗口的进程会跟踪创建它的进程ID,这就是关系的管理方式。要获得所需的内容,您必须更改存储在子进程中的父PID,或者将代码注入您希望成为父进程的进程并让它创建新的子进程。这些都不是真的可行。

The real solution is to use Job Objects, that way you can terminate all the processes which are associated with the job all at the same time. But you would have to migrate out of vbscript.

真正的解决方案是使用作业对象,这样您就可以同时终止与作业关联的所有进程。但你必须从vbscript迁移出来。

#1


1  

A window's process keeps track of the Process ID of who created it, this is how the relationships are being managed. To get what you want, you either have to change the parent PID stored in the child process, or inject code into the process you want to be the parent and have it create the new child process. Neither of these are really doable.

窗口的进程会跟踪创建它的进程ID,这就是关系的管理方式。要获得所需的内容,您必须更改存储在子进程中的父PID,或者将代码注入您希望成为父进程的进程并让它创建新的子进程。这些都不是真的可行。

The real solution is to use Job Objects, that way you can terminate all the processes which are associated with the job all at the same time. But you would have to migrate out of vbscript.

真正的解决方案是使用作业对象,这样您就可以同时终止与作业关联的所有进程。但你必须从vbscript迁移出来。