Solved

Catch Cancellation error from objShell.Run

Posted on 2016-08-27
10
66 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 68

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 83

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 83

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 68

Expert Comment

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

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 83

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now