可以通过 WMI 监视另一个进程启动的进程吗?

发布于 2024-07-05 07:09:29 字数 296 浏览 7 评论 0原文

我有一个需要安装的安装可执行文件。 当我运行它时,它会启动一个 msi 来执行实际安装,然后立即终止。 这样做的副作用是,在安装完成之前,它会将控制权返回给您调用它的任何控制台。 根据我运行它的机器,它可能需要三到十分钟,因此让调用脚本休眠是不受欢迎的。 我会直接启动 msi,但它抱怨缺少组件。

我有一个 WSH 脚本,它使用 WMI 启动一个进程,然后监视直到它的 pid 不再运行。 有没有某种方法可以确定初始可执行文件正在执行的 MSI 的 pid,然后使用 WMI 观察该 pid 是否结束? 启动进程信息是否与进程相关联?

I have a setup executable that I need to install. When I run it, it launches a msi to do the actual install and then dies immediately. The side effect of this is it will return control back to any console you call it from before the install finishes. Depending on what machine I run it on, it can take from three to ten minutes so having the calling script sleep is undesirable. I would launch the msi directly but it complains about missing components.

I have a WSH script that uses WMI to start a process and then watch until it's pid is no longer running. Is there some way to determine the pid of the MSI the initial executable is executing, and then watch for that pid to end using WMI? Is the launching process information even associated with a process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2024-07-12 07:09:29

对具有初始设置作为父进程的进程进行 WMI 查找可以解决问题吗? 例如,如果我从进程 ID 为 4000 的命令提示符启动 MSI,我可以执行以下命令行来查找有关 msiexec 进程的信息:

c:\>wmic PROCESS WHERE ParentProcessId=4000 GET CommandLine, ProcessId 
CommandLine                                                 ProcessId
"C:\Windows\System32\msiexec.exe" /i "C:\blahblahblah.msi"  2752

这可能是查找所需信息的一种方法。 这是在 vbs 中查找该信息的演示:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("select * from Win32_Process where ParentProcessId = 4000")
For Each objProcess in colProcesses
    Wscript.Echo "Process ID: " & objProcess.ProcessId
Next

我希望这会有所帮助。

Would doing a WMI lookup of processes that have the initial setup as the parent process do the trick? For example, if I launch an MSI from a command prompt with process id 4000, I can execute the following command line to find information about msiexec process:

c:\>wmic PROCESS WHERE ParentProcessId=4000 GET CommandLine, ProcessId 
CommandLine                                                 ProcessId
"C:\Windows\System32\msiexec.exe" /i "C:\blahblahblah.msi"  2752

That may be one way to find the information you need. Here is a demo of looking up that information in vbs:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("select * from Win32_Process where ParentProcessId = 4000")
For Each objProcess in colProcesses
    Wscript.Echo "Process ID: " & objProcess.ProcessId
Next

I hope this helps.

眉目亦如画i 2024-07-12 07:09:29

如果您使用 .NET 语言(您可以在 Win32 中执行此操作,但在 .NET 中更容易),您可以枚举系统中的所有进程(在对 Setup.exe 的初始调用完成后)并查找所有进程父进程的 PID 等于 Setup.exe 的 PID - 然后监视所有这些进程。 当它们完成时 - 设置完成。 确保它们不会再产生任何子进程。

If you're using a .NET language (you can do it in Win32, but waaaay easier in .NET) you can enumerate all the Processes in the system (after your initial call to Setup.exe completes) and find all the processes which parent's PID equal to the PID of the Setup.exe - and then monitor all those processes. When they will complete - setup is complete. Make sure that they don't spawn any more child processes as well.

小伙你站住 2024-07-12 07:09:29

这应该可以做到。

$p1 = [diagnostics.process]::start($pathToExecutable) # this way we know the PID of the initial exe
$p2 = get-wmiobject win32_process -filter "ParentProcessId = $($p1.Id)" # using Jim Olsen's tip
(get-process -id $p2.ProcessId).WaitForExit() # voila--no messy sleeping

不幸的是,.NET 对象没有 ParentProcessId 属性,并且 WMI 对象没有 WaitForExit() 方法,因此我们必须来回操作。

支持 Jeffrey Snover(始终) 这篇文章

This should do it.

$p1 = [diagnostics.process]::start($pathToExecutable) # this way we know the PID of the initial exe
$p2 = get-wmiobject win32_process -filter "ParentProcessId = $($p1.Id)" # using Jim Olsen's tip
(get-process -id $p2.ProcessId).WaitForExit() # voila--no messy sleeping

Unfortunately, the .NET object doesn't have a ParentProcessId property, and the WMI object doesn't have the WaitForExit() method, so we have to go back and forth.

Props to Jeffrey Snover (always) for this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文