Avatar of JenH2
JenH2
Flag for United States of America asked on

VBScript hangs due to rights issue

I'm working on a script that reads a list of computer names, then for each one it connects to the system and inventories it for Adobe products.  I'm running the script with domain admin rights, however there seem to be a few systems that have removed "Domain Admins" from their local admin group.  When the script gets to the part where it queries the system it just hangs, rather than just move on (via On Error Resume Next).  Can anyone help me with a work around for this?

I run the script with CScript.exe so all the Echo commands show me the progress of each system and it always hangs when it echoes "Inventorying..." when it hits a system that I don't have access to.
Const ForAppending = 8
Const ForReading = 1
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Scripts\adobeinventory.csv", ForAppending, True)
Set objComputerList = objFSO.OpenTextFile("C:\Scripts\computers.txt",ForReading)
Set objShell = WScript.CreateObject("Wscript.Shell")
 
objTextFile.WriteLine "Computer Name" & "," & "Description" & "," & "Version" 
 
Do Until objComputerList.AtEndOfStream    
On Error Resume Next
   strComputer = objComputerList.ReadLine
   WScript.Echo "Starting " & strComputer & "..."
   cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Request timed out"" (exit 55)"
   cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
 
   If cmdReturn=0 Then
      cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Ping request could not find host "&strComputer&""" (exit 65)"
      cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
         If cmdreturn=0 Then
            WScript.Echo "Ping successful."
            Err.Clear
            Set objComputer = GetObject("LDAP://cn=" & strComputer & ",cn=Computers,dc=domain,dc=com")
            If Err.Number = 0 Then
               WScript.Echo "System on the domain."
               Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	
               'Get list of software installed
               WScript.Echo "Inventorying..."
               Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product where Description like '%adobe%'")
               For Each objSoftware in colSoftware
                  objTextFile.WriteLine strComputer & "," & objSoftware.Description & "," & objSoftware.Version
               Next
	
               Set objWMIService = "Nothing"
               Set colSoftware = "Nothing"
            Else
               WScript.Echo "System in a Workgroup."	
            End If
         Else
            WScript.Echo "Ping failed."
         End If
    End If
    WScript.Echo strComputer & " was successfully inventoried."
Loop
 
objTextFile.Close
Wscript.echo "Done"

Open in new window

VB Script

Avatar of undefined
Last Comment
JenH2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JenH2

ASKER
Like this?


Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product where Description like '%adobe%'")
If Err.Number = 0 Then
   'Get list of software installed
   For Each objSoftware in colSoftware
      objTextFile.WriteLine strComputer & "," & objSoftware.Description & "," & objSoftware.Version
   Next
Else
   WScript.Echo "Access Denied"
End If
 
Set objWMIService = "Nothing"
Set colSoftware = "Nothing"

Open in new window

Guy Hengel [angelIII / a3]

yes, that looks better.
JenH2

ASKER
Still hanging up.  I also commeneded "On Error Resume Next" out to see if it would give me an error and it still hangs there.  You mentioned checking if solSoftware is an object, but I'm not sure how to do that (still pretty new to scripting).
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Guy Hengel [angelIII / a3]

if solSoftware is nothing then
  'no object returned
else
 ...
end if

JenH2

ASKER
Still hanging up on the system that I don't have access to...  and yeah, it would be easy to take this system off the list, but the list is about 400 computers and I don't know which ones have locked our domain admins...

Here is what I'm currently using:

'On Error Resume Next 
Const ForAppending = 8
Const ForReading = 1
Const HKEY_LOCAL_MACHINE = &H80000002
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Scripts\adobeinventory.csv", ForAppending, True)
Set objComputerList = objFSO.OpenTextFile("C:\Scripts\computers.txt",ForReading)
Set objShell = WScript.CreateObject("Wscript.Shell")
 
objTextFile.WriteLine "Computer Name" & "," & "Description" & "," & "Version" 
 
