Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Catch Cancellation error from objShell.Run

Posted on 2016-08-27
10
Medium Priority
?
108 Views
Last Modified: 2016-09-22
I have a script which installs programs. Everything works fine. But if the user cancels the installation a script error occurs. Is there anyway we can end the script with a custom message.

Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
Set objShell = CreateObject("WScript.Shell")
objShell.Run strPath & "\McAfee\XP\MTPForXP.exe"            

Once the program is executed a security warning comes which say's do you want this program to make changes to your computer. if the user selects yes the script exits and the program installs and everything is fine.

But if the user Clicks No a script error occurs. Is there anyway we can end the script with a custom message.
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
10 Comments
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 41773020
.Run returns the result, if you set the third parameter bWaitOnReturn to true. A non-zero result means a warning or error.
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
Set objShell = CreateObject("WScript.Shell") 
result = objShell.Run(strPath & "\McAfee\XP\MTPForXP.exe",1,true)

Open in new window

0
 
LVL 85

Accepted Solution

by:
oBdA earned 1500 total points
ID: 41773054
Try something like this:
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.CurrentDirectory
On Error Resume Next
intErrorLevel = objShell.Run(strPath & "\McAfee\XP\MTPForXP.exe", 1, true)
If Err.Number <> 0 Then 
	If Err.Number = -2147023673 Then
		MsgBox "Elevation declined by user, installation canceled!"
	Else
		MsgBox "Error " & Err.Number & vbCRLF & "Source: " & Err.Source & vbCRLF & "Description: " & Err.Description
	End If
	Err.Clear
Else
	If intErrorLevel = 0 Then
		MsgBox "Installation was successful."
	ElseIf intErrorLevel = 3010 Then
		MsgBox "Installation was successful, but requires a reboot."
	Else
		MsgBox "Installation finished with errorlevel " & intErrorLevel
	End If
End If
On Error Goto 0

Open in new window

0
 

Author Comment

by:Venkata Ramesh Pagadala
ID: 41773072
Thanks a lot for the help. Works beautifully.
0
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
LVL 85

Expert Comment

by:oBdA
ID: 41794563
Sorry, but I'm afraid I have to object to the Close Request (not because I mind sharing points, but because the suggested accepted answer would not answer the question correctly).

Qlemo,
the initial question was [...] a security warning comes which say's do you want this program to make changes to your computer. [...] But if the user Clicks No a script error occurs. Is there anyway we can end the script with a custom message.

This is the UAC prompt - and a "No" to an UAC prompt will not be caught by a simple bWaitOnReturn, but result in a script error, without the possibility to display a custom message (try it, for example, with regedit.exe using an administrative user in a non-elevated shell). So implementing the "On Error Resume Next" is not "adding more flesh to [your] code", it's a requirement.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41794574
I didn't read the script error as being an UAC prompt, but likely it is.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41802970
If the OP will not provide anything, my adjusted suggestion is
   https:#a41773020   125
   https:#a41773054   375
as oBdA is probably correct regarding UAC prompts, but my code is still part of the solution.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41803271
No objection, your honor.
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question