Re: Loop with out Do
From: Richard Mueller [MVP] (rlmueller-NOSPAM_at_ameritech.NOSPAM.net)
Date: 04/20/04
- Next message: Richard Mueller [MVP]: "Re: RUNAS!?!?!?....SU!?!?!?"
- Previous message: Gary: "RUNAS!?!?!?....SU!?!?!?"
- In reply to: ToxicAdmin: "Loop with out Do"
- Next in thread: ToxicAdmin: "Re: Loop with out Do"
- Reply: ToxicAdmin: "Re: Loop with out Do"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 19 Apr 2004 20:49:38 -0500
Hi,
I believe your first program is fine, except you don't account for blank
lines. Your text file probably has a carriage return at the end, and the
blank line raises the error. I modified it as below and it worked for me:
Dim objFile, objFSO, objLog
Const ForWriting = 2, ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\computerlist1.txt", _
ForReading,False)
Set objLog =
objFSO.OpenTextFile("c:\scripts\KB837001Log.txt",ForWriting,True)
objLog.WriteLine "Audit started: " & Date & " " & Time
objLog.WriteBlankLines(1)
Do While objFile.AtEndofStream = False
installed = False
strComputer = objFile.ReadLine
strHotFix = "KB837001"
If strComputer <> "" Then
Set objWMIService = GetObject("winmgmts:\\" & _
strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery_
("Select * from Win32_QuickFixEngineering",,48)
For Each objItem in colItems
If objItem.HotFixID = strHotFix then
installed = true
end if
Next
If installed = true then
'Wscript.Echo "HotFix " & strHotFix _
& " is installed on " & strComputer
objLog.WriteLine "HotFix " & strHotFix _
& " is installed on " & strComputer
Else
'Wscript.Echo "HotFix " & strHotFix _
& " needs to be Installed on " & strComputer
objLog.WriteLine "HotFix " & strHotFix & " needs to" _
& " be installed on " & strComputer
end if
objLog.WriteBlankLines(1)
WScript.Sleep 5000
End If
Loop
objLog.WriteLine vbNewLine & "Audit finished: " & Date & " " & Time _
& vbNewLine & vbNewLine
objFile.Close
objLog.Close
MsgBox "Script is done running"
The second program has "If" statements without corresponding "End If" and a
"For Each" without a "Next". Also, strComputer will never be False, but
rather blank. I use the Trim function when I read lines from a file so that
my code is not fooled by spaces, then I test if the line that was read was
"". When I fixed the problems in the second script, it was clear that the
first one was better. If you want to trap the error if a computer in the
text file is not found (perhaps it is down), then I would suggest:
Dim objFile, objFSO, objLog
Const ForWriting = 2, ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\computerlist1.txt", _
ForReading,False)
Set objLog =
objFSO.OpenTextFile("c:\scripts\KB837001Log.txt",ForWriting,True)
objLog.WriteLine "Audit started: " & Date & " " & Time
objLog.WriteBlankLines(1)
Do While objFile.AtEndofStream = False
installed = False
strComputer = objFile.ReadLine
strHotFix = "KB837001"
If strComputer <> "" Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & _
strComputer & "\root\cimv2")
If Err.Number = 0 Then
On Error GoTo 0
Set colItems = objWMIService.ExecQuery_
("Select * from Win32_QuickFixEngineering",,48)
For Each objItem in colItems
If objItem.HotFixID = strHotFix then
installed = true
end if
Next
If installed = true then
'Wscript.Echo "HotFix " & strHotFix _
& " is installed on " & strComputer
objLog.WriteLine "HotFix " & strHotFix _
& " is installed on " & strComputer
Else
'Wscript.Echo "HotFix " & strHotFix _
& " needs to be Installed on " & strComputer
objLog.WriteLine "HotFix " & strHotFix & " needs to" _
& " be installed on " & strComputer
end if
Else
On Error GoTo 0
objLog.WriteLine "Computer " & strComputer & " not found"
End If
objLog.WriteBlankLines(1)
End If
Loop
objLog.WriteLine vbNewLine & "Audit finished: " & Date & " " & Time _
& vbNewLine & vbNewLine
objFile.Close
objLog.Close
MsgBox "Script is done running"
Notice that I use "On Error Resume Next" only where I anticipate an error,
then I restore normal error handling as soon as possible with "On Error GoTo
0". Also, I try to line up "If" statements with the corresponding "End If",
and "For Each" loops with the corresponding "Next" so I can make sure they
match. Finally, the Sleep is not needed. I hope this helps.
-- Richard Microsoft MVP Scripting and ADSI HilltopLab web site - http://www.rlmueller.net -- "ToxicAdmin" <spmcatcher69@hotmail.com> wrote in message news:d525a912.0404191337.3581809f@posting.google.com... > Hi been working on this script for a while to get it running and with > some adjustments I can get it to work. When I put more decision making > structures in the script I get a "loop with out do error". I am also > trying to avoid using an "on error resume next statment as this is > just a bandaid. The first script I have provided will run but gives > and error at the end of my computer list, as the do loop keeps on > running. In addition to this, the first script provides no error > handling for computers in the list it can not find. The second script > has more error handling but gives me the Loop with out Do error. In > addition to this in script 2 I got away from using a "Do while or loop > until" structure to fix the error at the end of the computerlist text > file. Any help would be greatly appreciated . Thank You in advance. > > on error resume next > Dim objFile, objFSO, objLog > Const ForWriting = 2, ForReading = 1 > > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("c:\scripts\computerlist1.txt", _ > ForReading,False) > Set objLog = objFSO.OpenTextFile("C:\scripts\KB837001Log.txt",ForWriting,True) > objLog.WriteLine "Audit started: " & Date & " " & Time > objLog.WriteBlankLines(1) > > Do While objFile.AtEndofStream = False > installed = False > strComputer = objFile.ReadLine > > strHotFix = "KB837001" > Set objWMIService = GetObject("winmgmts:\\" & _ > strComputer & "\root\cimv2") > Set colItems = objWMIService.ExecQuery("Select * from > Win32_QuickFixEngineering",,48) > For Each objItem in colItems > If objItem.HotFixID = strHotFix then > installed = true > end if > Next > > If installed = true then > 'Wscript.Echo "HotFix " & strHotFix & " is installed on " & > strComputer > objLog.WriteLine "HotFix " & strHotFix & " is installed on " & > strComputer > Else > 'Wscript.Echo "HotFix " & strHotFix & " needs to be Installed on " & > strComputer > objLog.WriteLine "HotFix " & strHotFix & " needs to" _ > & " be installed on " & strComputer > end if > objLog.WriteBlankLines(1) > WScript.Sleep 5000 > Loop > objLog.WriteLine vbNewLine & "Audit finished: " & Date & " " & Time _ > & vbNewLine & vbNewLine > objFile.Close > objLog.Close > > MsgBox "Script is done running" > > > Number 2 > > > Dim objFile, objFSO, objLog > Const ForWriting = 2, ForReading = 1 > > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile("c:\scripts\computerlist1.txt", _ > ForReading,False) > Set objLog = objFSO.OpenTextFile("C:\scripts\KB837001Log.txt",ForWriting,True) > objLog.WriteLine "Audit started: " & Date & " " & Time > objLog.WriteBlankLines(1) > > > Do > installed = False > strComputer = objFile.ReadLine > If strComputer = false then > objLog.Writeline strComputer & " Does not exist " > Else > strHotFix = "KB837001" > Set objWMIService = GetObject("winmgmts:\\" & _ > strComputer & "\root\cimv2") > Set colItems = objWMIService.ExecQuery("Select * from > Win32_QuickFixEngineering",,48) > For Each objItem in colItems > If objItem.HotFixID = strHotFix then > installed = true > If installed = true then > 'Wscript.Echo "HotFix " & strHotFix & " is installed on " & > strComputer > objLog.WriteLine "HotFix " & strHotFix & " is installed on " & > strComputer > Else > 'Wscript.Echo "HotFix " & strHotFix & " needs to be Installed on " > & strComputer > objLog.WriteLine "HotFix " & strHotFix & " needs to" _ > & " be installed on " & strComputer > end if > objLog.WriteBlankLines(1) > WScript.Sleep 5000 > If objFile.AtEndofStream = False then > Exit do > Else > Loop > > objLog.WriteLine vbNewLine & "Audit finished: " & Date & " " & Time _ > & vbNewLine & vbNewLine > objFile.Close > objLog.Close > > MsgBox "Script is done running"
- Next message: Richard Mueller [MVP]: "Re: RUNAS!?!?!?....SU!?!?!?"
- Previous message: Gary: "RUNAS!?!?!?....SU!?!?!?"
- In reply to: ToxicAdmin: "Loop with out Do"
- Next in thread: ToxicAdmin: "Re: Loop with out Do"
- Reply: ToxicAdmin: "Re: Loop with out Do"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|