SBS 2003 assigning mapped drives?

Discussion in 'Windows Server Systems' started by Heeter, Aug 8, 2007.

  1. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Hey All,

    Was wondering if I can setup SBS2003 to assign mapped drives to workstations instead of me going around manually mapping them from each workstation?


    Thanks,

    Heeter
     
  2. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    use a login script, assign the login script to the user accounts
     
  3. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Thanks Fitz,

    would you know of a copy that I can find of this logon script that I could use as a template?

    Since it is a script, I am presuming that it is a .bat file, am I correct?


    Thanks,

    Heeter
     
  4. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    you can use either batch or VBScript/WSH (windows Script Host) - technically, you can also use KIX and some other ones too..

    in Batch:
    net use <drive letter> <share path>
    Code:
    net use z: \\server1\shareMe
    
    in VBScript
    Code:
    set objNet = CreateObject("wscript.Network")
    objNet.MapNetworkDrive "z:","\\server1\shareMe"
    
    Note: neither of these does much error checking.. for example, if the drive letter is already mapped, it will fail. You can put in some error checking etc and remap drives if needed.

    Since I prefer VBScript, you can copy my script below and hack it up for your needs. It does some minor error checking..
    Code:
    set objNet = CreateObject("Wscript.Network")
    set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strPath = "\\server1\shareName"
    strDriveLetter = "z:"
    
    'checks to see if the drive letter is already mapped
    if not ( objFSO.DriveExists(strDriveLetter) ) then 
    
        'drive is not mapped, so just map the drive and we're done
        objNet.MapNetworkDrive strDriveLetter,strPath
    
    else    
    
        'if the drive is already mapped, check to see if it is mapped to the correct share
        'if the drive is mapped correctly, nothing to do
        'if the drive is mapped to a different share, disconnect the share and map correctly
        set objDrive = objFSO.GetDrive(strDriveLetter)
        if not (objDrive.ShareName = strPath) then
            objNet.RemoveNetworkDrive strDriveLetter,true,true
            objNet.MapNetworkDrive strDriveLetter,strPath
        end if
    end if
    
    The scripts will go into the netlogon share of the domain controller (in this case your SBS server). Going into ADUC (Active Directory Users and Computers) you can assign the script under the profile tab in the user account properties. Technically, you can also use a GPO to assign a login script.


    edit: and madmatt.. piss off and stop bugging me in IM to post a script :) :p
     
  5. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Just making sure you fulfill your duties as a moderator and master scripter. =)
     
  6. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Awesome, thanks for the template!!!!!!


    Should I go around and disconnect the existing mapped drives?

    With this setup, I will presume that I can create a different vbscript for each drive that I would like to map, would this be correct?

    saving this file would be *.vbs, like in my situation it would be datadrive.vbs and specdrive.vbs, etc, etc?


    Thanks again for all your help.



    Heeter
     
  7. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732

    Thanks for lighting a fire under his butt, Matt, LOLOL



    Heeter
     
  8. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    well, I like to do it to make sure my users are all connected to the correct drives as some of them got into the habit of remapping drives. The script snippet I wrote will automagically disconnect the drive if the drive letter that you are trying to map correctly is in use.

    funny.. madmatt asked me the same question in an IM..

    basically, no - you don't create seperate scripts for each drive. The user account properties will only accept a single login script file (although, technically I suppose you could create seperate vbs files and call them through a bat file and have the bat file as your one login script.

    But, generally, you'll wrap it into one file. You could remove the variables (strDriveLetter and strPath) and use the actual string values and write multiple connect lines like:
    Code:
    objNetwork.MapNetworkDrive "y:","\\server\share1"
    objNetwork.MapNetworkDrive "z:","\\server\share2"
    .
    .
    .
    
    But you would have to wrap the lines to check each drive letter and disconnect the existing drive for each path if you wanted that functionality.

    A better way would be to turn the mapping script section into a function/subroutine and call the function from the main script. Something along the lines of this:

    Code:
    MapDrive "x:","\\192.168.25.103\c$"
    MapDrive "y:","\\192.168.25.103\e$"
    wscript.quit
    
    Function MapDrive(strDriveLetter,strPath)
      set objNet = CreateObject("Wscript.Network")
      set objFSO = CreateObject("Scripting.FileSystemObject")
    
      if not ( objFSO.DriveExists(strDriveLetter) ) then 
         objNet.MapNetworkDrive strDriveLetter,strPath
      else    
         set objDrive = objFSO.GetDrive(strDriveLetter)
         if not (objDrive.ShareName = strPath) then
            objNet.RemoveNetworkDrive strDriveLetter,true,true
            objNet.MapNetworkDrive strDriveLetter,strPath
         end if
      end if
    End Function
    

    yeah.. I think he called me lazy or something along those lines asking why I didn't just post the script. I havea copy of the IM conversation somewhere on my work laptop (working on my home machine now).
     
    Last edited: Aug 9, 2007
  9. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Thanks again, for your input and time.

    I am going to be making the mods to the SBS2003 Domain Controller in the morning, and I am certainly going to use that last one you posted. Everyone is going to see the same two drives:

    So it should look like this:

    Code:
    MapDrive "M:","\\server\D:\Data$"
    MapDrive "N:","\\server\D:\Spec$"
    wscript.quit
    
    Function MapDrive(strDriveLetter,strPath)
      set objNet = CreateObject("Wscript.Network")
      set objFSO = CreateObject("Scripting.FileSystemObject")
    
      if not ( objFSO.DriveExists(strDriveLetter) ) then 
         objNet.MapNetworkDrive strDriveLetter,strPath
      else    
         set objDrive = objFSO.GetDrive(strDriveLetter)
         if not (objDrive.ShareName = strPath) then
            objNet.RemoveNetworkDrive strDriveLetter,true,true
            objNet.MapNetworkDrive strDriveLetter,strPath
         end if
      end if
    
    End Function
    
    
    I am going to save it as "mapped.vbs"

    I won't bother with going around and disconnecting the M: mapped drives already there.

    How does this look to you?


    I can't thank you enough for your time....



    Heeter
     
  10. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    no worries
    Make sure you remember to add the script to the user account so they actually run the script when they login.
     
  11. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Update,

    Thanks Fitz,

    I still haven't quite figured out the filepath for the shared drives on the network, but it is something that I will continue to play with as I go along. Right now, it is disconnecting the manually mapped drive when the user logs on, but not issuing the vbs mapped drives.

    I erased the default .bat file that was in the logon script section of the profile. But I was wondering if I could of just added the vbs script to the original line, sort of like this into the logon script line of the profile:

    Code:
    logonscript.bat; mappeddrive.vbs
    
    I don't know if this works or not, can't really see the difference.


    Heeter
     
  12. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    This is pretty darn cool - when I first read this thread I remember thinking " why do you need error checking for a login script?" But, this actually will override anything they have done locally? Such as....

    login script says

    net use m: \\contoso1\mattspr0n

    Physical Drive mapping (done manually says)

    m drive is mapped to \\contoso1\mattschildhoodphotos

    So regardless, Matt's pr0n will always be mapped to M?

    How would you go about addressing login scripts that don't seem to run when the person is working from home? Obviously they don't hit the PDC when logging into their machines using cached credentials, and after connecting to the VPN can only access drives that were mapped from a previous session.
     
  13. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    yup.. the M: drive would also map correctly.

    as for VPN, it may depend on what VPN Client is being used.. most clients have some option to run a script after login (in which case, see if it will handle vbs scripts or just wrap the vbs script in a batch file).

    The bigger question on VPN logins is how to get any login script to run at all if they already logged in via Cached credentials.. connecting via VPN using the "login using dial-up" checkbox on the main GINA is a different story..

    If your VPN client supports it, it may be possible to run a post-login/post connection script off the VPN client which might require the script be local on the machine.. and if it won't support vbs would require a BAT file wrapper.. the harder part on that is making sure the local script is updated when changes to the main script changes.

    edit:
    you could also write a local process/service that installs on the client to either watch for VPN connections and/or monitors IP changes and runs the login script that way.

    It's the same issues as when a laptop user can start a computer off the network and later plug in and/or dock the laptop - no login scripts. Or if a computer's network cable is unplugged at startup and after logging in with cached credentials, they plug in the network cable..
     
  14. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Update:
    I am still having issues with my mapped drive vbscript. It is disconnecting the existing M drive, but it is giving me errors, please see screenie.

    Folder to map is on E:: drive (E:/ClientData), would like to call it M: drive

    Thanks,

    Heeter
     

    Attached Files:

  15. kcnychief

    kcnychief █▄█ ▀█▄ █ Political User Folding Team

    Messages:
    16,948
    Location:
    Massachusetts
    I'm assuming if it can't find the path from the computer that is trying to run it, the user who is running the script might not have access to that admministrative share?
     
  16. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    All the users that I have applied this script to in their profile have this error come up.

    The folder in question has full admin/everyone privileges assigned to it.

    I do manually map the drive from the workstation when I am logged in as the user. but when I relogon, the mapped M: drive gets disconnected (As the script intends it to do) and I get this error.


    Heeter
     
  17. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    not sure what the problem is. The error indicates that it doesn't think the share exists..

    You said the m: drive gets disconnected, it should only get disconnected if the m: drive is connected to a different share (for example: the m: drive is already mapped correctly to \\1600SC\NonClientData, the script would disconnect it).
    If you have the M: drive connected correctly and the script is disconnecting the drive, then you are passing the wrong share string into the script.

    just for kicks, go to your start/run and just type in "\\1600SC\clientData$" (without the quotes).
     
    Last edited: Sep 5, 2007
  18. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Start/run, It works without the share sign

    \\1600sc\clientdata$ <-doesn't work
    \\1600sc\clientdata <-works


    Thanks Fitz, for your help so far.........



    Heeter
     
  19. fitz

    fitz Just Floating Along Staff Member Political User Folding Team

    Messages:
    4,076
    Location:
    Chicagoland
    the $ is the mark to set a share as a hidden share.. if you didn't create it as a hidden share, you don't use the $ symbol.

    The share path that gets passed should be the same as what you would use in the start/run (or when you map the drive any other way), so your first line of the script you are using should read:

    Code:
    MapDrive "M:","\\1600SC\ClientData"
    
     
  20. Heeter

    Heeter Overclocked Like A Mother

    Messages:
    2,732
    Oh, Okay

    Hidden Share, got it.

    Thanks for enlightening me on this one,

    Will fix up the script.

    If there is two scripts that I would like to use on a user's profile, do I seperate the scripts with a ";" or just a space between them?

    Thanks a million for your help, I really appreciate it.

    Heeter