Do Until objComputerList.AtEndOfStream    
'On Error Resume Next
	strComputer = objComputerList.ReadLine
	WScript.Echo "Starting " & strComputer & "..."
	cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Request timed out"" (exit 55)"
	cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
 
	If cmdReturn=0 Then
		WScript.Echo "Ping did not time out."
		cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Ping request could not find host "&strComputer&""" (exit 65)"
		cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
 
		If cmdreturn=0 Then
			WScript.Echo "Ping successful."
			Err.Clear
			Set objComputer = GetObject("LDAP://cn=" & strComputer & ",cn=Computers,dc=corp,dc=ad,dc=domain,dc=com")
 
				If Err.Number = 0 Then
					WScript.Echo "System on the domain."
					Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
					Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product where Description like '%adobe%'")
 
					If Err.Number = 0 Then	
						
						If colSoftware is Nothing Then
							WScript.Echo "Access Denied"
						Else
							'Get list of software installed
							WScript.Echo "Inventorying..."
							For Each objSoftware in colSoftware
								objTextFile.WriteLine strComputer & "," & objSoftware.Description & "," & objSoftware.Version
							Next
						End If
						
					End If
 
				End If
 
		Else
			WScript.Echo "Ping Failed."
		End If
 
	Else
		WScript.Echo "Ping Failed."
	End If
 
Loop
 
objTextFile.Close
Wscript.echo "Done"

Open in new window

Guy Hengel [angelIII / a3]

actually, I don't see why it would "hang" at those lines!? ...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
JenH2

ASKER
Yeah, its getting very frustrating.  The script works great on most systems, skipping any that aren't online or not on the domain, and works great for the systems that are on the domain and I have access to.  But whenever I hit a system that locked me out it gets to the part where it echoes "System on the domain" and just sits there.

Is there some other sort of method to see if you have rights to a system or not?  Maybe attempt to create a text file in the system's temp forlder and see if that errors out?


JenH2

ASKER
Got it!

I had to create a temp file on the system to see if I had rights to it, then if I was able to do that it can move forward.

There's got to be a better way though...

On Error Resume Next 
Const ForAppending = 8
Const ForReading = 1
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Scripts\adobeinventory.csv", ForAppending, True)
Set objComputerList = objFSO.OpenTextFile("C:\Scripts\computers.txt",ForReading)
Set objShell = WScript.CreateObject("Wscript.Shell")
 
objTextFile.WriteLine "Computer Name" & "," & "Description" & "," & "Version" 
 
Do Until objComputerList.AtEndOfStream    
On Error Resume Next
	strComputer = objComputerList.ReadLine
	WScript.Echo "Starting " & strComputer & "..."
	cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Request timed out"" (exit 55)"
	cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
 
	If cmdReturn=0 Then
		cmdPing = "for /f ""delims=."" %I in ('ping -n 1 "&strComputer&"') do if ""%I""==""Ping request could not find host "&strComputer&""" (exit 65)"
		cmdReturn = objShell.run("cmd /c "+ cmdPing,0,true)
 
		If cmdreturn=0 Then
			WScript.Echo "Ping successful."
			Err.Clear
			Set objComputer = GetObject("LDAP://cn=" & strComputer & ",cn=Computers,dc=Domain,dc=com")
 
				If Err.Number = 0 Then
					WScript.Echo "System on the domain."
					Set TestFile = objFSO.CreateTextFile("\\" & strComputer & "\c$\testfile.txt", True)
					TestFile.WriteLine "This is a test."
					TestFile.Close
					Set TestFile = Nothing
					
						If Err.Number = 0 Then
							Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
							Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product where Description like '%adobe%'")
							WScript.Echo "Inventorying..."
							If colSoftware.Count = 0 Then
								objTextFile.WriteLine strComputer & ",no Adobe applications,0"
							Else
								For Each objSoftware in colSoftware
									objTextFile.WriteLine strComputer & "," & objSoftware.Description & "," & objSoftware.Version
								Next
							End If
							Set objWMIService = Nothing
							Set colSoftware = Nothing
						Else
							WScript.Echo "Access Denied"
							objTextFile.WriteLine strComputer & ",Access Denied"
						End If
						
				End If
 
		Else
			WScript.Echo "Ping Failed."
			objTextFile.WriteLine strComputer & ",Ping Failed"
		End If
 
	Else
		WScript.Echo "Ping Failed."
		objTextFile.WriteLine strComputer & ",Ping Failed"
	End If
 
Loop
 
objTextFile.Close
Wscript.echo "Done"

Open in new window

JenH2

ASKER
Any suggestions on how to verify you have access to a system that isn't as clumsy as creating a file on the system before trying anything else?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Guy Hengel [angelIII / a3]

>Any suggestions on how to verify you have access to a system that isn't as clumsy as creating a file on the system before trying anything else?

with the file is the simplest method. anything else is MUCH clumsier...
JenH2

ASKER
Still had some problems to overcome after adding this into the script, but it was a big help.