Jump to content

Scripted assignment of drive letters


Recommended Posts

Hello

I've created a script to change drive letters based on the volume label (from My Computer) or the CD rom device name (which can be seen in device manager). It uses WMI to query to CD drive descriptions and partition labels.

So for example I can setup the PC like this:

cscript SetDriveLetterFromDescription.vbs //Nologo "HL-DT-ST DVD-ROM GDR8163B" "Y:"
cscript SetDriveLetterFromDescription.vbs //Nologo "HL-DT-ST DVDRAM GSA-4163B" "Z:"
cscript SetDriveLetterFromDescription.vbs //Nologo "Data" "D:"
cscript SetDriveLetterFromDescription.vbs //Nologo "512MB DISK" "U:"
cscript SetDriveLetterFromDescription.vbs //Nologo "80Gb USB2 Disk" "P:"

following a reboot the drive letters in explorer will change.

Shortcomings:

1) No support for multiple installations of same type of CDROM or same volume name in a single PC

2) No neat way of handling removable media (eg, compact flash)

3) No protection from people breaking their Windows install by reassigning the boot or system drive

Save the below code in a file named "SetDriveLetterFromDescription.vbs". Take care before running it.

Const ComputerName = "."
Const HKLM = &H80000002
Set wmiServices  = GetObject ( "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)

if WScript.Arguments.count <> 2 then
WScript.Echo "Usage: " & WScript.ScriptName & " Description DriveLetter"
WScript.Quit 1
end if

SetDriveLetterFromDescription WScript.Arguments(0), WScript.Arguments(1)

function SetDriveLetterFromDescription(strDescription,strNewLetter)

currentdriveletter = GetDriveLetterFromDescription(strDescription)

if currentdriveletter = vbNullString then
 strMessage = " Unknown description"
elseif UCase(currentdriveletter)=UCase(strNewLetter) then
 strMessage = " Already set"
elseif ChangeDrvLetter(currentdriveletter,strNewLetter) then
 strMessage = " OK"
else
 strMessage = " Failed"
end if

WScript.Echo strDescription & " " & currentdriveletter & " -> " & strNewLetter & strMessage

end function


function GetDriveLetterFromDescription(strDescription)

Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption,Description,DeviceID,Drive FROM Win32_CDROMDrive WHERE Caption =""" & strDescription & """")

For Each wmiDiskDrive In wmiDiskDrives
 GetDriveLetterFromDescription = wmiDiskDrive.Drive
 exit function
next

Set wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption FROM Win32_LogicalDisk WHERE VolumeName =""" & strDescription & """")

For Each wmiDiskDrive In wmiDiskDrives
 GetDriveLetterFromDescription = wmiDiskDrive.Caption
 exit function
next

end function

Function ChangeDrvLetter(sSourceDrive, sTargetDrive)
bOK = True  ' Init value
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\default:StdRegProv")

sKeyPath = "SYSTEM\MountedDevices"
sSrc = "\DosDevices\" & UCase(sSourceDrive)

iRC = oReg.GetBinaryValue(HKLM, sKeyPath, sSrc, sValue)

If iRC = 0 Then
 sTrg = "\DosDevices\" & UCase(sTargetDrive)

 oReg.DeleteValue HKLM, sKeyPath, sSrc
 iRC = oReg.SetBinaryValue(HKLM, sKeyPath, sTrg, sValue)

 If iRC <> 0 Then
 bOK = False
 End If
Else
 bOK = False
End If
ChangeDrvLetter = bOK
End Function

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...