Link to home
Start Free TrialLog in
Avatar of Zach2001
Zach2001

asked on

Remap local removable drive letters from command line

Hi,

We have a load of new Windows 7 machine with those 7in1 USB reader installed.  This is gobbling up a swathe of local drive letters, some of which overlap with our 'standard' net work drive mappings.

Is there a neat way to remap these removable drive letters via login script ?

Eg:

net use g: /d
If exist g: then [[make g: something else]]
net use g: \\server\share

Thanks

z
Avatar of Jofnn
Jofnn
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

You could use a batch file with the following:

net use d: | find "completed successfully"
if errorlevel 0 net use d: /delete
net use d: \\server\share

Is that what you're looking for?
Avatar of Zach2001
Zach2001

ASKER

Hi, thanks, nah, it's the actual means of moving the local removable drive mapping out of the way - rather than testing it's there or not....

HI,

What to you mean "moving the local removable drive mapping out of the way" ??

The code above checks whether it's mapped... if it is, it'll un-map it... The final line then re-maps the drive to your designated \\server\share.

Jonathon
You can't just unmap a local removable media drive mapping, it has to be done either via Disk Managment or DiskPart.
Avatar of Steven Carnahan
Very useful, thanks.  The issue is, finding a neat way to extract the required info from the data returned from the list volume command - piping this into a txt file then running that back into diskpart.

This is tricky as its drive letters I need to be keyed on, not Volume ID's – the association between them will be different across different machines – so I can’t just go changing the drive letters of the Volumes, without first ensuring that volume has the drive letter which is getting in the way…
So is this what you want to do?

1. Determine if drive D: is in use as one of the removable drives on the 7in1
2. Move that assignment to drive X: (available drive letter not used in the login script)
3. Assign drive D: according to login script.

This will prevent losing access to the removable drive while maintaining drive integrity through the login script.
Hi, yup, that's pretty much it in a nutshell :)
Take a look at this:  http://snipplr.com/view/19276/get-list-of-drive-letters-and-labels/

I am a bit tied up to actually work it out but I think you can piece everything together from the two links I gave you.  If you need further assistance let me know.
first off you don;t need to key off of a serial number you can key off a drive letter that might be all you need.  i started to write the script which will key off the serial number and began playing with Diskpart and found it has some neat abilities, like you can list all the volumes and just use that list so my script which checks every letter may not be needed...  

However one thing I've noticed is virtual CDROMs like those made by PowerISO and WinImage cannot be located by Diskpart, therefore they cannot be manipulated by Diskpart... if this is not a problem then we're OK.


Here is how to write your diskpart script to use only drive letters:

Select Volume ZRemoveAssign Letter XExit

Did you think of the new feature of W7 to only show drives which are indeed containing a media? It was especially invented for those one-of-a-million readers ... It can be found in the Folder Options, View, "Hide empty drives in the Computer folder", and should be checked.
okay so here is a partial script, I did more than this but I wanted to pst this much because you can just use this to compare your list of local drives per mapping attempt.  I was starting to create a loop that will go through all your drives you plan to map to compare them, and all that is far beyond your initial scope, I assume you may have done all of that already.

ECHO OFF
SET "LocalDriveList=EMPTY"
SET "DiskPartFile=C:\DiskPartFile\Tmp.dat"
MD "C:\DiskPartFile\"

::- Creates a lisat of drives on your computer to compare against
FOR /F "Tokens=3 skip=8" %%V IN ('ECHO List Volume ^| Diskpart ') DO SET "TD=%%V"&CALL :Start-Make-Drive-List
SET "LocalDriveList=%LocalDriveList:EMPTY =%"

Open in new window

you can compare a single drive as:

ECHO "%LocalDriveList%" | FIND "T:" > nul && ECHO Select Volume %%v>>"DiskPartFile"&ECHO Remove %%v>>"DiskPartFile"&ECHO Assign Letter X

However the thing is making the list of the available drives, this needs to be all the drives NOT currently in use and NOT being mapped by your script.

you will then need to iterate through this list and remove an item each time you assign it to one of the other existing drives.

