myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


  


Script for collecting logical disk info

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> VB Script >> Script for collecting logical disk info Page: [1]
Login
Message << Older Topic   Newer Topic >>
Script for collecting logical disk info - 10/3/2008 2:36:53 PM   
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 |
Post #: 1
RE: Script for collecting logical disk info - 10/8/2008 12:09:01 PM   
vrodrigues

 

Posts: 68
Score: 9
Joined: 6/8/2001
From: Massachusetts
Status: offline
Here's a function I created that will convert Byte size to kB, MB, GB, TB, etc... 
This will help make the bytes all little more readable.


Function ConvertByteSize(Size)
Dim CommaLocate
Dim suffix
Const NumDecimalPlaces = 2

Do While InStr(Size,",") 'Remove commas from size
    CommaLocate = InStr(Size,",")
    Size = Mid(Size,1,CommaLocate - 1) & _
    Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
Loop
 
Suffix = " Bytes"
If Size >= 1024 Then suffix = " kiloBytes (kB)" ' binary multipliers are 2 to the 10th power or 1024 to the 1st power
If Size >= 1048576 Then suffix = " MegaBytes (MB)" ' binary multipliers are 2 to the 20th power or 1024 to the 2nd power
If Size >= 1073741824 Then suffix = " GigaBytes (GB)" ' binary multipliers are 2 to the 30th power or 1024 to the 3rd power
If Size >= 1099511627776 Then suffix = " TeraBytes (TB)" ' binary multipliers are 2 to the 40th power or 1024 to the 4th power
If Size >= 1125899906842624 Then suffix = " PetaBytes (PB)" ' binary multipliers are 2 to the 50th power or 1024 to the 5th power
If Size >= 1152921504606846976 Then suffix = " ExaBytes (EB)" ' binary multipliers are 2 to the 60th power or 1024 to the 6th power
If Size >= 1180591620717411303424 Then suffix = " ZettaBytes (ZB)" ' binary multipliers are 2 to the 70th power or 1024 to the 7th power
If Size >= 1208925819614629174706176 Then suffix = " YottaBytes (YB)" ' binary multipliers are 2 to the 80th power or 1024 to the 8th power

Select Case Suffix
    Case " kiloBytes (kB)" Size = Round(Size / 1024, NumDecimalPlaces)
    Case " MegaBytes (MB)" Size = Round(Size / 1048576, NumDecimalPlaces)
    Case " GigaBytes (GB)" Size = Round(Size / 1073741824, NumDecimalPlaces)
    Case " TeraBytes (TB)" Size = Round(Size / 1099511627776, NumDecimalPlaces)
    Case " PetaBytes (PB)" Size = Round (Size / 1125899906842624, NumDecimalPlaces)
    Case " ExaBytes (EB)" Size = Round(Size / 1152921504606846976, NumDecimalPlaces)
    Case " ZettaBytes (ZB)" Size = Round(Size / 1180591620717411303424, NumDecimalPlaces)
    Case " YottaBytes (YB)" Size = Round(Size / 1208925819614629174706176, NumDecimalPlaces)
End Select
ConvertByteSize = Size & Suffix
End Function




(in reply to jschramke)
Post #: 2
RE: Script for collecting logical disk info - 10/8/2008 11:52:12 PM   
jschramke


Posts: 64
Score: 4
Joined: 9/5/2002
From: Princeton, New Jersey
Status: offline
I apologize that I did not include this in the description, but the script that I posted returns values in GB, not Bytes.

_____________________________

| Jay Schramke |

(in reply to vrodrigues)
Post #: 3
Page:   [1]
All Forums >> [Scripting Technologies] >> VB Script >> Script for collecting logical disk info Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts



  
Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.359