Sorry guys, I forgot about doing this
I have had this running for a couple of years as a startup script in the domain
for most desktop workstations. It uses MountVol.exe to actually change the drive
letter. The script just manages the process.
It was written when a new PCs were delivered that had 4 SD type drive which
pushed the use drives up to H: or I: which clashed with our network drives. I
needed to move these up to some other set of letters hence this script was born.
Warning there may be some line wrapping so watch out for that. Its just one of
the hazards of UseNet posts.
********************* Script follows ************************
'MapRemovableDrives.vbs Version 1.00.00
' ******************************************************************
' Copyright (c) 2008, D J Mills DJMills Consulting
'
www.djmills.co.uk
' All rights reserved.
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions
' are met:
'
' * Redistributions of source code must retain the above copyright
' notice, this list of conditions and the following disclaimer.
' * Redistributions in binary form must reproduce the above
' copyright notice, this list of conditions and the following
' disclaimer in the documentation and/or other materials provided
' with the distribution.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
' FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
' COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
' INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
' BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
' CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
' LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
' ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
' POSSIBILITY OF SUCH DAMAGE.
' ******************************************************************
'
'Removable drives such and those for flash drives and cameras get assigned a
dive letter at system startup
'These often take over letters required for mapped network drives and cause the
mapping to fail.
'
'This script re-assigns these devices to a list of acceptable drive letters. It
does not re-assign drive A or B
'You must edit the line later in this code to set the list of drive letters to
use.
'
'There are some debug msgbox lines commented out and the last line if
uncommented will report what happened.
'Actions are logged to C:\Windows\MapRemovableDrives.log
'
Option Explicit
Dim strDrivesToUse 'List of drive letters that can be used as a
string e.g. "PQRST"
Dim strDrivesLeft 'List of drive letters left for use and is edited
as drives are allogated.
Dim strComputer 'Name of computer - Only tested "."
Dim strNewID 'Drive ID to assign
Dim strCmd
Dim objWMIService, objSh
Dim colDisks, colVolumes
Dim objDisk, objVolume
Dim strOutput
Dim strType
Dim intNewID
Dim objFSO
Dim objLogFile
Dim blnChanged 'Turns on the log if a change is made
Const ForAppending = 8
Const Drive_REMOVABLE = 2
Const Drive_FIXED = 3
Const Drive_NETWORK = 4
Const Drive_CDROM = 5 'can be a CD or a DVD
Const Drive_RAMDISK = 6
Const WINDOW_STYLE_HIDDEN = 1
Const WAIT_ON_RETURN = True
'
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' Change the line below to the acceptable drive letters to use
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
strDrivesToUse = "RUVWY"
strComputer = "."
strOutput = ""
blnChanged = False
Set objSh = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
strDrivesLeft = strDrivesToUse 'Init the list to allocate from
'First remove any drive IDs that are already being used from strDrivesLeft
'so that it will not try to assign an in use drive ID later
For Each objDisk in colDisks
intNewID = instr(strDrivesToUse,Left(objDisk.DeviceID,1))
If intNewID > 0 Then
strDrivesLeft = Replace(strDrivesLeft,Left(objDisk.DeviceID,1),"")
End If
Next
' msgbox strDrivesLeft
strOutput = strOutput & "ID: "& vbTab & "Drive Type" & vbCRLF 'Headings
'Now for eeach removable drive test to see if it is Legal, i.e. is in
strDrivesToUse
'If it is do nothing as it is not a conflict
'If it is not move it to one of still in strDrivesLeft
For Each objDisk in colDisks
Select Case objDisk.DriveType
Case 1
strType = "No root directory. Drive type could not be determined."
Case Drive_REMOVABLE 'Removable drive
Select Case objDisk.DeviceID
Case "A:", "B:" 'Ignore the floppies
strType = "Removable drive. '" & objDisk.DeviceID &"' not reset"
Case Else
'See if the current ID is in the list of acceptable drive letters
'If it is then simply remove it from tha acceptable list. If not then
reallocate it
' msgbox strDrivesToUse & " " & objDisk.DeviceID & " " &
Left(objDisk.DeviceID,1)
intNewID = instr(strDrivesToUse,Left(objDisk.DeviceID,1))
' msgbox intNewID
If intNewID = 0 then
strNewID = left(strDrivesLeft,1) & ":"
strDrivesLeft = Mid(strDrivesLeft,2)
strCmd = "%ComSpec% /C FOR /F %V IN (" _
& "'mountvol.exe " & objDisk.DeviceID & " /L'" _
& ") DO (" _
& "mountvol.exe " & objDisk.DeviceID & " /D & " _
& "mountvol.exe " & strNewID & " %V)"
' msgbox strcmd
blnChanged = True
objSh.Run strCmd, WINDOW_STYLE_HIDDEN, WAIT_ON_RETURN
strType = "Removable drive. Reset from '" & objDisk.DeviceID & "' to
'" & strNewID & "'"
Else
strType = "Removable drive. '" & objDisk.DeviceID &"' not reset"
End if
End Select
Case Drive_FIXED
strType = "Local hard disk."
Case Drive_NETWORK
strType = "Network disk."
Case Drive_CDROM
strType = "Compact disk."
Case Drive_RAMDISK
strType = "RAM disk."
Case Else
strType = "Could not be determined."
End Select
strOutput = strOutput & objDisk.DeviceID & vbTab & strType & vbCRLF
Next
'Log the chages made
If blnChanged Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("C:\windows\MapRemovableDrives.log",
ForAppending, True)
objLogFile.Write vbCRLF & Now() & vbCRLF & strOutput
objLogFile.Close
End If
set objFSO = Nothing
set objLogFile = Nothing
set objSh = Nothing
set objWMIService = Nothing
set colDisks = Nothing
' Wscript.Echo strOutput
****************************Script ends*************************