I am attempting to create a VBscript that will kill a Windows process that is passed in as a parameter (argument). I have the following code and believe the problem to be at either line 8, 18, or 24 but am not sure what the problem is. When I replace that code with notepad.exe instead of the variable, it works. Any help would be great. Thanks.
Dim prcid
Dim check
Dim Inp
Set Inp = WScript.Arguments
check=0
Set objService = GetObject("winmgmts:")
For Each Process In objService.InstancesOf("Win32_process")
If process.name= "Inp" Then
prcid=process.processid
check=1
Exit For
End If
Next
If check =0 Then
WScript.Quit [ExitCode]
End if
For Each process In objService.InstancesOf("Win32_process")
If process.name= "Inp" Then
If process.processid=prcid Then
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'Inp'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
End If
Exit For
End If
Next
You are using a variable as a literal:
1.
process.name= "Inp"should beprocess.name = Inp(twice)2.
"SELECT * FROM Win32_Process WHERE Name = 'Inp'"should be"SELECT * FROM Win32_Process WHERE Name = '" & Inp & "'"And you are using the
WScript.Argumentsobject incorrect: to get the first argument from the command line, useWScript.Arguments(0)Disclaimer: I did not test your code with these enhancements, I just spotted these errors on first sight. There could be more lurking inside.