Then you will also need to handle the possibility there are too many drives.  How do you want to handle that?  or are you certain it woudl never be the case?  (I'm sure a few of my servers coudl have this issue)

obviously before you do anything you should un-map everything currently mapped on the computer using:
NET USE * /D /Y
Another idea to prevent from mapping drives: Change the drive letters to paths. You can do so once for any PC, creating e.g. a "c:\CardReader" folder with 7 subfolders (e.g. "SDCard"), and mapping each card reader slot to one of those subfolders. No drive letter conflict anymore.
Qlemo, that is a terrific Idea!  I think that is definitely the best way to solve these issues!  Thank you vMuch Sir!  that will make the script FAR Simpler!
SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America 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
WOW - I got busy with work (we have auditors here) and couldn't actually write anything here.  But then it looks like anything I might have come up with would have been over complicated as usual. Always happy to learn new ways so thanks for helping out here you guys.  The links that I gave would have pointed to one method but probably not as easy as yours.

:)
This all looks awsome, cheers guys - I'll have a good peek in the AM after some kip.
ASKER CERTIFIED SOLUTION
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
Rob, you legend. MountVol is the hidden gem I was searching for !!

Although I can't make your VBS work with Windows 7 (It just returns 0 and does squat) I have knocked up a very ugly BatchScript which I can deploy at logon, to be run as admin.

I'm no programmer! What do you think, have I made some awful oversight here – I have attempted to make it human readable too, but I suppose a loop could be created with a counter for the remappings (?)

It seems to work very well and is surprisingly fast.



@echo off
REM	Set Drive Letters to be remapped here - Add Remove as Neccessary
echo Setting Variables
Set DriveVar1=i:
Set DriveVar1New=h:
Set DriveVar2=m:
Set DriveVar2New=i:
Set DriveVar3=n:
Set DriveVar3New=j:
Set DriveVar4=o:
Set DriveVar4New=k:

REM	Remap Drive Letter 1 - Add Remove as Neccessary
echo Re-Mounting Drive1
FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar1%% /l') DO SET TempVar=%%A
if NOT (%TempVar%) == (The) (
mountvol %DriveVar1% /D
mountvol %DriveVar1New% %TempVar% 
) 

REM	Remap Drive Letter 2 - Add Remove as Neccessary
echo Re-Mounting Drive2
FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar2%% /l') DO SET TempVar=%%A
if NOT (%TempVar%) == (The) (
mountvol %DriveVar2% /D
mountvol %DriveVar2New% %TempVar% 
) 

REM	Remap Drive Letter 3 - Add Remove as Neccessary
echo Re-Mounting Drive3
FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar3%% /l') DO SET TempVar=%%A
if NOT (%TempVar%) == (The) (
mountvol %DriveVar3% /D
mountvol %DriveVar3New% %TempVar% 
) 

REM	Remap Drive Letter 4 - Add Remove as Neccessary
echo Re-Mounting Drive4
FOR /F "tokens=1 delims= " %%A IN ('mountvol %%DriveVar4%% /l') DO SET TempVar=%%A
if NOT (%TempVar%) == (The) (
mountvol %DriveVar4% /D
mountvol %DriveVar4New% %TempVar% 
) 

REM	Clear Variables
echo Clearing Variables
SET TempVar=
Set DriveVar1=
Set DriveVar1New=
Set DriveVar2=
Set DriveVar2New=
Set DriveVar3=
Set DriveVar3New=
Set DriveVar4=
Set DriveVar4New=

Open in new window

SOLUTION
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
One flaw of that script is that if a drive is not mapped on log in, it will not be remapped at all - shouldn't that be forced? E.g. in the first case, h: will only be mapped if i: is mapped.
That's ace, thanks Qlemo.

This script is to be used to shunt physical drive mappings out the way so that network drives can take there place.  Before this runs, there will be the usual net use  /d and afterwards the usual net use \\server\share

Dunno what the best way to share the points is, but will do my best.

Thanks everybody
There are three ways here to do what I needed, the neatest is a batch file using mountvol - at least for my environment.
Thanks for the points this is pretty cool that each solution has it's own unique methods =)
Thanks for the grade.  I haven't tried the VBS on Windows 7, but I dare say you'd need to run it elevated.  You could use the Elevate powertoy via
elevate.exe wscript.exe remapphysicaldrives.vbs

and it should work.

Regards,

Rob.
QLEMO:

The passed paremters should be 1 & 2 eg:


@echo off
REM     Set Drive Letters to be remapped here - Add Remove as Neccessary
call :Remap e j
call :Remap f k
call :Remap g l
call :Remap h m
call :Remap i n

exit /b 0


REM ----- Don't change this -------
:Remap
echo Remap Drive Letter %1 to %2
FOR /F "tokens=1 delims= " %%A IN ('mountvol %1: /l') DO if NOT "%%A" == "The" (
  echo Remap Drive Letter %1 to %2
  mountvol %1: /D
  mountvol %2: %%A
)

Open in new window

Your are sooo right, Zach! Sorry for that shift!