Suppress popup window in vbs on timeout of Windows Update Search - vbscript

I have a vb-script which checks if there are updates pending. Sometimes windows search returns an error (for example 503) and an error popup is generated. Since I'm not familiar with vbs, I don't know where to start searching for a solution.
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
The problem occurs in line 3. It says:
Script: C:\path-to-my-script\my-script.vbs
Line: 3
Char: 1
Error: 0x80244022
Code: 80244022
Source: (null)
How can I either prevent the popup from being generated or get a handle to it and close it immediately?

Error 0x80244022 means that the update server HTTP service became temporarily unavailable (for whatever reason). See this MSKB article for a list of connectivity-related error codes.
To handle this error put it between an On Error Resume Next and an On Error Goto 0 statement:
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
On Error Resume Next
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
If Err Then
'add logging routine here
WScript.Quit 1
End If
On Error Goto 0
'more code here
Note that the above will indiscriminately terminate the script on any error that occurs when calling the Search method. If you want to handle different errors in different ways, you could for instance put a Select statement inside the conditional:
If Err Then
Select Case Err.Number
Case &h80244022
'quit immediately
WScript.Quit 1
Case &h8009033F
'wait some time and retry
WScript.Sleep 900000
Set searchResult = updateSearcher.Search(...)
Case &h...
'just log a message and continue
...
End Select
End If

Related

How in code to detect if session is enabled rather than just get an error

If I set
#ENABLESESSIONSTATE = false
then
session("foo") = "bar"
then the result is
Microsoft VBScript runtime error '800a0114'
Variable is undefined 'Session'
... file and line no
It would usually indicate a mistaken assumption regarding program flow and I would trace and fix the issue.
However, in a specific set of circumstances I have a case where a piece of code that uses session is always invoked first on every page request. It is to do with performance monitoring.
This code includes a fork - if the user has a session we go one way, if not we go another.
But of course where the users session is absent because we introduced some code that runs with session disabled, we get the crash.
I could solve it with
on error resume next
session("foo") = "bar"
if err.number <> 0 then
' do the no-has-session fork
else
' do the has-session fork
end if
on error goto 0
But I wondered if there is a less hacky approach.
For the sake of having this question show an accepted answer....
Regarding the suggestions for use of the isObject() approach, the results are not good. The following asp...
<%#EnableSessionState=False%>
<% option explicit
response.write "session enabled=" & IsObject(Session)
response.end
%>
results in
Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'Session'
/errortest.asp, line 6
Therefore it would appear that the session object is marked as truly not having been declared.
My conslusion is to construct a function as below.
<%#EnableSessionState=False%>
<% option explicit
response.write "session enabled=" & isSessionEnabled() ' <-- returns false
response.end
function isSessionEnabled()
dim s
isSessionEnabled = true ' Assume we will exit as true - override in test
err.clear() ' Clear the err setting down
on error resume next ' Prepare to error
s = session("foobar") ' if session exists this will result as err.number = 0
if err.number <> 0 then
on error goto 0 ' reset the error object behaviour
isSessionEnabled = false ' indicate fail - session does not exist.
exit function ' Leave now, our work is done
end if
on error goto 0 ' reset the error object behaviour
end function ' Returns true if get to this point
%>
This is then used as
If isSessionEnabled() then
' do something with session
else
' don't be messin with session.
end if

Handling VBScript errors when calling script using Ajax

I have a page that calls a classic ASP/VB script using the jQuery Ajax function, which allows handlers to be specified for success and error results. Those handlers receive back the response from the ASP script, which just executes some SQL code to insert a record into a database. If there's an SQL error that indicates a duplicate key violation, I want to replace the generated error message with a friendly one, using the code below. As I've discovered, that doesn't work because the code never reaches the "if conn.Errors.Count" line. If the SQL generates an error, the code immediately returns with the error message, which includes the line number of the "conn.Execute" line. Is there a way to get this to do what I want?
set conn=CreateObject("ADODB.Connection")
conn.Open ConnString
conn.Execute "INSERT ... " (long statement omitted for readability)
if conn.Errors.Count> 0 then
if instr(conn.Errors(0).Description, "duplicate key") > 0 then
Response.Write "Unable to add herb - code already exists"
else
Response.Write conn.Errors(0).Description
end if
else ' success
Response.Write "Herb added"
end if
conn.Close
set conn=nothing
as others have pointed out, the best solution is to use "on error resume next". so your code would look something like:
on error resume next
set conn=CreateObject("ADODB.Connection")
conn.Open ConnString
conn.Execute "INSERT ... " (long statement omitted for readability)
if Err.Number > 0 then
if instr(Err.Description, "duplicate key") > 0 then
Response.Write "Unable to add herb - code already exists"
else
Response.Write conn.Errors(0).Description
end if
else ' success
Response.Write "Herb added"
end if
conn.Close
set conn=nothing
on error goto 0 '-- this will remove error handling for the rest of the page and can be considered optional in this case

