troubleshooting Question

Need assistance on how to handle Object that doesn't get created or is nothing

Avatar of wally_davis
wally_davisFlag for United States of America asked on
VB Script
3 Comments1 Solution287 ViewsLast Modified:
On Line 105 of the code below, i.e. --> Set objService = objLocator.ConnectServer(strServer, "\root\default", strDomain & "\" & strUsername, strPassword), this object doesn't get created because I don't have access to that Server to pull the Registry data.
My problem is that when I add (Lines 73 - 84) the Is Nothing on my object in my If ElseIf statement, it pretty much thinks everything is not an object and ends up writing the error "Unable to make WMI Server connection; Error Number: -2147024891; Error Description: Access is denied.". I know this is not the case because when I don't add the "Is Nothing" in my If..ElseIf statement, it writes the Registry data to my output file. However, there is one Server that it hits and it just hangs there. So, I'm a little frustrated and could you some help to sift out the error in my code.
I do appreciate the help very much.
Thank you Experts,
Wallace
Option Explicit

On Error Resume Next

Const HKLM = &H80000002

Dim objExcel, objFSO, objInputFile, objService
Dim x, iRow, AnError
Dim strDomain, strUsername, strPassword
Dim strScriptPath, strServer, strServers, strFilePath, strNameAndVersion
Dim strKey, strSubKey1, strSavVer, strVersionCheck, strDName, strVName
Dim strSAV, strSAV0_N, strSAV0_V, strSAV1_N, strSAV1_V, strSAV2_N, strSAV2_V
Dim strSAV3_N, strSAV3_V, strSAV4_N, strSAV4_V, strSAV5_N, strSAV5_V
Dim strSAV6_N, strSAV6_V, strSAV7_N, strSAV7_V, strSAV8_N, strSAV8_V
Dim strSAV9_N, strSAV9_V, strSAV10_N, strSAV10_V, strSAV11_N, strSAV11_V

x = 2

'Create Excel Worksheet
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add

objExcel.Cells(1, 1).Value = "Server Name"
objExcel.Cells(1, 2).Value = "SAV Name"
objExcel.Cells(1, 3).Value = "SAV Version"
objExcel.Cells(1, 4).Value = "Errors?"

objExcel.Range("A1:E1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

'Read in Server names from file
Set objFSO = CreateObject("Scripting.FileSystemObject")
strScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
strServers = WScript.Arguments.Item(0)
strFilePath = strScriptPath & "\" & strServers

Set objInputFile = objFSO.OpenTextFile(strFilePath, 1)

If IsEmpty(strServers)Then
	WScript.Echo "###########################################################"
	WScript.Echo "#     Usage: Detect_SavCE_Version.vbs ServerList.txt      #"
	WScript.Echo "#                                                         #"
	WScript.Echo "#     Important: You need to run this VBScript and        #"
	WScript.Echo "#                your serverlist.txt file from the        #"
	WScript.Echo "#                the same Directory path.                 #"
	WScript.Echo "#                                                         #"
	WScript.Echo "###########################################################"
	WScript.Quit
End If

strDomain = InputBox("Please enter your Domain name: ")
strUsername = InputBox("Please enter your Username: ")
strPassword = InputBox("Please enter your Password: ")

Do While Not (objInputFile.AtEndOfLine)
	strServer = ""
	strServer = objInputFile.ReadLine
	
	'Check if Server is available
	If Ping(strServer) = True Then
		iRow = x
		
		objExcel.Cells(iRow, 1).Value = strServer
		'Retrieve SavCE Version
		GetSavInfo
		
		AnError = "Error Number: " & Err.Number
		WScript.Echo AnError
		If Err.Number <> 0 Or Err.Number = 80070005 Then
			objExcel.Cells(iRow, 2).Value = "N/A"
			objExcel.Cells(iRow, 3).Value = "N/A"
			objExcel.Cells(iRow, 4).Value = "Unable to make WMI Server connection; " & "Error Number: " & _
											  Err.Number & "; " & "Error Description: " & Err.Description & "."
		ElseIf (objService) Is Nothing Then
			objExcel.Cells(iRow, 2).Value = "N/A"
			objExcel.Cells(iRow, 3).Value = "N/A"
			objExcel.Cells(iRow, 4).Value = "Unable to create object to make WMI Server connection; " & "Error Number: " & _
											  Err.Number & "; " & "Error Description: " & Err.Description & "."
			Err.Clear			
		End If

		x = x + 1
	Else
		iRow = x
		objExcel.Cells(iRow, 1).Value = strServer
		objExcel.Cells(iRow, 2).Value = "N/A"
		objExcel.Cells(iRow, 3).Value = "N/A"
		objExcel.Cells(iRow, 4).Value = "Ping Failed"
		x = x + 1
	End If
Loop

objExcel.Quit()

MsgBox "Done!"

Sub GetSavInfo
	Dim objRegistry, objLocator
	
	Set objLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objService = objLocator.ConnectServer(strServer, "\root\default", strDomain & "\" & strUsername, strPassword)
	AnError = "Error Number: " & Err.Number
	WScript.Echo AnError
	objService.Security_.ImpersonationLevel = 3
	objService.Security_.AuthenticationLevel = 6
	Set objRegistry = objService.Get("StdRegProv")
'	Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
'			strServer & "\root\default:StdRegProv")
	
	If Not(objRegistry) Is Nothing Then
	AnError = "Error Number: " & Err.Number
	WScript.Echo AnError
		'Check for SAV 10.1.6010.6
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{50E125D1-88E5-48CE-80AE-98EC9698E639}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV0_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV0_V
		
		'Check for SAV 10.1.5000.5
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{78D891EF-9E2D-4FC8-A71F-E6F897BA1B21}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV1_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV1_V
		
		'Check for SAV 10.1.4000.4
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{46B63F23-2B4A-4525-A827-688026BE5E40}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV2_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV2_V
		
		'Check for SAV 10.1.394.0
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{A011A1DC-7F1D-4EA8-BD11-0C5F9718E428}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV3_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV3_V
		
		'Version Unknown but will verify - 
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{BA4B71D1-898E-4306-AE87-8BA7A596F0ED}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV4_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV4_V
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5A633ED0-E5D7-4D65-AB8D-53ED43510284}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV5_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV5_V	
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{473AF7D0-B864-4699-9974-DF570C5B6DCE}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV6_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV6_V	
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{33CFCF98-F8D6-4549-B469-6F4295676D83}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV7_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV7_V
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{D75D48AF-E2D5-49EF-9571-EE7AFB6565B4}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV8_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV8_V
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{0698CECB-9072-47B1-AEA1-94CA350989B8}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV9_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV9_V
		
		'Version Unknown but will verify -
		strSAV = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{E9FA3047-0b15-4e19-85ce-ee7fc6e60f99}"
		objRegistry.GetStringValue HKLM, strSAV, "DisplayName", strSAV10_N
		objRegistry.GetStringValue HKLM, strSAV, "DisplayVersion", strSAV10_V
		
		If Not IsNull(strSAV0_N) Or Not IsNull (strSAV0_V) Then
			objExcel.Cells(iRow, 2).Value = strSAV0_N
			objExcel.Cells(iRow, 3).Value = strSAV0_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV0_N = Null
			strSAV0_V = Null
		ElseIf Not IsNull(strSAV1_N) Or Not IsNull (strSAV1_V) Then
			objExcel.Cells(iRow, 2).Value = strSAV1_N
			objExcel.Cells(iRow, 3).Value = strSAV1_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV1_N = Null
			strSAV1_V = Null
		ElseIf Not IsNull(strSAV2_N) Or Not IsNull (strSAV2_V) Then
			objExcel.Cells(iRow, 2).Value = strSAV2_N
			objExcel.Cells(iRow, 3).Value = strSAV2_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV2_N = Null
			strSAV2_V = Null
		ElseIf Not IsNull(strSAV3_N) Or Not IsNull (strSAV3_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV3_N
			objExcel.Cells(iRow, 3).Value = strSAV3_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV3_N = Null
			strSAV3_V = Null
		ElseIf Not IsNull(strSAV4_N) Or Not IsNull (strSAV4_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV4_N
			objExcel.Cells(iRow, 3).Value = strSAV4_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV4_N = Null
			strSAV4_V = Null
		ElseIf Not IsNull(strSAV5_N) Or Not IsNull (strSAV5_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV5_N
			objExcel.Cells(iRow, 3).Value = strSAV5_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV5_N = Null
			strSAV5_V = Null
		ElseIf Not IsNull(strSAV6_N) Or Not IsNull (strSAV6_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV6_N
			objExcel.Cells(iRow, 3).Value = strSAV6_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV6_N = Null
			strSAV6_V = Null
		ElseIf Not IsNull(strSAV7_N) Or Not IsNull (strSAV7_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV7_N
			objExcel.Cells(iRow, 3).Value = strSAV7_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV7_N = Null
			strSAV7_V = Null
		ElseIf Not IsNull(strSAV8_N) Or Not IsNull (strSAV8_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV8_N
			objExcel.Cells(iRow, 3).Value = strSAV8_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV8_N = Null
			strSAV8_V = Null
		ElseIf Not IsNull(strSAV9_N) Or Not IsNull (strSAV9_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV9_N
			objExcel.Cells(iRow, 3).Value = strSAV9_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV9_N = Null
			strSAV9_V = Null
		ElseIf Not IsNull(strSAV10_N) Or Not IsNull (strSAV10_V) Then 
			objExcel.Cells(iRow, 2).Value = strSAV10_N
			objExcel.Cells(iRow, 3).Value = strSAV10_V
			objExcel.Cells(iRow, 4).Value = "N/A"
			strSAV10_N = Null
			strSAV10_V = Null
		Else
			objExcel.Cells(iRow, 2).Value = "SAV CE Name Unknown"
			objExcel.Cells(iRow, 3).Value = "SAV CE Version Unknown"
			objExcel.Cells(iRow, 4).Value = "N/A"
		End If
	End If

	Set objLocator = Nothing
	Set objService = Nothing
	Set objRegistry = Nothing

End Sub

'Ping Results
Function Ping(strServer)
	Dim objShell, boolResults
	Set objShell = CreateObject("WScript.Shell")
	boolResults = objShell.Run("Ping -n 2 -w 1000 " & strServer, 0, True)
	
	If boolResults = 0 Then
		Ping = True
	Else
		Ping = False
	End If
	
	Set objShell = Nothing
End Function
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros