jschramke
Posts: 64
Score: 4 Joined: 9/5/2002 From: Princeton, New Jersey Status: offline
|
Here is a script I wrote to collect logical disk space data from a list of remote systems (drive name, total, free & used space). This must be run using cscript in a shell window. Place a text file named syslist.txt in the same directory as the script. This file must contain the computer names of all of the hosts (workstations and/or servers) with one name per line and no extra spaces. The output will be displayed in the shell window and written to a tab-separated text file named DiskInfo.txt (can be opened in MS Excel). If a system is inaccessible, the name of the system will be recorded in a file named NoConnect.txt. Any existing DiskInfo.txt files in the directory must be deleted before running the script. You will be prompted for local account credentials for the server. The script will first attempt to authenticate using the current users AD credentials and then the local credentials if authentication fails. Copy and paste the code below into a file anmed DiskInfo.vbs or use the attached file and change the extension to vbs. Open a shell window, change the directory to the location of the script file and system list and enter "cscript DiskInfo.vbs" (w/o quotes).
' ********************************
' diskinfo.vbs
' Copyright Jason Schramke
' retrieves disk space data on remote systems
' list of systems must exist in same directory as script
' records output to file
' script must be run using cscript interpreter
' *********************************
' Name of Input File
strInputFile = "Syslist.txt"
' Name of Output File
strOutputFile = "DiskInfo.txt"
' Name of file for recording failures
strFailFile = "NoConnect.txt"
' Prompt for local account username/pw
wscript.echo "Please enter the local administrator account name:"
strUser = Wscript.StdIn.ReadLine
Set objPassword = CreateObject("ScriptPW.Password")
wscript.echo "Password:"
strPassword = objPassword.GetPassword()
' Create file system object
set objFSO = CreateObject("Scripting.FileSystemObject")
' Check for Existing Output file, Create output file
If objFSO.FileExists(strOutputFile) Then
Wscript.echo "You must delete or remove " & strOutputFile & " from this directory prior to running script"
wscript.quit
End If
set objOutputFile = objFSO.OpenTextFile(strOutputFile, 2 , True)
set objFailFile = objFSO.OpenTextFile(strFailFile, 2 , True)
If objFSO.FileExists(strInputFile) Then
set objInStream = objFSO.OpenTextFile(strInputFile, 1)
Set objWebmLocator = CreateObject("WbemScripting.SWbemLocator")
' Write file headers
objOutputFile.WriteLine "Servername" & vbTab & "Drive ID" & vbTab & "Disk Size (GB)" & vbTab & "Free Space (GB)" & vbTab & _
"Space Used (GB)" & vbTab & "Percent Free" & vbTab & "Percent Used"
' Begin reading input file
Do While objInStream.AtEndOfStream <> True
strComputer = objInStream.Readline
On Error Resume Next
wscript.echo "Connecting to " & strComputer
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Err.Clear
Set objWMIService = objWebmLocator.ConnectServer(strComputer, "root\CIMV2", strUser, strPassword)
End If
If Err.Number = 0 Then
Set colDisks = objWMIService.ExecQuery _
("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3")
For Each objDisk in colDisks
strDeviceID = objDisk.DeviceID
Wscript.Echo "DeviceID: "& vbTab & strDeviceID
' Wscript.Echo "File System: "& vbTab & objDisk.FileSystem
intDiskSize = FormatNumber(objDisk.Size / 1074000000, 2)
wscript.echo "Size: "& vbTab & vbTab & intDiskSize
intFreeSpace = FormatNumber(objDisk.FreeSpace / 1074000000, 2)
wscript.echo "Free Space: " & vbTab & intFreeSpace
intSpaceUsed = FormatNumber(intDiskSize - intFreeSpace, 2)
wscript.echo "Space Used: " & vbTab & intSpaceUsed
intPercentUsed = FormatPercent(intSpaceUsed / intDiskSize, 2)
wscript.echo "Percent Used: " & vbTab & intPercentUsed
intPercentFree = FormatPercent(intFreeSpace / intDiskSize, 2)
wscript.echo "Percent Free: " & vbTab & intPercentFree & vbcrlf
objOutputFile.writeline strComputer & vbTab & strDeviceID & vbTab & _
intDiskSize & vbTab & intFreeSpace & vbTab & intSpaceUsed & vbTab & _
intPercentFree & vbTab & intPercentUsed
Next
Else
Wscript.echo strComputer & " unreachable" & vbcrlf
objFailFile.WriteLine strComputer
End If
Err.Clear
Loop
Else wscript.echo "Error!" & vbcrlf & strInputFile & " not found"
End If
wscript.quit
Author does not guarantee the accuracy of the information generated by this code. Use at your own risk. Not for sale or redistribution.
Attachment (1)
_____________________________
| Jay Schramke |
|