No announcement yet.

script to map networkdrives

  • Filter
  • Time
  • Show
Clear All
new posts

  • script to map networkdrives

    Hello everyone,

    I am a windows systems manager but do not have any programming experience. I have a wireless network in our office. laptops connect automatically to the company wireless network. The laptops are only used for presentations etc. A user logs on locally on the laptop so he/she does'nt get the logscript which normaly maps all the networkdrives.

    I need a script that maps a networkdrive to a share dependent to which department the user belongs to. It needs to promt the user to which department he/she belongs en prompt for a user name and password (otherwise it won't connect to the network share). Dependent to the department certain networkdrives need to mapped.

    for example:
    user 1 belongs to department A
    user 1 needs te networkdrives:
    H: \\server1\share1
    P: \\server2\share2

    user 2 belongs to department B
    user 2 needs the networkdrives:
    H: \\server1\share3
    P: \\server2\share2

    Please help, thanks!


  • #2
    Re: script to map networkdrives

    see if this helps you at all. it's not a full solution, but it might help get you started.
    i used to have one on my old laptop I would have pasted but don't have it any more (mainly cause i don't have a network to need it.. :P)
    Please do show your appreciation to those who assist you by leaving Rep Point


    • #3
      Re: script to map networkdrives

      Building on the link tehcamel posted I whipped up a method to do what you need. It uses HTML to get the username and password as I don't know of any other way to mask the password.

      It requires two files; a VBS file and an HTML file.

      Change the variables highlighted in red and save both files to the same directory.

      Save this with a .vbs extension:
      On Error Resume Next
      'Change strPassHTM to the name of the HTML file
      strPassHTM = "password.htm"
      'Change strGroup1 and strGroup2 to the exact names of the groups
      strGroup1 = "Department A" 
      strGroup2 = "Department B"
      'Share path names. Change to the actual paths.
      strSharePath1 = "\\server1\share1" 'This is for Department A's H: drive
      strSharePath2 = "\\server2\share2" 'This is for Department A & B's P: drive
      strSharePath3 = "\\server1\share3" 'This is for Department B's H: drive
      Set objExplorer = WScript.CreateObject _
          ("InternetExplorer.Application", "IE_")
      strCurDir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
      objExplorer.Navigate "file:///" & strCurDir & strPassHTM   
      objExplorer.ToolBar = 0
      objExplorer.StatusBar = 0
      objExplorer.Width = 400
      objExplorer.Height = 350 
      objExplorer.Left = 300
      objExplorer.Top = 200
      objExplorer.Visible = 1             
      Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
          Wscript.Sleep 250                 
      strUser = objExplorer.Document.Body.All.UserName.Value
      strPassword = objExplorer.Document.Body.All.UserPassword.Value
      strButton = objExplorer.Document.Body.All.OKClicked.Value
      Wscript.Sleep 250
      If strButton = "Cancelled" Then
      End If
      Set objRootDSE = GetObject("LDAP://rootDSE")
      strADsPath = objRootDSE.Get("defaultNamingContext")
      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Open "Provider=ADsDSOObject;"
      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection
      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
        objCommand.CommandText = _
          "SELECT distinguishedName FROM 'LDAP://" & strADsPath & "' WHERE " & _
              "objectCategory='user' AND sAMAccountName='" & strUser & "'"
        Set objRecordSetUser = objCommand.Execute
        Do Until objRecordSetUser.EOF
          strUserDN = objRecordSetUser.Fields("distinguishedName").Value
      Set objNetwork = CreateObject("Wscript.Network")
      strUserPath = "LDAP://" & strUserDN
      Set objUser = GetObject(strUserPath)
      For Each strGroup in objUser.MemberOf
          strGroupPath = "LDAP://" & strGroup
          Set objGroup = GetObject(strGroupPath)
          strGroupName = objGroup.CN
          Select Case strGroupName
              Case strGroup1
                  objNetwork.MapNetworkDrive "H:", strSharePath1 
                  objNetwork.MapNetworkDrive "P:", strSharePath2
              Case strGroup2 
                  objNetwork.MapNetworkDrive "H:", strSharePath3
                  objNetwork.MapNetworkDrive "P:", strSharePath2
          End Select
      Save this with a .htm or html extention:
      <SCRIPT LANGUAGE="VBScript">
      Sub RunScript
          OKClicked.Value = "OK"
      End Sub
      Sub CancelScript
          OKClicked.Value = "Cancelled"
      End Sub
      <font size="2" face="Arial">
      Username:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
      <input type="text" name="UserName" size="40"></font></p>
      <font size="2" face="Arial">
      Password:&nbsp;&nbsp;&nbsp; </font><font face="Arial">
      <input type="password" name="UserPassword" size="40"></font></p>
      <input type="hidden" name="OKClicked" size = "20">
      <input id=runbutton class="button" type="button" value=" OK " name="ok_button" onClick="RunScript">
      <input id=runbutton class="button" type="button" value="Cancel" name="cancel_button" onClick="CancelScript">
      Last edited by JeremyW; 3rd May 2010, 15:37.

      Network Consultant/Engineer
      Baltimore - Washington area and beyond