' ********************************************************************* ' ' Name : Verify Remote Drive Free Space.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to check sufficient disk space is ' available for a remote hard disk drive. ' ' Arguments : 1 - Server Name. ' 2 - Minimum percentage free (number, 1-100). ' ' WIZARD:PARAMS=1 = Server Name or . for Local Machine|2 = Minimum percentage free (%) ' WIZARD:PARAMEXAMPLE=MyServer 20 ' ' Returns : 0 - Drives OK & sufficient space available. ' 1 - Parameter error. ' 2 - WMI error. ' 3 - Insufficient space available. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = Parameter Error|2 = WMI error or script not supported on O/S|3 = Insufficient free space available on one or more hard disks.||Script output also indicates "OK" for success, or "Error" with details of the faults found. ' ' Notes : Requires Windows Scripting Host (for VBScript) & ' WMI installed. ' ' WIZARD:REMOTE ' ' ********************************************************************* ' Enable in-line error handling On Error Resume Next ' ------ Local declarations ------ Const HARD_DISK = 3 Dim objArguments ' Command line arguments Dim objWMIService ' WMI object Dim objData ' Data object Dim strServer ' The server on which the shared drive is held Dim strData ' The data name Dim colData ' Data collection Dim blnFoundItem ' True if we found an entry Dim intResult ' Result Dim intPercentage ' % required ' ------ Main Logic ------ ' Default the answer intResult = 0 ' Retrieve Parameters, server name Set objArguments = WScript.Arguments strServer = objArguments.Item(0) If Len (strServer) = 0 Then WScript.Echo "Error. Cannot verify disk space. No server name specified" WScript.Quit (1) End If ' Drive name strData = objArguments.Item(1) If Len (strData) = 0 Then WScript.Echo "Error. Cannot verify disk space. No percentage free space specified" WScript.Quit (1) End If ' Check is numeric intPercentage = CLng(strData) If (intPercentage = 0) Or (intPercentage > 99) Then WScript.Echo "Error. Cannot verify disk space. Percentage free space is not numeric or not within the range 1-100" WScript.Quit (1) End If ' Connect to WMI Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2") If Err.Number <> 0 Then WScript.Echo "Error. Cannot connect to WMI." WScript.Echo Err.Description WScript.Quit (2) End If ' Get the status of the disk Set colData = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK) ' Check each disk For Each objData in colData ' Calculate disk space If (((objData.FreeSpace * 100) / objData.Size) < intPercentage) Then intResult = 3 Wscript.Echo "Error. Drive " & objData.DeviceID & " has insufficient space available (" & Round((objData.FreeSpace / 100), 2) & " Kb available, " & Round(((objData.FreeSpace * 100) / objData.Size), 2) & "% free space)" End If Next If intResult = 0 Then Wscript.Echo "OK. All drives have sufficient free space available" End If ' ------ Cleanup & Return ------ Set colData = Nothing Set objData = Nothing Set objWMIService = Nothing WScript.Quit (intResult) ' ------ End of Script ------