vbs save and close powerpoint presentation

I am trying to save, then close all microsoft applications, such as word, excel, and powerpoint using VBScript.
I have got word and excel to work:
'Word
On Error Resume Next
Set wd = GetObject(, "Word.Application")
On Error Goto 0
If Not IsEmpty(wd) Then
For Each doc In wd.Documents
doc.Save
doc.Close
Next
wd.Quit
End If
And:
'Excel
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
If Err Then
If Err.Number = 429 Then
WScript.Quit 0
Else
CreateObject("WScript.Shell").LogEvent 1, Err.Description & _
" (0x" & Hex(Err.Number) & ")"
WScript.Quit 1
End If
End If
On Error Goto 0
xl.DisplayAlerts = False
For Each wb In xl.Workbooks
wb.Save
wb.Close False
Next
xl.Quit
Set xl = Nothing
Sadly, I have not figured out how to do the same thing with powerpoint. I have researched online, but have not found the answer. Everything I found that worked wasn't using VBScript.
This is the script that I am trying to make work:
'PowerPoint
On Error Resume Next
Set objPPT = GetObject("PowerPoint.Application")
On Error Goto 0
If Not IsEmpty(objPPT) Then
For Each doc In objPPT.Presentation
objPresentation.Save
objPresentation.Close
objPPT.Quit
Next
objPPT.Quit
End If
When I run this script, nothing happens.
Could anyone help me to fix my script please?
Thank you!
I would appreciate the time and effort spent!
This snippet should get that done by looping through the presentations collection (tested with office 2010):
On Error Resume Next
Set objPPT = GetObject(,"powerpoint.Application")
On Error Goto 0
If Not IsEmpty(objPPT) Then
For Each doc In objPPT.Presentations
doc.Save
doc.Close
Next
objPPT.Quit
End If
Note that VBA <> VBScript. Might want to change the tag to correct that.
Anyhow, you've got the object hierarchy wrong
objPPT is a PowerPoint Application object.
The Application object has a Presentations collection, containing one Presentation object for each open Presentation.
You want to iterate through the Presentations collection.
Try this. I'm guessing at the VBScript syntax from the surface of my VBA brain:
On Error Resume Next
Set objPPT = GetObject("PowerPoint.Application")
On Error Goto 0
If Not IsEmpty(objPPT) Then
For Each Presentation In objPPT.Presentations
Presentation.Save
Presentation.Close
objPPT.Quit
Next
objPPT.Quit
End If

returning error code to VBScript

Here is what I am trying to do:
Get a VBScript to run another VBScript.
get the second VBScript to post an error on completion, either 0 if successful or >0 if not back to the original script and then work on conditions Based on the error code returned.
Uninstall 2010 & copy office 2013
'Copy files from a network share to machine
Set FSO = CreateObject("Scripting.FileSystemObject")
WScript.Echo "Starting to uninstall Microsoft Office 2010 from the machine"
FSO.CopyFile "\\data01\Tools\WSM\Copy_2013.vbs", "C:\temp\Copy_2013.vbs"
FSO.CopyFile "\\data01\Tools\WSM\OffScrub10.vbs", "C:\Temp\OffScrub10.vbs"
FSO.CopyFile "\\data01\Tools\WSM\DeleteOffice13Package.vbs", "C:\temp\DeleteOffice13Package.vbs"
'Wait to execute rest of script where copied filed need to be in location
WScript.Sleep 5000
'Executes Office 2013 copy at the same time, do not wait to continue uninstalling office 2010
Set objShell = WScript.CreateObject("WScript.Shell")
Call objShell.Run("C:\temp\Copy_2013.vbs", 0, False)
WScript.Sleep 3000
'Run VBScript that uninstalls office 2010 (currently set to copy a non existent path for error capture test)
strRemoveOffice10 = "c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
Call objShell.Run(strRemoveOffice10, 0, True)
WScript.Echo Err.Number
If Err.Number <> 0 Then WScript.Echo " Microsoft Office 2010 could not be uninstalled. Please uninstall again manually."
If Err.Number = 0 Then WScript.Echo "Microsoft Office 2010 has uninstalled from the machine"
Set objFileSys = Nothing
WScript.Quit
OffScrub10.vbs
Dim objFileSys
Set objFileSys = CreateObject("Scripting.FileSystemObject")
objFileSys.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
On Error Resume Next
If Err.Number <> 0 WScript.Quit Err
To enable error handling you need to put On Error Resume Next before the statement that may cause an error. Then you can return a status code like this:
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
WScript.Quit Err.Number
However, since you said you want a return value >0 in case of an error and Err.Number is an unsigned integer that might be interpreted as a positive or negative value depending on its actual value, something like this might be a better choice:
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fso.GetFolder("C:\Temp\Temp1\bla").Copy "C:\WSM\Test"
If Err Then WScript.Quit 1
WScript.Quit 0 'can be omitted, because it's the default
To check the returned value in the calling script you need to capture it in a variable. When using the Call statement like you do in your first script the return value is simply discarded. VBScript does not put return values of external commands in the Err object. You may also want to make sure that your script is being run with cscript.exe to avoid messages/popups blocking execution.
strRemoveOffice10 = "cscript.exe c:\Temp\offscrub10.vbs ALL /Quiet /NoCancel"
rc = objShell.Run(strRemoveOffice10, 0, True)
If rc = 0 Then
'OK
Else
'an error occurred
End If
Yes, you can return an exit code from your second script to the first as follows...
WScript.Quit(-1)
Where -1 is your exit code of choice.
Option Explicit
If WScript.Arguments.Count = 0 Then
' If we don't have any arguments, call ourselves to retrieve
' the exit code. It will be returned by the call to the
' Run method
Dim returnCode
returnCode = WScript.CreateObject("WScript.Shell").Run ( _
Chr(34) & WScript.ScriptFullName & Chr(34) & " myArgument " _
, 0 _
, True _
)
' Note ^ the "True"
' We need to wait for the "subprocess" to end to retrieve the exit code
Call WScript.Echo(CStr( returnCode ))
Else
' We have arguments, leave current process with exit code
Call WScript.Quit( 1234 )
End If
Quick sample for testing.
There are two elements to consider:
The called subprocess uses the WScript.Quit method to return the process exit code to the caller
The caller must wait for the subprocess to end to retrieve the exit code. The Run method will return the exit code of the subprocess

