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

Error handling in script

Status
Not open for further replies.

eraH

MIS
Joined
Dec 1, 2003
Messages
106
Location
NZ
If I list multiple computers in the array and one of them doesn't exist then the whole script fails, what I would like to happen is for a message to appear saying that the server is offline and then going onto the next server.

Here is the script.

*************************************
*************************************

'
'This script will find the physical drives on a PC and work out the Total Size, Free Space & Percentage of Free Space
'How to run: cscript.exe disks.vbs > disks.txt
'

Wscript.Echo("Date: ")
Wscript.Echo(FormatDateTime(Date()))

Const HARD_DISK = 3

arrComputers = Array(".")


For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")

For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
Wscript.Echo "Drive Letter"& vbTab & vbTab & objDisk.DeviceID
Wscript.Echo "Total Disk Size"& vbTab & vbTab & FormatNumber(intTotalSpace / 1073741824 ,3)
Wscript.Echo "Free Space"& vbTab & vbTab & Formatnumber(objDisk.Freespace / 1073741824 ,3)
Wscript.Echo "Percentage Free Space"& vbTab & FormatPercent(pctFreeSpace)
Wscript.Echo
Next

Next
 
I found how to do this.
Here is the updated script.


'
'This script will find the physical drives on a PC and work out the Total Size, Free Space & Percentage of Free Space
'How to run: cscript.exe disks.vbs > disks.txt
'

Wscript.Echo("Date: ")
Wscript.Echo(FormatDateTime(Date()))

Const HARD_DISK = 3

arrComputers = Array(".")

on error resume next


For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

If Err.number <> 0 Then
Wscript.Echo "Couldn't connect to the server."
else


Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")

For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace
intTotalSpace = objDisk.Size
pctFreeSpace = intFreeSpace / intTotalSpace
Wscript.Echo "Drive Letter"& vbTab & vbTab & objDisk.DeviceID
Wscript.Echo "Total Disk Size"& vbTab & vbTab & FormatNumber(intTotalSpace / 1073741824 ,3)
Wscript.Echo "Free Space"& vbTab & vbTab & Formatnumber(objDisk.Freespace / 1073741824 ,3)
Wscript.Echo "Percentage Free Space"& vbTab & FormatPercent(pctFreeSpace)
Wscript.Echo
Next
End if

Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top