• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2512
  • Last Modified:

How to catch and handle the Windows Script Host Error Code 800A01CE

I have a vbs script which occasionally throws this error. This is fine, however, I need it to handle this error rather than just skipping over this entry. I have attached the function that I wrote below. I used an if statement to check for an error number but it doesn't seem to be catching this error. Any help would be apprieciated.

Here is the popup error that I get
Script: D:\myscript.vbs
Line: 9
Char: 1
Error: The remote server machine does not exist or is unavailable: 'GetObject'
Code: 800A01CE
Function GetOS(aComputer) 
'WMI is required for this script to function 
Dim strComputer, strWMIOS 
strComputer = aComputer 
Err.Clear
Dim objWmiService
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2") 
if (Err.Number <> 0) Then
	GetOS = "ERROR With WMI Impersonate"
end if
'msgbox Err.Number
Dim strOsQuery : strOsQuery = "Select * from Win32_OperatingSystem" 
Dim colOperatingSystems : Set colOperatingSystems = objWMIService.ExecQuery(strOsQuery) 
Dim objOs 
Dim strOsVer 
 
For Each objOs in colOperatingSystems 
        strWmios = objOs.Caption & " " & objOs.Version 
Next 
    
if (Err.Number <> 0) Then
	GetOS = "ERROR With WMI Impersonate"
else
    	GetOS = strWmios
end if
 
End Function

Open in new window

0
bbcac
Asked:
bbcac
  • 5
  • 2
1 Solution
 
yehudahaCommented:
first you need to use  : on error resume next
before the line that error

like this

if Err.Number <> 0 Then
        Err.Clear
        GetOS = "ERROR With WMI Impersonate"
Else
On Error Goto 0
end if

by writing on error resume next i catching error and skiping them so if no error happen i use
on error goto 0 to turn off error catching and continune normally
0
 
yehudahaCommented:
first you need to use  : on error resume next
before the line that error

like this

if Err.Number <> 0 Then
        Err.Clear
        GetOS = "ERROR With WMI Impersonate"
Else
On Error Goto 0
end if

by writing on error resume next i catching error and skiping them so if no error happen i use
on error goto 0 to turn off error catching and continune normally
0
 
yehudahaCommented:
wmi error can cause from unreachable computer so you can use this function to check if it's reachable
and use if statment like this

if reachable(strcomputer) then
'your code
else
wscript.echo strcomputer & " not reachable"
end if
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function

Open in new window

0
Independent Software Vendors: 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!

 
bbcacAuthor Commented:
ok... I was moreso looking for a way to handle the exception.  Reason being, that I also sometimes get an access denied error.

Why doesn't the Err.Number check work for this error?
0
 
yehudahaCommented:
as i sad above you should use on error resume next before the line that causing the error, look in my example.

look here :

http://www.devguru.com/technologies/vbscript/QuickRef/onerror.html

http://msdn.microsoft.com/en-us/library/53f3k80h(VS.85).aspx

if you need something else let me know.
0
 
bbcacAuthor Commented:
Yehudaha...See line 8 of my code. I have put an On Error Resume Next there. If I take out all instances of the "On Error Resume Next" my script will through the noted error (from original post) on line 9

If I put back all of the "on error resume next" peices (exactly as seen below), the error is not caught on line 10. It doesn't get handeled until line 25. At this point it simply says "Not an Object". I need it to get caught on line 10 and handelled such that he description is something like "computer is not reachable" or "accessed denied"

Thanks so much for your help so far, I hope my comments are clear
Function GetOS(aComputer) 
On Error Resume Next
'WMI is required for this script to function 
Dim strComputer, strWMIOS 
strComputer = aComputer 
Err.Clear
Dim objWmiService
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2") 
if (Err.Number <> 0) Then
	GetOS = "ERROR With WMI Impersonate 1: " & Err.Description
end if
Dim strOsQuery : strOsQuery = "Select * from Win32_OperatingSystem" 
Dim colOperatingSystems : Set colOperatingSystems = objWMIService.ExecQuery(strOsQuery) 
if (Err.Number <> 0) Then
	GetOS = "ERROR With WMI Impersonate 2: " & Err.Description
end if
Dim objOs 
Dim strOsVer 
 
For Each objOs in colOperatingSystems 
        strWmios = objOs.Caption & " " & objOs.Version 
Next 
    
if (Err.Number <> 0) Then
	'first check if it is reachable
	if (Reachable(aComputer)) then
	GetOS = "ERROR With WMI Impersonate 3: " & Err.Number & " ( " & Err.description & ")"
	else
	GetOS = "Not Reachable"
	end if
else
    	GetOS = strWmios
end if
 
End Function 

Open in new window

0
 
yehudahaCommented:
ok

in the script attached i added a function to check wmi\permission issue before running the actual script
the line:

If per(strcomputer) Then

you can test just change in this line "computername" to the computer you want to check

WScript.Echo GetOS("ComputerName")
WScript.Echo GetOS("ComputerName")
 
Function GetOS(aComputer) 
strComputer = aComputer
If per(strcomputer) Then
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2") 
strOsQuery = "Select * from Win32_OperatingSystem" 
Set colOperatingSystems = objWMIService.ExecQuery(strOsQuery) 
 
For Each objOs in colOperatingSystems 
        getos = objOs.Caption & " " & objOs.Version 
Next 
Else
GetOS = "Permission Issue"
End if
End Function 
 
Function per(computer)
	strcomputer = computer
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	If err.number <> 0 Then
		err.Clear
		per = False
		On Error goto 0
	Else
		per = True
		On Error goto 0
	End If
End Function

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now