' ********************************************************************* ' ' Name : DiskDismount.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to dismount a disk volume. ' ' Arguments : 1 - Server Name or "." ' 2 - Drive letter to dismount ' ' WIZARD:PARAMS=1 = Server Name or . for Local Machine|2 = Name (letter) of drive to dismount ' WIZARD:PARAMEXAMPLE=MyServer T ' ' Returns : 0 - Success ' 1 - Parameter error occurred. ' 2 - WMI error occurred. ' 3 - One or more warnings issued. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = Parameter Error|2 = WMI error or script not supported on O/S|3 = One or more errors/warnings were issued.||Script output also indicates "OK" for success, or "Error" with details of the faults found. ' ' Requires : Windows Server 2003 or above. ' ' Notes : Requires Windows Scripting Host (for VBScript) ' and Windows 2003 server or above. ' ' WIZARD:REMOTE ' ' ********************************************************************* ' Enable in-line error handling On Error Resume Next ' ------ Local declarations ------ Dim objArguments ' Command line arguments Dim objWMI ' WMI object reference Dim objItems ' WMI items returned Dim objItem ' Individual item returned Dim strServer ' The server Dim strVolume ' The volume letter Dim intResult ' The overall result ' ------ Main Logic ------ ' Get the computer name or "." for the local computer Set objArguments = WScript.Arguments strServer = objArguments.Item(0) If Len (strServer) = 0 Then ' No command line specified WScript.Echo "Error. Unable to dismount drive. No server specified" WScript.Quit (1) End If ' get the volume we wish to dismount. This should be a single letter only & must be ' a mountable drive strVolume = objArguments.Item(1) If Len (strVolume) = 0 Then ' No command line specified WScript.Echo "Error. Unable to dismount drive. No drive specified" WScript.Quit (1) ElseIf Len (strVolume) > 1 Then ' No command line specified WScript.Echo "Error. Unable to dismount drive. Drive name is incorrectly specified" WScript.Quit (1) End If ' Access WMI to access the information we require Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2") If Err.Number <> 0 Then ' Error WScript.Echo "Unable to create the WMI object. " & Err.Description WScript.Quit(2) End If Set objItems = objWMI.ExecQuery ("Select * From Win32_Volume Where Name = '" & strVolume & ":\\'") If Err.Number <> 0 Then ' Error WScript.Echo "Unable to execute WMI query. " & Err.Description Set objWMI = Nothing WScript.Quit(2) End If ' Dismount intResult = 0 For Each objItem in objItems ' Dismount the volume objItem.Dismount(True, True) If Err.Number <> 0 Then ' Error intResult = 3 WScript.Echo "Warning: Unable to dismount disk " & objItem.Name & ". " & Err.Description Err.Clear End If Next If intResult = 0 Then WScript.Echo "OK, drive disomoumnted successfully" End If ' ------ Cleanup ------ Set objItems = Nothing Set objWMI = Nothing WScript.Quit(intResult) ' ------ End of Script ------