Summary
This article demonstrates how to obtain hard drive information from a remote machine using WMI.
DL» drive_report.zip
This article demonstrates how to obtain hard drive information from a remote machine using WMI.
DL» drive_report.zip
Retrieve Hard Drive Summary Data from a Remote Machine
Keeping track of Hard Drive space / usage is critical to planning future upgrades. Being able to retrieve
this information at will greatly simplifies one part of life-cycle management for network servers. It is far
too time-consuming to login to each server on the network and collect this information manually. Using WMI allows
an Administrator to collect this information from a single location. The sample code demonstrates how to
achieve this task. It should be noted that the account running the script must have permissions to access WMI
on the remote machine.
' Copyright© 2004 Michael McLean
'
' Usage: cscript.exe drv_rpt.vbs --server=[COMPUTERNAME] [UNIT]
' [COMPUTERNAME] NetBIOS, FQDN or IP address to remote computer
' [UNIT] Determines unit used in displaying drive space.
' Must be KB, MB, GB or TB.
'
' [Last Updated: 21 Nov 2004]
' http://www.mmclean.ca
'
' Win32_LogicalDisk DriveTypes
' UNKNOWN_DRIVE = 0
' NO_ROOT_DIR = 1
' REM_DISK = 2
' HARD_DISK = 3
' NET_DRIVE = 4
' CD_DVD = 5
' RAM_DISK = 6
Option Explicit
On Error Resume Next
Const HARD_DISK = 3
Const SUCCESS = 1, EMPTY_RESULTSET = -1, INVALID_PARAMS = -2
Const INVALID_UNIT = -3, OBJECT_NOT_INITIALIZED = -4, DIVIDE_BY_ZERO = -5
Const INVALID_COMPUTERNAME = -6
Dim lstArgs, sComputer, nUnit, nDec, oWMIService, colDisks, oDisk
Set lstArgs = WScript.Arguments
If lstArgs.Count = 2 Then
sComputer = Trim( lstArgs( 0 ))
Else
displaySyntax()
WScript.Quit( INVALID_PARAMS )
End If
Select Case UCase( lstArgs( 1 ))
Case "KB"
nUnit = 1024
nDec = 0
Case "MB"
nUnit = 1048576
nDec = 0
Case "GB"
nUnit = 1073741824
nDec = 2
Case "TB"
nUnit = 1099511627776
nDec = 4
Case Else
WScript.Echo( "INVALID_UNIT :: " & UCase( lstArgs( 1 )) )
WScript.Quit( INVALID_UNIT )
End Select
sComputer = getServer( lstArgs( 0 ))
If( isaddr( sComputer ) = False ) Then
WScript.Echo( "INVALID_COMPUTERNAME" & vbCRLF _
& " [" & Trim( sComputer ) & "]" )
WScript.Quit( INVALID_COMPUTERNAME )
End If
Set oWMIService = GetObject( "winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\cimv2" )
If( IsEmpty( oWMIService ) = True ) Then
WScript.Echo( "OBJECT_NOT_INITIALIZED :: " & sComputer )
WScript.Quit( OBJECT_NOT_INITIALIZED )
End If
Set colDisks = oWMIService.ExecQuery( "SELECT DeviceID, FreeSpace, Size FROM " _
& "Win32_LogicalDisk WHERE DriveType = " & HARD_DISK & "" )
If( IsEmpty( colDisks ) = True ) Then
WScript.Echo( "EMPTY_RESULTSET :: Disk Collection" )
WScript.Quit( EMPTY_RESULTSET )
End If
If( nUnit <> 0 ) Then
For Each oDisk in colDisks
WScript.Echo( "Drive Letter: " & oDisk.DeviceID )
WScript.Echo( "Used Space: " & FormatNumber(( oDisk.Size - oDisk.FreeSpace ) _
/ nUnit, Abs( nDec )) & " " & UCase( lstArgs( 1 )))
WScript.Echo( "Free Space: " & FormatNumber( oDisk.FreeSpace / nUnit, _
Abs( nDec )) & " " & UCase( lstArgs( 1 )))
WScript.Echo( "Capacity: " & FormatNumber( oDisk.Size / nUnit, Abs( nDec ))_
& " " & UCase( lstArgs( 1 )))
WScript.Echo( "Percent Free: " & FormatNumber(( oDisk.FreeSpace / oDisk.Size )_
* 100, 3 )) & "%" & vbCRLF
Next
Else
WScript.Echo( "DIVIDE_BY_ZERO" )
WScript.Quit( DIVIDE_BY_ZERO )
End If
WScript.Quit( SUCCESS )
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' String Functions
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function displaySyntax()
WScript.Echo " Usage: cscript.exe drv_rpt.vbs --server=[COMPUTERNAME] [UNIT]"
WScript.Echo vbTAB & "[COMPUTERNAME] NetBIOS, FQDN or IP address to " _
& "remote computer."
WScript.Echo vbTAB & "[UNIT] Determines unit used in displaying drive space."
WScript.Echo vbTAB & " Must be KB, MB, GB or TB."
End Function
Function getServer( ByVal svrName )
Dim sName, oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^--server="
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Test to see if parameter token is valid.
If( oRegEx.Test( svrName) = True ) Then
sName = oRegEx.Replace( svrName, "" )
Else
displaySyntax()
WScript.Quit( INVALID_PARAMS )
End If
getServer = sName
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid IP address or domain name.
Function isaddr( ByVal intxt )
If(( isip( intxt ) = True ) OR ( isdomain( intxt) = True )) Then
isaddr = True
Else
isaddr = False
End If
End Function
' Function to determine if string is a valid IP address.
Function isip( ByVal intxt )
Dim oRegEx
Set oRegEx = New RegExp
oRegEx.Pattern = "^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\."_
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])\." _
& "(\d|[01]?\d\d|2[0-4]\d|25[0-5])$"
oRegEx.Global = True
oRegEx.IgnoreCase = True
isip = oRegEx.Test( intxt )
Set oRegEx = Nothing
End Function
' Function to determine if string is a valid domain name.
Function isdomain( ByVal intxt )
Dim bok, retVal, oRegEx, dnsLabel, ele
retVal = True
bok = True
' Ensure string does nto start or end with a '.'
If(( bok = True ) AND (( Left( intxt, 1 ) = "." ) OR ( Right( intxt, 1 ) = "." ))) Then
retVal = False
bok = False
End If
' String must be less than 256 characters and greater than 1 character.
If(( bok = True ) AND (( Len( intxt ) > 255 ) OR ( Len( intxt ) < 2 ))) Then
retVal = False
bok = False
End If
' Test for invalid characters in domain name.
If( bok = True ) Then
Set oRegEx = New RegExp
oRegEx.Pattern = "[^A-Za-z0-9\.\-\_]"
oRegEx.Global = True
oRegEx.IgnoreCase = True
' Invalid characters found in string.
If( oRegEx.Test( intxt ) = True ) Then
retVal = False
End If
Set oRegEx = Nothing
End If
' Split each component in domain name.
' Ensure string length is less than 64 characters and the string is not a number.
If( bok = True ) Then
dnsLabel = Split( intxt, "." )
For Each ele In dnsLabel
If(( Len( ele ) > 63 ) OR ( IsNumeric( ele ) = True )) Then
retVal = False
Exit For
End If
Next
End If
isdomain = retVal
End Function
