Map network drive depending which computer the user connects to RRS feed

  • Question

  • Hello,

    We got currently a very light vbs file to map drives which works fine.

    To continu a migration, I need to add a condition to a mapping vbs script.

    So my question is, how can I add that condition ?

    Below you have the logon script.

    It is for the line "WshNetwork.MapNetworkDrive "J:", "\\Fileserver\" + WshNetwork.UserName that i need to add a condition.

    The idea is, If the user logs on in remote desktop to Computer A and/or B it must be  \\Fileserver2\Applications and for all other computers it muss be \\Fileserver\Company

    Any help is really appreciated.


    Logon script :

    On Error resume Next

    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    set fso = CreateObject("Scripting.FileSystemObject")

    WshShell.Popup "Welcome to Company Network." & CRx2 & "Connecting Drives.", 5, AppTitle, 64
    NetwOk = True

    WshNetwork.MapNetworkDrive "H:", "\\Fileserver\" + WshNetwork.UserName ,FALSE

    WshNetwork.MapNetworkDrive "J:", "\\Fileserver\Company" ,FALSE

    • Edited by Gontie Wednesday, July 2, 2014 3:16 PM
    Wednesday, July 2, 2014 3:15 PM


All replies

  • Use Group Policy Preferences to map a drive instead of a logon script.

    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, July 2, 2014 5:14 PM
    • Marked as answer by Bill_StewartModerator Saturday, July 26, 2014 2:05 PM
    Wednesday, July 2, 2014 3:18 PM
  • Hi,

    I'd use Group Policy for this. You can target specific policies at specific machines using WMI filters.

    EDIT: Hah, slow on the Submit button.

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Edited by Mike Laughlin Wednesday, July 2, 2014 3:19 PM
    • Proposed as answer by jrv Wednesday, July 2, 2014 5:14 PM
    Wednesday, July 2, 2014 3:18 PM
  • Bill and Mike are both right - you should be using Group Policy (GP) or Group Policy Preferences (GPP) to do your drive mappings. In addition to using WMI filters with GP, as mentioned by Mike, you can use Item-Level Targeting with GPP. This is a great way to ensure that mappings only happen for certain hosts, certain security groups (user and computer groups), certain IP ranges, etc.

    That being said, it is quite easy to get the computer name and then use that in an If conditional that wraps your mapping statement. Here's an example.

    Set wshShell = WScript.CreateObject("WScript.Shell")
    ComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
    ComputerName = LCase(ComputerName)
    If ComputerName = "usercomputer01" Then
        WshNetwork.MapNetworkDrive ""H:"", ""\\Fileserver\"" + WshNetwork.UserName ,FALSE
    End If

    Multiple conditions are handled like so...

    var = "one"
    If var = "one" OR var = "two" Then
        Wscript.Echo "match"
    End If

    Wednesday, July 2, 2014 4:00 PM
  • Thanks for all your responses.

    In fact, i don't have the rights to manage the group Policies, so that is why a very little vbs script do very well the job.

    Thanks tommymaynard, i will try this.

    Thursday, July 3, 2014 7:07 AM