2013-01-01 52 views

回答

1

使用Get-WMIObject win32_process得到所有进程的列表。查看ParentProcessId成员以查看哪个进程生成了哪个Excel实例。

VB脚本通过WScript的或CScript将执行,所以你需要在正确的父母先看看。

请注意,Windows不会回收进程标识。也就是说,在时刻t0,pid1000不一定是与时刻t1相同的处理。

附录:

使用-Computername切换到另一个指定的WMI查询的计算机。如果没有-Computername交换机,则假定为localhost。本地主机也被称为.,它在上面的VBScript答案中使用。像这样,

#Get process list from Server01, assuming you have sufficient rights 
Get-WMIObject win32_process -Computername server01 
+0

我想要全局运行这个脚本,所以我怎么能够在那里给他们的计算机名称,就像你的link.please指南中提到的'code' – CodeLover

+0

Excel作为一个COM服务器运行,其中ppid = winlogon.exe ,而不是VBS过程。 – EJP

2

即使你标记这个PowerShell的,这里有一个VBS唯一的选择将可能需要根据您的Excel版本的一些修修补补(我有2003对这个PC):

'Create Excel Application for demo purposes only 
Dim ex: Set ex = CreateObject("Excel.Application") 
Dim objWMIService, objProcess, colProcess 
Dim strComputer, strList 

strComputer = "." 'Change if you want to run on another computer 

Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

'Look only at Excel process name 
Set colProcess = objWMIService.ExecQuery _ 
("Select * from Win32_Process Where NAME = 'EXCEL.exe'") 

'Get the list of Processes, included only for demo purposes 
For Each objProcess In colProcess 
    strList = strList & vbCr & objProcess.commandline 
Next 
MsgBox strList ' Displayed "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" 
          '"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE"/automation - Embedding 
       ' Second row was the created Object. 

'Find processes that match the Created Object format commandline 
Set colProcess = objWMIService.ExecQuery _ 
("Select * from Win32_Process Where CommandLine Like '%EXCEL.exe"" /automation -Embedding'") 
For Each objProcess In colProcess 
    'Do something with process 
    objProcess.Terminate 
Next 
'Next line will cause runtime error due to process already being terminated. 
ex.Quit 

美与此类似的方法是,通过使用objProcess.Commandline,您可以找到发送到应用程序的命令行开关,您可以使用它来查明特定的进程。例如,如果您有一个.bat文件来打开Excel文件,如下所示:start Excel.exe C:\example.xls该进程将在进程Commandline属性中包含C:\example.xls

+0

我有疑问!我的脚本会遍布世界各地,所以我很难获得'strComputer'的价值。有没有机会避免相同? – CodeLover

+0

如果您不知道计算机运行在哪台计算机上,您会如何在计算机上运行脚本?我想我不明白这个问题。 '.'将指脚本运行的计算机。 –

+0

让我自己清楚这一点。我需要在该参数上提供“计算机”名称吗?和FYI我正在使用EXCEL 2010 – CodeLover

相关问题