Announcement

Collapse
No announcement yet.

Script to force a certain drive letter.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to force a certain drive letter.

    Here's a script I wrote it enforce drive letters. Internally we use N, and S as static drive letters. There's probably other or even better ways to do this but it was fun trying to write a script to do it.

    Basically what this does... If you want to force a certain share to be on a certain drive it enforces that at logon. For example, if \\computer\share is mapped as Z: it will detect that and move it to the letter you want - say H:. If something else is on H: it will remove that first and map \\computer\share to it.

    Code:
    On Error Resume Next
    
    Dim objNetwork, objShell, sDriveLetter
    
    Set objNetwork = CreateObject("WScript.Network") 
    Set objShell = CreateObject("WScript.Shell") 
    
    MapDrive "N:","\\machine\sharename"
    MapDrive "S:","\\machine2\sharename2"
    
    Sub MapDrive(sDriveLetter,sUNC)
      bResult = DriveExists(sDriveLetter, sUNC)
    
        If bResult = "MatchL" Then
    	'A drive is already mapped to the letter you're attempting to use
    	'Remove it and map your drive in its place
          objNetwork.RemoveNetworkDrive sDriveLetter, "True", "True"
          objNetwork.MapNetworkDrive sDriveLetter, sUNC
        ElseIf Mid(bResult,3,7) = "MatchU" Then
          objNetwork.RemoveNetworkDrive Mid(bResult,1,2), "True", "True"
    	'The drive is mapped but under a different letter
    	'Remove the current mapping under the different letter
    	'Check to make sure it isn't already mapped under the correct letter/unc combination
    	'If its not, map the drive, otherwise do nothing 
            If Not DriveExists(sDriveLetter, sUNC) = "MatchLU" Then
              objNetwork.MapNetworkDrive sDriveLetter, sUNC
            End If
        ElseIf bResult = "MatchLU" Then
    	'Do Nothing drive already exists
        Else
    	'The drive isn't mapped, map it
          objNetwork.MapNetworkDrive sDriveLetter, sUNC
        End If 
    End Sub
    
    Function DriveExists(sDriveLetter, sUNC)
      Set oDrives = objNetwork.EnumNetworkDrives
    	'Loop through all current drives and determine if the drive is already mapped
        For i = 0 to oDrives.Count - 1 Step 2
          If UCase(sDriveLetter = oDrives.Item(i)) And UCase(sUNC = oDrives.Item(i+1)) Then
    	'Drive attempting to be mapped is already mapped
            DriveExists = "MatchLU"
            Exit For
          ElseIf UCase(sDriveLetter = oDrives.Item(i)) And Not UCase(sUNC = oDrives.Item(i+1)) Then
    	'A drive is already mapped using the same drive letter but a different UNC
            DriveExists = "MatchL"
            Exit For
          ElseIf Not UCase(sDriveLetter = oDrives.Item(i)) And UCase(sUNC = oDrives.Item(i+1)) Then
    	'A drive is already mapped using the same UNC but a different drive letter 
            DriveExists = oDrives.Item(i) & "MatchU"
            Exit For
          End If
        Next
    End Function
    
    Wscript.Quit
    Last edited by ahinson; 14th March 2007, 04:31.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

  • #2
    Re: Script to force a certain drive letter.

    nice script Andrew

    I like the part:
    'The drive is mapped but under a different letter
    'Remove the current mapping under the different letter


    I had this script
    Code:
    MapDrive "F:","\\server\share","FriendlyName"
    
    wscript.quit
    
    
    Sub MapDrive(preDrive,sFolderShare,sAlias)
        Set objNet = WScript.CreateObject("WScript.Network")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oShell = CreateObject("Shell.Application")
    
                   If fso.DriveExists(preDrive) Then
                      On Error Resume Next 
                      objNet.RemoveNetworkDrive preDrive
                      On Error GoTo 0
                   End If
    
                   If Not fso.DriveExists(preDrive) Then
                      FreeDrive = preDrive
                   Else
                     'Find the first avaiable driveletter
                         For i = Asc("D") To Asc("Z") 
                             If Not fso.DriveExists(Chr(i)) Then 
                                FreeDrive = Chr(i) & ":" 
                                Exit For 
                             End If 
                         Next
                      sAlias = "["& preDrive &"] "& sAlias
                   End If
    
        objNet.MapNetworkDrive FreeDrive, sFolderShare, False
        oShell.NameSpace(FreeDrive).Self.Name = sAlias
    
        Set oShell = Nothing
        FreeDrive = preDrive
    End Sub
    No advanced checks in my script, just first delete any connections to that driveletter, then create the wanted mapping. So this script is missing the check if the share already is mapped to an other drive letter to remap it.
    But an extra feature in this script is that, if it was not possible to release the driveletter (because it is in use by a localdrive or usbdrive) then the networkshare will be mapped anyway to the first available drive letter. In some occasions that can be better than no mapping at all.

    \Rem

    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

    Comment


    • #3
      Re: Script to force a certain drive letter.

      Rems:

      Thanks for the feedback and the suggestion about a fallback to map to something.

      In this case, our internal app is hardcoded to use N: and S: but generally something is always better than nothing.
      Last edited by ahinson; 16th March 2007, 04:32.
      Andrew

      ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

      Comment

      Working...
      X