Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript internal countdown?

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
I know it is a little confusing based on the subject, but here is what I am having problem with.

I have a script that runs on the network and looks into different hostnames based on a txtfile I have, but the problem is that once in a while there is a hostname I get stuck with(is mostly always a different one) which could be caused because it is off or some other reason.

What I would like to know if there is some kind of feature or command I can integrate with my script, so that if the operation into looking into that hostname has taken longer than 5 minutes it would go to the next line(in the text file of hostnames).

Thanks.
 
>the operation into looking into that hostname

It might help if we knew what 'looking into that hostname' consisted of
 
Ok, I'll give you the little piece of code that does it. If you need the whole script I can put it up to.

Code:
Do Until txtFilePc.AtEndOfStream
strLinePc = txtFilePc.Readline
MyArray = Split(strLinePc, ",", -1, 1)
strLinePc = MyArray(0)

ON ERROR RESUME NEXT
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
   strLinePc & "\root\default:StdRegProv")

strRegComputerName = "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"


Dim sUninstallPath(0)
sUninstallPath(0)= "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
'This one is for windows vista and windows 7 computers
'sUninstallPath(1)= "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

i = 0
For count = 0 to 0

oReg.EnumKey HKLM, sUninstallPath(count), aSubkeys
oReg.EnumKey HKCU, sUninstallPath(count), bSubkeys
oReg.EnumKey HKLM, strRegComputerName, cSubKey


	On Error Resume Next
	For Each sSubkey In cSubKey
	
	   oReg.GetStringValue HKLM, strRegComputerName &"\" &_
	      sSubkey, "ComputerName", strComputerName

	Next

	On Error Resume Next
	For Each sSubkey In aSubkeys
	
	   oReg.GetStringValue HKLM, sUninstallPath(count) &"\" &_
	      sSubkey, "DisplayName", tmp

		If Not IsNull(tmp) Then

		ReDim Preserve aAppName(i)

		ReDim Preserve aAppDate(i)

		ReDim Preserve aAppVer(i)

		ReDim Preserve aAppVer2(i)

		aAppName(i) = tmp

	   oReg.GetStringValue HKLM, sUninstallPath(count) &"\" &_
	      sSubkey, "InstallDate", aAppDate(i)
	
	   oReg.GetStringValue HKLM, sUninstallPath(count) &"\" &_
	      sSubkey, "DisplayVersion", aAppVer(i)

	   oReg.GetStringValue HKLM, sUninstallPath(count) &"\" &_
	      sSubkey, "Inno Setup: Setup Version", aAppVer2(i)
i=i+1
		End If
		
	Next

	On Error Resume Next
	For Each sSubkey In bSubkeys

	   oReg.GetStringValue HKCU, sUninstallPath(count) &"\" &_
	      sSubkey, "DisplayName", tmp

		If Not IsNull(tmp) Then

		ReDim Preserve aAppName(i)

		ReDim Preserve aAppDate(i)

		ReDim Preserve aAppVer(i)

		ReDim Preserve aAppVer2(i)

		aAppName(i) = tmp
	

	   oReg.GetStringValue HKCU, sUninstallPath(count) &"\" &_
	      sSubkey, "InstallDate", aAppDate(i)

	   oReg.GetStringValue HKCU, sUninstallPath(count) &"\" &_
	      sSubkey, "DisplayVersion", aAppVer(i)

	   oReg.GetStringValue HKCU, sUninstallPath(count) &"\" &_
	      sSubkey, "Inno Setup: Setup Version", aAppVer2(i)

i=i+1   
		End If
	Next

Next
 
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strLinePc & "\root\default:StdRegProv")

this statement is bound to error when one of the machines you are trying to connect to is offline, wihch i guess is why you have On Error Resume Next before it.
i would advise, something like

For Each strLinePC In arrLinePcs
On Error Resume Next
Err.Clear
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strLinePc & "\root\default:StdRegProv")
If Err.Number = 0 Then
Wscript.Echo "able to connect to " & strLinePc
'carry on with the rest of your registry get info stuff
Else
Wscript.Echo "unable to connect to " & strLinePc
End If
Set oReg = Nothing
Next

at least that way you will get info back that you couldnt connect and you wont run off and try and do all the registry stuff from a machine which you cant connect to

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
I did a little test script to check the way you did it

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.CreateTextFile(".\SoftInv02.txt")

Const HKCR = &H80000000
Const HKCU = &H80000001
Const HKLM = &H80000002

On Error Resume Next
Err.Clear

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
   "ERM-10167180\root\default:StdRegProv")

If Err.Number = 0 Then

WScript.Echo "able to connect to computer"

sUninstallPath= "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

oReg.EnumKey HKLM, sUninstallPath, aSubkeys

'    On Error Resume Next
    For Each sSubkey In aSubkeys
    
       oReg.GetStringValue HKLM, sUninstallPath &"\" &_
          sSubkey, "DisplayName", sAppName

    
       objFile.WriteLine(sAppName)

         

    Next
Else

WScript.Echo "unable to connect to computer"

End If

Set oReg=Nothing




WScript.Echo "Done"

but it gets stuck on that hostname after it echo's 'able to connect to computer'
 
So that is part of my problem, sometimes it IS able to connect to the pc, but for some reason it just hangs, that is why I need some type of timeout to go to the next hostname.
 
So by the absent of answers, I assume this is not possible?
 
Since you're out of control once the WMI call begins, there's no way for you to detect that it stopped unless you have a second script that acts as a watchdog.

Do you know which line it is hanging on? There are quite a few things that happen after your wscript.echo.

Also consider inserting "On Error Goto 0" after you finish your initial error trap. This may help troubleshoot.

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
>you're out of control once the WMI call begins

The problem is that there is an assumption that the remote WMI service is running. The attempts to connect to it, particualrly when the target host if unavailable can be horrendoulsy slow, and the On Error resume Next compunds the problem

Easier to ping the target, and if that fails bypass everything for that host.

I've provided VB6 code for this in the VB6 forum in the past: thread222-1560292. Since it is WMI-based (local WMI host rather than remote, thus avoiding avaiability isue) is works fine in VBScript as well (just remove all the As <type> bits from the function)

 
'On Error Resume Next compounds the problem'....hmm no, it is a requirement to handle the potential runtime (regardless of if you can ping the machine or the call to WMI may still fail'

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
I am well aware that we need to be able to handle the error condition. I was commenting on the OP, not your code; the OP uses On Error Resume Next to ignore errors, which is not the same thing as handling an error. Your code, and the OP's subsequent 'test script', do this better
 
a useful clarification strongm

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top