terminate named Win32_Process

A

abovetreeline

Hi,
How to make javascript terminate a win32_process?

vbscript will terminate a named process with the following code:

strcomputer = "."
Set objwmiservice = GetObject("winmgmts:" _
& "{impersonationlevel=impersonate}!\\"&strcomputer & "\root\cimv2")
Set colprocesslist = objwmiservice.execquery _
("SELECT * FROM Win32_Process WHERE Name = 'winword.exe'")
For Each objprocess In colprocesslist
objprocess.terminate()
next

Javascript: Here is my best attempt at translating the vbscript into
javascript, but javascript just closes with no error message without
killing the process.

strcomputer = "."
var objWMIService =
GetObject("winmgmts:{impersonationlevel=impersonate}!\\\\" +
strcomputer + "\\root\\cimv2");
var colitems = objWMIService.ExecQuery("SELECT * FROM Win32_Process
WHERE Name = 'winword.exe'");
var indexitems;
for (indexitems in colitems){
indexitems.Terminate();
}

Thanks for the help.
 
A

abovetreeline

I had been searching for the answere all weekend and then I found it
just after posting the question. The answer is the following code. I
was missing enumeration when accessing a collection.

strcomputer = "."
var objWMIService, colitems, e
objWMIService =
GetObject("winmgmts:{impersonationlevel=impersonate}!\\\\" +
strcomputer + "\\root\\cimv2");
colitems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE
name = 'winword.exe'");
e = new Enumerator(colitems);
for (; !e.atEnd(); e.moveNext())
{
e.item().Terminate();
}
 
D

Dietmar Meier

vbscript will terminate a named process with the following code: [...]
Javascript: Here is my best attempt at translating the vbscript into
javascript, but javascript just closes with no error message without
killing the process.

JavaScript's for..in does not work as you expect on VB-like collections.
An Enumerator object [1] is your friend:

var strcomputer = "."
var objWMIService = GetObject(
"winmgmts:{impersonationlevel=impersonate}!\\\\" +
strcomputer +
"\\root\\cimv2"
);
var colitems = new Enumerator(
objWMIService.ExecQuery(
"SELECT * FROM Win32_Process WHERE Name = 'winword.exe'"
)
);
for (; !colitems.atEnd(); colitems.moveNext()) {
colitems.item().Terminate();
}

[1] http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjenumerator.asp

ciao, dhgm
 
M

Martin Honnen

strcomputer = "."
var objWMIService =
GetObject("winmgmts:{impersonationlevel=impersonate}!\\\\" +
strcomputer + "\\root\\cimv2");
var colitems = objWMIService.ExecQuery("SELECT * FROM Win32_Process
WHERE Name = 'winword.exe'");
var indexitems;
for (indexitems in colitems){
indexitems.Terminate();
}

You can do it with an Enumerator:

var computer = '.';
var WMIService = GetObject('winmgmts:' +
'{impersonationLevel=impersonate}!\\\\' +
computer + '\\root\\cimv2');
var processList = WMIService.ExecQuery('Select * From Win32_Process');
WScript.Echo('Found ' + processList.Count + ' processes.');
var processEnumerator = new Enumerator(processList);
while (!processEnumerator.atEnd()) {
WScript.Echo(processEnumerator.item().Name);
processEnumerator.moveNext();
}

Note that you would better ask such questions in a WSH oder server
scripting group on the MS news server.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top