Option Explicit On Error Resume Next Dim colProcesses Dim refProcess Dim refWMI Dim strMoniker Dim strQuery Dim numLow Dim numHigh Dim i 'check that we have the right number of command-line args 'if not, then quit with a usage message If WScript.Arguments.Count <> 3 Then WScript.Echo "Usage: killitIP.vbs " WScript.Quit End If 'Convert the second and third command-line argument into numbers 'using IPToNum() and store their results numLow = IPToNUm(WScript.Arguments(1)) numHigh = IPToNUm(WScript.Arguments(2)) 'check that numLow and numHigh are valid (IPToNum() returns -1 if not) If numLow < 0 Or numHigh < 0 Then WScript.Echo "Incorrect IP address range" WScript.Quit End If 'check that numLow is smaller than numHigh! If numLow > numHigh Then WScript.Echo "The second IP address must be higher than the first." WScript.Quit End If 'Build query string based on process name provided as first cmd-line arg strQuery = "SELECT * FROM Win32_Process WHERE Name='" & WScript.Arguments(0) & "'" 'loop through address range For i = numLow To numHigh Err.Clear() 'try to connect to machine with given address NumToIP() converts the counter value back to a textual address strMoniker = "winmgmts:\\" & NumToIP(i) Set refWMI = GetObject(strMoniker) If Err.Number <> 0 Then WScript.Echo "Failed to connect to " & NumToIP(i) Else 'get a collection of Win32_Process objects Set colProcesses = refWMI.ExecQuery(strQuery) If colProcesses.Count=0 Then WScript.Echo WScript.Arguments(0) & " - Not found on " & NumToIP(i) Else 'there are some matching process, so loop through and kill For Each refProcess in colProcesses If refProcess.Terminate()=0 Then WScript.Echo "(PID " & refProcess.Handle & ") " & refProcess.Name & " Terminated on - " & NumToIP(i) Else WScript.Echo "Unable to terminate - " & refProcess.Name & " on " & NumToIP(i) End If Next End If End If Set colProcesses = Nothing Set refWMI = Nothing Next 'IPToNum() function - turns a textual IP address into a 32-bit number Function IPToNum(strIP) Dim numOctetsArray Dim i numOctetsArray = Split(strIP,".") 'sanity checks If UBound(numOctetsArray) <> 3 Then 'oops = wrong number of octets IPToNum = -1 Exit Function End If For i = 0 to 3 If Not IsNumeric(numOctetsArray(i)) Then 'oops - not an IP address IPToNum = -2 Exit Function End If If numOctetsArray(i) > 254 Then 'oops - octet out of range IPToNum = -3 Exit Function End If Next 'now compile a number IPToNum = numOctetsArray(0) * (2^24) IPToNum = IPToNum + numOctetsArray(1) * (2^16) IPToNum = IPToNum + numOctetsArray(2) * (2^8) IPToNum = IPToNum + numOctetsArray(3) End Function 'NumToIP() function - turns a 32-bit number into an IP address Function NumToIP(numNum) Dim numOctetsArray(4) Dim param 'simulate passbyval Dim i param = numNum 'don't bother with sanity checks here because we are always passing something created by IPToNum numOctetsArray(0) = Int(param / (2^24)) param = param - (numOctetsArray(0) * (2^24)) numOctetsArray(1) = Int(param / (2^16)) param = param - (numOctetsArray(1) * (2^16)) numOctetsArray(2) = Int(param / (2^8)) numOctetsArray(3) = param - (numOctetsArray(2) * (2^8)) NumToIP = numOctetsArray(0) & "." & numOctetsArray(1) & "." & numOctetsArray(2) & "." & numOctetsArray(3) End Function