What does the "On Error Resume Next" statement do?

I came to some VBScript examples, and I saw the statement On Error Resume Next basically at the beginning of the script.
What does it do?
It basically tells the program when you encounter an error just continue at the next line.
It's worth noting that even when On Error Resume Next is in effect, the Err object is still populated when an error occurs, so you can still do C-style error handling.
On Error Resume Next
DangerousOperationThatCouldCauseErrors
If Err Then
WScript.StdErr.WriteLine "error " & Err.Number
WScript.Quit 1
End If
On Error GoTo 0
When an error occurs, the execution will continue on the next line without interrupting the script.
It means, when an error happens on the line, it is telling vbscript to continue execution without aborting the script. Sometimes, the On Error follows the Goto label to alter the flow of execution, something like this in a Sub code block, now you know why and how the usage of GOTO can result in spaghetti code:
Sub MySubRoutine()
On Error Goto ErrorHandler
REM VB code...
REM More VB Code...
Exit_MySubRoutine:
REM Disable the Error Handler!
On Error Goto 0
REM Leave....
Exit Sub
ErrorHandler:
REM Do something about the Error
Goto Exit_MySubRoutine
End Sub
It enables error handling. The following is partly from https://msdn.microsoft.com/en-us/library/5hsw66as.aspx
' Enable error handling. When a run-time error occurs, control goes to the statement
' immediately following the statement where the error occurred, and execution
' continues from that point.
On Error Resume Next
SomeCodeHere
If Err.Number = 0 Then
WScript.Echo "No Error in SomeCodeHere."
Else
WScript.Echo "Error in SomeCodeHere: " & Err.Number & ", " & Err.Source & ", " & Err.Description
' Clear the error or you'll see it again when you test Err.Number
Err.Clear
End If
SomeMoreCodeHere
If Err.Number <> 0 Then
WScript.Echo "Error in SomeMoreCodeHere:" & Err.Number & ", " & Err.Source & ", " & Err.Description
' Clear the error or you'll see it again when you test Err.Number
Err.Clear
End If
' Disables enabled error handler in the current procedure and resets it to Nothing.
On Error Goto 0
' There are also `On Error Goto -1`, which disables the enabled exception in the current
' procedure and resets it to Nothing, and `On Error Goto line`,
' which enables the error-handling routine that starts at the line specified in the
' required line argument. The line argument is any line label or line number. If a run-time
' error occurs, control branches to the specified line, making the error handler active.
' The specified line must be in the same procedure as the On Error statement,
' or a compile-time error will occur.
On Error Statement - Specifies that when a run-time error occurs, control goes to the statement immediately following the statement. How ever Err object got populated.(Err.Number, Err.Count etc)
On Error Resume Next means that On Error, It will resume to the next line to resume.
e.g. if you try the Try block, That will stop the script if a error occurred

Resources