I need a script to change the CDROM drive letter

Author Message
lucdesaulniers
  • Total Posts : 11
  • Scores: 0
  • Reward points : 0
  • Joined: 5/11/2005
  • Location:
I need a script to change the CDROM drive letter - Wednesday, May 11, 2005 10:34 AM
0
Hi,

I have a script that changes my cd-rom drive from d: to f: but it requires a reboot to be effective.

Does ne1 know if it is possible to make it effective right away without rebooting the computer?

Here is the script.

'Begining of script.
' Script that changes drive letters
' Note: Do NOT use it on SYSTEM or BOOT partition drive letters !!!
' Author: Torgeir Bakken


sComputer = "."
Const HKLM = &H80000002


' from/to
If ChangeDrvLetter("D:", "F:") Then
WScript.Echo "Drive letters changed, please reboot to see the change!"
Else
WScript.Echo "Failed changing drive letters!"
End If


Function ChangeDrvLetter(sSourceDrive, sTargetDrive)
bOK = True ' Init value
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sComputer & "\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)


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


If iRC = 0 Then
oReg.DeleteValue HKLM, sKeyPath, sSrc
Else
bOK = False
End If
Else
bOK = False
End If
ChangeDrvLetter = bOK
End Function

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
Re: I need a script to change the CDROM drive letter - Wednesday, May 11, 2005 4:10 PM
0
Someone correct me if I'm wrong, but I think only Logical Drives can be renamed without having to reboot.

lucdesaulniers
  • Total Posts : 11
  • Scores: 0
  • Reward points : 0
  • Joined: 5/11/2005
  • Location:
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 2:31 AM
0
Why would we be able to change it through Disk management without reboot then?

There should be a way.

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 2:50 AM
0
Maybe this will help:

http://support.microsoft.com/kb/300415/

lucdesaulniers
  • Total Posts : 11
  • Scores: 0
  • Reward points : 0
  • Joined: 5/11/2005
  • Location:
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 3:49 AM
0
Good to know but it does not fix my problem.
But thanks anyway for trying...

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 3:58 AM
0
"Why would we be able to change it through Disk management without reboot then?"

If that is the case could you not use the SendKeys Method to do it?

lucdesaulniers
  • Total Posts : 11
  • Scores: 0
  • Reward points : 0
  • Joined: 5/11/2005
  • Location:
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 5:15 AM
0
What do you mean?
Do you have an example?
Thanks.

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
Re: I need a script to change the CDROM drive letter - Thursday, May 12, 2005 10:28 AM

Country73
  • Total Posts : 754
  • Scores: 10
  • Reward points : 0
Re: I need a script to change the CDROM drive letter - Friday, May 13, 2005 5:35 AM
0
I haven't had a chance to try this out, but maybe this link will help out:
http://groups.google.co.uk/group/microsoft.public.windowsxp.security_admin/msg/feed137416c78ee4?dmode=source

TNO
  • Total Posts : 2094
  • Scores: 36
  • Reward points : 0
  • Joined: 12/18/2004
  • Location: Earth
Re: I need a script to change the CDROM drive letter - Friday, May 13, 2005 5:26 PM
0
That code is the first thing that was posted. It was already said that you still need to reboot.

lucdesaulniers
  • Total Posts : 11
  • Scores: 0
  • Reward points : 0
  • Joined: 5/11/2005
  • Location:
Re: I need a script to change the CDROM drive letter - Monday, May 16, 2005 3:39 AM
0
Here is the new script I use and it works fine but I have an unknown problem with my MSI file that I launch this script with...When I execute the script directly by double-clicking on the vbs. It checks if the F G or H drive are free and if they are it reassign it to the CD drive. But when I launch the script through my MSI file, it maps the drive directly on the F drive and does not even see that it is already mapped.

Here is the vbs script :
' Changes the D drive to F,G or H drive depending if it is in use.
Dim nRtn, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set oDrv = fso.Drives

If fso.DriveExists("F") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvF.txt"
nRtn = wshShell.Run(strRun, 2 , True)
ElseIf fso.DriveExists("G") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvG.txt"
nRtn = wshShell.Run(strRun, 2 , True)
ElseIf fso.DriveExists("H") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvH.txt"
nRtn = wshShell.Run(strRun, 2 , True)
End If

***End of Script***
Here is the content of one of the chgDrv.txt that is a diskpart script :

select volume 0
assign letter=F
exit

***End Of Script***
If anyone have an idea to make this script work through an MSI it would be great. Thanks.

timdawg
  • Total Posts : 1
  • Scores: 0
  • Reward points : 0
  • Joined: 7/14/2007
RE: Re: I need a script to change the CDROM drive letter - Saturday, July 14, 2007 10:24 AM
0
Excellent idea lucdesaulniers, however, I took it a little further.  To ensure that adding or changing devices doesn't mess with the volume numbers, I wrote a script that makes sure a specific Device ID gets a specific Drive Letter.  If the Drive letter is not correct, it writes the diskpart script, executes diskpart, then deletes the file it just created, and goes on to the next drive.  Here it is:

=====Script Start=====
On Error Resume Next

'Device IDs
DeviceID_V = ""
DeviceID_W = ""
DeviceID_X = ""
DeviceID_Y = ""
DeviceID_Z = ""

strComputer = "."
Dim nRtn, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")

For Each objItem in colItems
  Select Case (objItem.DeviceID)
    Case DeviceID_V
      x = ChgDrv ("V:", objItem.Drive)
    Case DeviceID_W
      x = ChgDrv ("W:", objItem.Drive)
    Case DeviceID_X
      x = ChgDrv ("X:", objItem.Drive)
    Case DeviceID_Y
      x = ChgDrv ("Y:", objItem.Drive)
    Case DeviceID_Z
      x = ChgDrv ("Z:", objItem.Drive)
   End Select
Next


Function ChgDrv(Drive, theDrive)
wscript.echo theDrive & " => " & Drive
 If theDrive <> Drive Then

      'Create File
      strFilename = "C:\Scripts\cdrom\chgdrv" & Left(Drive,1) & ".txt"
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.CreateTextFile(strFilename)
      objFile.Close

      'Write Data to File
      Set objFile = objFSO.OpenTextFile (strFilename, 2)
      objFile.WriteLine "select volume " & theDrive
      objFile.WriteLine "assign letter=" & Drive
      objFile.WriteLine "exit"
      objFile.Close

      'Run Diskpart calling the script just created
      strRun = "diskpart /s " & strFilename
      nRtn = wshShell.Run(strRun, 2 , True)
     
      'Delete File
      objFSO.DeleteFile(strFilename)
  End If
End Function
=====Script End=====
Don't forget to insert your Device IDs.