Solved

Catch Cancellation error from objShell.Run

Posted on 2016-08-27
10
90 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 69

Assisted Solution

by:Qlemo
Qlemo earned 125 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 84

Accepted Solution

by:
oBdA earned 375 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 84

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 69

Expert Comment

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

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 84

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

740 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