No announcement yet.

remounting drive lettters by script

  • Filter
  • Time
  • Show
Clear All
new posts

  • remounting drive lettters by script

    Hi all,
    Is there a way to automatically mount drive letters (on SAN disks) after they loose it (by some unchanging identifier like GUID or path)? I tried using diskpart, but since windows changes the volume enumeration every time it looses and reacquires the devices, it cannot guaranty that the same volume will have the same index next time around.


  • #2
    Re: remounting drive lettters by script

    Maybe you can use the Name of the volume ('label')?

    The script below can change the driveletter according to the (unique) Volumename you gave to a volume.

    First the script search for the Name you provided, so it can determine the automatically assigned DeviceID (driveletter, path, ..) to that volume.

    With Mountvol.exe can use the DeviceID to determine the GUID

    Now having the GUID of that drive you can use Mountvol.exe command lines to change drive letters or mount the volume to a drive (and more).

    I wrote this script for USB-drives, you can test if it will work for you;
    (by default this script doesn't change anything yet)
    ' MountMyVol.vbs
    ' by: Remco Simons [NL] 2007
    'STEP 1 - Determine the DeviceID (eg. driveletter)
    '         by using the drive's VolumeName (Label)
    strLabel = "MyDrive"   '<- the name you gave to the volume!!!!
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" &_
            strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
            ("Select DeviceID From Win32_LogicalDisk" _
            & " Where VolumeName = '" & strLabel & "'")
    For Each objItem in colItems
        sTation = objItem.DeviceID
        exit For
    'STEP 2 - Now you can determine the volumeGUID
    '         by using the DeviceID
    Const ForReading = 1
    On Error Resume Next
    Set oShell=CreateObject("WScript.Shell")
    sOutTmp = oShell.ExpandEnvironmentStrings("%temp%\volp.txt") "%comspec% /c mountvol " & sTation & " /L>" & sOutTmp, 0, true
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    With objFSO.OpenTextFile(sOutTmp, ForReading)
     'Do Until .AtEndOfStream
        strLine = .ReadLine
    End With
    wscript.echo strLine
    'FINAL - Create a new path to the volume, 
    '        choose one of the three options
    'option 1. Set Volumename to an Environment variable...
    ']  oshell.Environment.item(Replace(strLabel, " ", vbNullString)) = sTation
    'option 2. Mount to a folder eq to volumename...
    '] strPath = "c:\" & strLabel
    '] If Not objFSO.FolderExists(strPath) Then
    ']     Set fldr= objFSO.CreateFolder(strPath)
    ']     Set fldr = Nothing
    '] End if
    '] "mountvol " & chr(34) & strPath & chr(34) & strLine, 0, true
    '] Set objFileSystem = nothing
    'option 3. To subsitude a drive...
    '] "subst B: " & sTation & "\", 0, true
    'option 4. To change drive letters...
    ']  sNwDrive = "Q:"
    '] "mountvol " & sTation & " /D", 0, true
    '] "mountvol " & sNwDrive & " /D",0 ,true
    '] "mountvol " & sNwDrive & strLine, 0, true
    '( Remove:  ']  for the lines above to activate one of the options )
    Set oShell = Nothing
    Set objFSO = Nothing
    wscript.echo "Done!  " & strLabel
    Wscript.quit 0

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts


    • #3
      Re: remounting drive lettters by script

      Not sure if that matters, but the devices are SAN, not USB.

      Anyway, I am trying to failover Exchange, using VSS snapshot. The problem is the DR devices are blocked most of the times, and after I clear the vol attributes and disable and enable the devices (or reboot), the labels sometimes disappear. They only reappear after I manually assign a drive letter.