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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with my logic 1

Status
Not open for further replies.

eraH

MIS
Dec 1, 2003
106
NZ
There is something wrong with this script but I can't see what it is.

As soon as a server is reached that doesn't exists, then all servers listed after it come up as doesn't exist.

Sorry about the code formatting, just copy and paste it into a text editor and it should look fine.

Code:
'Error Control
On Error Resume Next

Const HARD_DISK = 3

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileServers = objFSO.OpenTextFile("ServerList.txt", 1)
Set objFileDisks = objFSO.CreateTextFile("disks.txt", True)
Set objFileTemp = objFSO.CreateTextFile("temp.txt", True)

'Disk space limit
DiskSpaceLimit = "0.500"

'Check that ServerList.txt exists
If Not objFSO.FileExists("ServerList.txt") Then
	WScript.Echo "File ServerList.txt Doesn't Exist. Create file ServerList.txt in same folder as script."
	WScript.Echo "Add server names to this file, each on it's own line."
	WScript.Quit
End If
	
'Prints Date
objFileDisks.WriteLine("Date: ")
objFileDisks.WriteLine(FormatDateTime(Date()))

objFileTemp.WriteLine
objFileTemp.WriteLine "******************************************"
objFileTemp.WriteLine " Computers with disk space below " & DiskSpaceLimit * 1000 & "MB"
objFileTemp.WriteLine "******************************************"

'Read's file one line at a time until the end of the file
Do Until objFileServers.AtEndOfStream
 strLine = objFileServers.ReadLine
 
   objFileDisks.WriteLine
   objFileDisks.WriteLine "=========================================="
   objFileDisks.WriteLine "Computer: " & strLine
   objFileDisks.WriteLine "=========================================="
   
   	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strLine & "\root\cimv2")
    
    If Err.number <> 0 Then
    	objFileTemp.WriteLine "Couldn't connect to the server." & strLine
    	objFileTemp.WriteLine
    Else    
    	Set colDisks = objWMIService.ExecQuery _
    	("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
		For Each objDisk in colDisks
    		intFreeSpace = objDisk.FreeSpace
    		intTotalSpace = objDisk.Size
    		objFileDisks.WriteLine "Drive Letter"& vbTab & vbTab & objDisk.DeviceID
    		objFileDisks.WriteLine "Total Disk Size"& vbTab & vbTab & FormatNumber(intTotalSpace / 1073741824 ,3)
    		objFileDisks.WriteLine "Free Space"& vbTab & vbTab & vbTab & Formatnumber(objDisk.Freespace / 1073741824 ,3)
   			objFileDisks.WriteLine    
    		If FormatNumber (intFreeSpace / 1073741824 ,3) < DiskSpaceLimit Then
    		   	objFileTemp.WriteLine
				objFileTemp.WriteLine "=========================================="
   				objFileTemp.WriteLine "Computer: " & strLine
   				objFileTemp.WriteLine "=========================================="
    		
    			objFileTemp.WriteLine "Drive Letter"& vbTab & vbTab & objDisk.DeviceID
    			objFileTemp.WriteLine "Total Disk Size"& vbTab & vbTab & FormatNumber(intTotalSpace / 1073741824 ,3)
    			objFileTemp.WriteLine "Free Space"& vbTab & vbTab & vbTab & Formatnumber(objDisk.Freespace / 1073741824 ,3)
   				objFileTemp.WriteLine
    		End If
		Next
	End If
Loop
objFileServers.Close

'Add contents of temp.txt to disks.txt
Set objFileDisks = objFSO.OpenTextFile("disks.txt", 8)
Set objFileTemp = objFSO.OpenTextFile("temp.txt", 1)
objFileDisks.WriteLine(objFileTemp.ReadAll)
objFileDisks.Close
objFileTemp.Close
 
...
If Err.number <> 0 Then
objFileTemp.WriteLine "Couldn't connect to the server." & strLine
objFileTemp.WriteLine
[!]Err.Clear[/!]
Else
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, that fixed it.

I used that same error control in another script and didn't have problems.

At least I know this is the correct way to do it now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top