Link to home
Start Free TrialLog in
Avatar of izharkhan
izharkhanFlag for United States of America

asked on

script to change DVD Drive letter

Hi,
I need a script to change a DVD Drive letter to R: in multiple servers. we have windows 2008 and windows 2003 servers. script should find DVD Drive and then change its letter to R:
In some of the servers we have D: and E: letters for DVD Drive and need to change them to R:
Thanks
Izhar
Avatar of chubby_informer
chubby_informer
Flag of Trinidad and Tobago image

Avatar of RobSampson
Hi, if you have a test server, run this code on that first, but it should work.  It uses WMI to identify drives with a DriveType of 5 (Compact Disc), and changes the letter to R:

This runs locally only, it does not run remotely, because the only way (or at least the most reliable way) to change the drive letter was to use the local MountVol utility.

You could run it remotely using PSExec though.

Regards,

Rob.

ChangeDriveLetters

Sub ChangeDriveLetters
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVolumes = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=5")
For Each objDisk in colVolumes
    If objDisk.DeviceID <> "R:" Then ChangeDriveLetterWithMountvol objDisk.DeviceID, "R:"
Next
End Sub

' Change the drive in one drive letter to another drive letter using
' mountvol.exe tool
'
Sub ChangeDriveLetterWithMountvol(strSourceDrive, strTargetDrive) 

    Dim objShell, objExec
    Dim strVolume
    
    Set objShell = CreateObject("WScript.Shell")
    
    ' Get volume associated to the old drive letter.
    '
    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /L")
    strVolume = Trim(objExec.StdOut.ReadLine())
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend
    
    ' Unmount the drive.
    '
    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /D")
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend
    
    ' Mount the drive on the new drive letter.
    '
    Set objExec = objShell.Exec("mountvol " & strTargetDrive & " " & strVolume)
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend

End Sub 

Open in new window

Avatar of izharkhan

ASKER

Hi Rob,
I tried this script but its not working.
Thanks
Izhar
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial