Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Catch Cancellation error from objShell.Run

Posted on 2016-08-27
10
Medium Priority
?
117 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…
Suggested Courses

927 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