Thursday, March 28, 2024

System Administration: Script to Execute/Kill Remote Applications

Datamation content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Author:Darron Nesbitt
Platform: Windows NT, Windows 2000
Type: VBScript

DESCRIPTION: These two scripts utilize WMI to connect to and run or kill an application on a remote PC. The scripts will run on Windows 2000 or Windows NT.

Execute Remote App script:

This script when executed prompts for the following:

Remote computer’s name

Path to application that will be executed on the remote computer

Path to directory Application resides in

Onces succesfully started a process id will be returned along with the process owner.


Kill Remote App script:

This script when executed prompts for the following:

Remote computer’s name

Process ID of the application to kill

Download the script.
Scroll down to view the script.


SCRIPT:


'************************************************************************
'*		This script is for executing an application on a remote
'*		computer running Win2K or WinNT w/WMI installed.
'*
'*		Author: Darron Nesbitt
'*		Date: 	7/10/2001
'*
'************************************************************************

Dim strComputerName ' The Computer Name to be queried via WMI
Dim strWinMgt		' The WMI management String

On Error Resume Next

'get computer's name or ip address
strComputerName = ucase(InputBox("Enter the remote computers name or IP","Computer Name/IP"))
AppPath = ucase(InputBox("Enter path to application:" & VBCRLF & VBCRLF & "Example:C:WINNTSYSTEM32Calc.exe","Application Path"))
WorkingDirectory = ucase(InputBox("Enter path to application directory" & VBCRLF & VBCRLF &  "Example: C:WINNTSYSTEM32","Application Working Directory"))
strWinMgt = "winmgmts://" & strComputerName & ""

'
' Get Computer/User Info
'
 Set CompSysSet = GetObject(strWinMgt).ExecQuery("select * from Win32_ComputerSystem")
 for each CompSys in CompSysSet
 strDescription = CompSys.Description
 strModel= CompSys.Model
 strName= CompSys.Name
			strManufacturer	= CompSys.Manufacturer
			strUserName		= CompSys.UserName
 next

 CompInfo = "Computer Information" & VBCrLf & VBCrLf
 CompInfo = CompInfo & "Computer Name: " & strName & VBTab & "User: " & strUserName & VBCrLf

'connect to processes
Set Process = GetObject(strWinMgt).Get("Win32_Process")

'start app
RetVal = Process.Create (AppPath,WorkingDirectory,null,PID)

'if 0 not returned error
if RetVal  0 then
	MsgBox "Error: " & Err.Description & ":" & Err.Number
else

	'build ProcessInfo string
	Set Processes = GetObject(strWinMgt).ExecQuery("select * from Win32_Process where ProcessID = " & PID)
	for each Process in Processes
		RetVal = Process.GetOwner(strUser,strDomain)
		ProcessInfo = "Name: " & Process.Caption & VBTAB & "ProcessID: " & PID & VBCRLF
		ProcessInfo = ProcessInfo & "Process Owner: " & ucase(strDomain) & "" & ucase(strUser) & VBCRLF
	next

	'display info about computer and process
	RetVal = MsgBox (CompInfo & VBCRLF & VBCRLF & "Application started." & VBCRLF & VBCRLF & _
			 		 	ProcessInfo,VBOKOnly,strComputerName & " - Start Application")
end if

msgbox "Done!!"




'************************************************************************
'*		This script is for Killing an application on a remote
'*		computer running Win2K or WinNT w/WMI installed. To use you need
'*		the process id of the application.
'*
'*		Author: Darron Nesbitt
'*		Date: 	7/10/2001
'*
'************************************************************************

Dim strComputerName ' The Computer Name to be queried via WMI
Dim strWinMgt		' The WMI management String

On Error Resume Next

'get computer's name or ip address
strComputerName = ucase(InputBox("Enter the remote computers name or IP","Computer Name/IP"))
PID = InputBox("Enter the applications Process ID","Process ID to Kill")
strWinMgt = "winmgmts://" & strComputerName & ""

'
' Get Computer/User Info
'
 Set CompSysSet = GetObject(strWinMgt).ExecQuery("select * from Win32_ComputerSystem")
 for each CompSys in CompSysSet
 strDescription = CompSys.Description
 strModel= CompSys.Model
 strName= CompSys.Name
			strManufacturer	= CompSys.Manufacturer
			strUserName		= CompSys.UserName
 next

 CompInfo = "Computer Information" & VBCrLf & VBCrLf
 CompInfo = CompInfo & "Computer Name: " & strName & VBTab & "User: " & strUserName & VBCrLf

'build ProcessInfo string
Set Processes = GetObject(strWinMgt).ExecQuery("select * from Win32_Process where ProcessID = " & PID)
for each Process in Processes
	RetVal = Process.GetOwner(strUser,strDomain)
	ProcessInfo = "Name: " & Process.Caption & VBTAB & "ProcessID: " & PID & VBCRLF
	ProcessInfo = ProcessInfo & "Process Owner: " & ucase(strDomain) & "" & ucase(strUser) & VBCRLF

	'display info about computer and process
	RetVal = MsgBox (CompInfo & VBCRLF & VBCRLF & "Application to Kill:" & VBCRLF & VBCRLF & _
		 		 	ProcessInfo & VBCRLF & VBCRLF & "Do you want to kill this process?",VBYesNo,strComputerName & " - Kill Application")
	if RetVal = 6 then
		RetVal = Process.Terminate(0)
	end if
next

'if 0 not returned error
if RetVal  0 then
	MsgBox "Error: " & Err.Description & ":" & Err.Number
end if


msgbox "Done!!"


Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided “as -is”, without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Subscribe to Data Insider

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more.

Similar articles

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Latest Articles