Fail a task sequence or program if a process is running

When upgrading certain applications for instance plug-ins to Internet Explorer, Internet explorer must not be running if the upgrade is to be successful. I answered this question on the Technet forum during the week and I thought I would share the simple script here.
I have a more advanced script which prompts the user to close open applications with the possibility to postpone the upgrade, I will post that later it just needs a bit more cleaning up 😉

The script can be used as a step in a  Task Sequence or “run this program before” and if you want to check more processes just add a new line as the script uses a command-line argument to pass the process-name to check status.

Example:

checkprocess1

Example syntax: cscript.exe checkprocess.vbs iexplore.exe

Script:

'The script will return error code 1 if process is running
'Processname can be passed on the command line as a variable
'Example cscript.exe checkprocess.vbs Iexplore.exe

Option explicit
DIM strComputer,strProcess
DIM Args

Set args = WScript.Arguments
strProcess = args.Item(0)

If args.count <> "1" then
wscript.echo "incorrect syntax"
wscript.echo "Syntax example = checkprocess.vbs notepad.exe"
end if

IF isProcessRunning(strProcess) THEN
wscript.quit(1)

END IF
FUNCTION isProcessRunning(BYVAL strProcessName)

DIM objWMIService, strWMIQuery

strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

SET objWMIService = GETOBJECT("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
isProcessRunning = TRUE
ELSE
isProcessRunning = FALSE
END IF

END FUNCTION

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *