XML-based Mapped Drives script for logon scripts

Author Message
ginolard

  • Total Posts : 1347
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
XML-based Mapped Drives script for logon scripts Tuesday, September 25, 2007 2:37 AM (permalink)
0
I thought others might find this useful.  I've been converting our login scripts from KIX to VBS and, much as I loathe KIX, I do admire its ability to handle INI files.  So, when it came to writing the part of the script that mapped the user's network drives I wanted to have a similar method.

This is what I came up with in the end

Sub MapNetworkDrives
 
    On Error Resume Next
    
   'Map group-specific drives based on the entries in the MappedDrives.xml file.
   
    Dim objNetwork    :     Set objNetwork = CreateObject("WScript.Network")
    Dim objFSO        :    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objSysInfo    :     Set objSysInfo = CreateObject("ADSystemInfo")
    Dim objUser     :    Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
    Dim XMLDoc        :     Set xmlDoc = CreateObject("Microsoft.XMLDOM")     
    
   Dim MD_XMLPath    :    MD_XMLPath = GetLogonServer & "\NETLOGON\MWTEST\MappedDrives.xml"
    Dim GroupObj,objDriveNode
 
   'Load the XML file
   
   xmlDoc.async = False
   If objFSO.FileExists(MD_XMLPath) Then
       xmlDoc.Load(MD_XMLPath)
       
       'Compare each group the user is a member of and see if it is found in the XML file
       'If so, retrieve the UNC path and drive latter to be mapped.
       
       For Each GroupObj In objUser.Groups
           Set objDriveNode = xmlDoc.selectSingleNode("//MappedDrives/Group[@Name = '" & Ucase(GroupObj.cn) & "']")
           If Not objDriveNode Is Nothing Then
               Err.Clear                                
               objNetwork.MapNetworkDrive objDriveNode.childNodes(1).text & ":", objDriveNode.childNodes(0).text,True
               If Err.Number <> 0 Then
                   WriteToErrorLog "Error mapping " & objDriveNode.childNodes(1).text & " to " & objDriveNode.childNodes(0).text
               End If
           End If 
       Next
   End If    
 End Sub
 


The format of the XML file is as follows

 <MappedDrives> 
 <Group Name="SERVER_GROUP1">
    <UNCPath>\\MYSERVER\SHARENAME$</UNCPath>
    <DriveLetter>G</DriveLetter>
    </Group>
 </MappedDrives>
 


Each drive mapping requires three nodes

Group, UNCPath and DriveLetter.  The Group node has the attribute "NAME" which is equal to the name of the AD security group associated with the share.  The UNCPath and drive letter nodes are pretty self-explanatory I guess ;)

Anyway, I thought this was a better way of doing things as it makes future management of this part of the login script easy.  All people have to do is amend the XML file accordingly for each share.

I haven't included the WriteToErrorLog sub as it's obviously quite site-specific.  Suffice to say it simply takes the string passed to it and writes it to a text file somewhere.
<message edited by ginolard on Tuesday, September 25, 2007 2:39 AM>
Author of ManagePC - http://managepc.net

 
#1
    dm_4ever

    • Total Posts : 3687
    • Scores: 82
    • Reward points : 0
    • Joined: 6/29/2006
    • Location: Orange County, California
    • Status: offline
    RE: XML-based Mapped Drives script for logon scripts Tuesday, September 25, 2007 3:17 AM (permalink)
    0
    I like the simplicity of it and think an XML file would be a great way to manage drive mappings and avoid having someone break the login script.
    dm_4ever

    My philosophy: K.I.S.S - Keep It Simple Stupid
    Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
    Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      dustervoice

      • Total Posts : 3
      • Scores: 0
      • Reward points : 0
      • Joined: 5/14/2010
      • Status: offline
      Re: XML-based Mapped Drives script for logon scripts Friday, May 14, 2010 11:20 AM (permalink)
      0
      I tried to implement this dont seem to work for me. is lt in the xml file apart of the code or sharename?
      <message edited by dustervoice on Friday, May 14, 2010 11:47 AM>
       
      #3
        ebgreen

        • Total Posts : 8227
        • Scores: 98
        • Reward points : 0
        • Joined: 7/12/2005
        • Status: offline
        Re: XML-based Mapped Drives script for logon scripts Friday, May 14, 2010 11:29 AM (permalink)
        0
        I would suggest posting the details of your problem in the Client Side Scripting area.
        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
        http://www.visualbasicscript.com/m_47117/tm.htm
         
        #4
          ginolard

          • Total Posts : 1347
          • Scores: 23
          • Reward points : 0
          • Joined: 8/11/2005
          • Status: offline
          Re: XML-based Mapped Drives script for logon scripts Tuesday, August 03, 2010 9:29 PM (permalink)
          0
          Here's a small update that allows you to specify multiple drives for a single AD group
           
           Sub MapNetworkDrives 
             
               On Error Resume Next 
                
              'Map group-specific drives based on the entries in the MappedDrives.xml file. 
               
               Dim objNetwork    :    Set objNetwork = CreateObject("WScript.Network") 
               Dim objFSO        :    Set objFSO = CreateObject("Scripting.FileSystemObject") 
               Dim objSysInfo    :    Set objSysInfo = CreateObject("ADSystemInfo") 
               Dim objUser     :    Set objUser = GetObject("LDAP://" & objSysInfo.UserName) 
               Dim XMLDoc        :    Set xmlDoc = CreateObject("Microsoft.XMLDOM")      
                
              Dim MD_XMLPath    :    MD_XMLPath = "PATH TO MAPPEDDRIVES.XML" 
               Dim GroupObj,objDriveNode 
             
              'Load the XML file 
               
              xmlDoc.async = False 
              If objFSO.FileExists(MD_XMLPath) Then 
                  xmlDoc.Load(MD_XMLPath) 
                   
                  'Compare each group the user is a member of and see if it is found in the XML file 
                  'If so, retrieve the UNC path and drive latter to be mapped. 
                   
                  For Each GroupObj In objUser.Groups 
                      Set objDriveNode = xmlDoc.selectSingleNode("//MappedDrives/Group[@Name = '" & Ucase(GroupObj.cn) & "']") 
                      If Not objDriveNode Is Nothing Then 
                          Err.Clear                                 
                           For i = 0 To objDriveNode.ChildNodes.length Step 2 
                               MapNetworkDrive objDriveNode.childNodes(i+1).text, objDriveNode.childNodes(i).text 
                               If Err.Number <> 0 Then 
                                   Writelog("!! Error mapping " & objDriveNode.childNodes(i+1).text & " to " & _ 
                                           objDriveNode.childNodes(i).text & ":" & Err.Description & " !!") 
                               Else 
                                   Writelog("Successfully mapped " & objDriveNode.childNodes(i+1).text & " to " & objDriveNode.childNodes(i).text) 
                               End If 
                           Next 
                      End If  
                  Next 
              End If     
            End Sub 
           


          So, now, the format of the XML file can be

           
            <MappedDrives>  
            <Group Name="SERVER_GROUP1"> 
               <UNCPath>\\MYSERVER\SHARENAME</UNCPath> 
               <DriveLetter>G</DriveLetter> 
               <UNCPath>\\MYSERVER\DIR\DIR2\DIR3</UNCPath> 
               <DriveLetter>X</DriveLetter> 
               </Group> 
            </MappedDrives> 
           



          Author of ManagePC - http://managepc.net

           
          #5
            ginolard

            • Total Posts : 1347
            • Scores: 23
            • Reward points : 0
            • Joined: 8/11/2005
            • Status: offline
            Re: XML-based Mapped Drives script for logon scripts Wednesday, August 04, 2010 8:57 PM (permalink)
            0
            Sorry, slight error in that above script.

            Replace

             For i = 0 To objDriveNode.ChildNodes.length Step 2
             


            with
             For i = 0 To objDriveNode.ChildNodes.length/2 Step 2
             


            Author of ManagePC - http://managepc.net

             
            #6
              Wakawaka

              • Total Posts : 456
              • Scores: 23
              • Reward points : 0
              • Joined: 8/27/2009
              • Status: offline
              Re: XML-based Mapped Drives script for logon scripts Wednesday, August 04, 2010 11:41 PM (permalink)
              0
              Hmmm...why not use attributes?

               <MappedDrives>   
                     <Group Name="SERVER_GROUP1"> 
                        <MapDrive DriveLetter='G'>\\MYSERVER\SHARENAME</MapDrive>  
                        <MapDrive DriveLetter='x'>\\MYSERVER\DIR\DIR2\DIR3</MapDrive>  
                        </Group> 
                     </MappedDrives>


              ... 
                   Set oNodeList = oXMLDocument.selectNodes("MappedDrives/Group[@Name = '" & Ucase(GroupObj.cn) & "']")/MapDrive") 
                   
                   For Each oNode In oNodeList 
                        MapNetworkDrive oNode.GetAttribute("DriveLetter"), oNode.Text 
                   Next


              It isn't tested, but it should work.
               
              You can get rid of the creation of the FileSystemObkject too, assuming it isn't used anywhere else since the .Load method returns a boolean.
               
                   If oXML.Load(sPath) Then     ...     Else     ...     End If

              <message edited by Wakawaka on Wednesday, August 04, 2010 11:44 PM>
               
              #7
                ginolard

                • Total Posts : 1347
                • Scores: 23
                • Reward points : 0
                • Joined: 8/11/2005
                • Status: offline
                Re: XML-based Mapped Drives script for logon scripts Thursday, August 05, 2010 12:21 AM (permalink)
                0
                I prefer to avoid attributes really.  Just a personal preference. 

                Also, I'm doing the same sort of thing for mapping printers and attributes in that file are redundant (as the format is simpler).  This means I'd prefer to keep the format the same for the both XML files so that others can maintain them easily.
                <message edited by ginolard on Thursday, August 05, 2010 12:23 AM>
                Author of ManagePC - http://managepc.net

                 
                #8
                  rainsworth

                  • Total Posts : 8
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 6/15/2008
                  • Status: offline
                  Re: XML-based Mapped Drives script for logon scripts Sunday, September 19, 2010 1:03 PM (permalink)
                  0
                  Can anyone explain to me what the advantages of using an xml file for drive/printer mappings over having them contained within one vb script would be? 
                   
                  #9
                    ginolard

                    • Total Posts : 1347
                    • Scores: 23
                    • Reward points : 0
                    • Joined: 8/11/2005
                    • Status: offline
                    Re: XML-based Mapped Drives script for logon scripts Monday, September 20, 2010 1:44 AM (permalink)
                    0
                    No need to change the login script when file servers change names

                    Portability.  I've used the script in 3 different sites now with minimal changes

                    Using XML is cool ;)
                    Author of ManagePC - http://managepc.net

                     
                    #10
                      Bird_FAT

                      • Total Posts : 1
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 3/4/2011
                      • Status: offline
                      Help with your XML-based Mapped Drives script for logon scripts Tuesday, March 08, 2011 1:13 AM (permalink)
                      0
                      Hello there Ginolard,
                       
                      I was creating a much longer script, but was thinking that it would be better calling an external map list for ease of editing, when I came across this one of yours - so simple and clean looking!

                      But I can't seem to get your script to work for me!!
                      I'm trying to run it on a test machine that is part of the domain, and I can ping the Server I'm trying to map to. I have placed the vb script and the XML in the root of the E: drive for testing.
                      From your original script to call and map, I have changed the'PATH TO MAPPEDDRIVES.XML' to 'E:\MappedDrives.xml'
                      But, when I run the vbs, it doesn't do anything!

                      The xml I have is (no matter what I seem to do, the code here seems to go on one line, sorry??):
                      <MappedDrives> <Group Name="Accounts"> <UNCPath>\\UK-DATA\Forms</UNCPath> <DriveLetter>R</DriveLetter> <UNCPath>\\UK-DATA\General</UNCPath> <DriveLetter>S</DriveLetter> >>etc for more drives here<<   </Group><Group Name="Technical"> >>etc for more drives here<< </MappedDrives> 


                      There are four groups in total, and here is a full AD description of one of the users:
                      CN='USER NAME',OU=Technical,OU=Users_Computers,DC='domain'
                       
                      I'm using 2003/2008 R2 servers with Windows XP, Vista and 7 (mainly 7)
                      So, my 3 questions are:
                      1. What am I doing wrong in the first place?
                      2. If I have certain drives that need to be mapped to every user, how would I add this into the script/xml
                      3. I have some PCs that seem to have an issue with persistent drives - is it possible to remove all mapping, before adding it in again?

                       
                      #11
                        ginolard

                        • Total Posts : 1347
                        • Scores: 23
                        • Reward points : 0
                        • Joined: 8/11/2005
                        • Status: offline
                        Re:Help with your XML-based Mapped Drives script for logon scripts Tuesday, March 08, 2011 9:43 PM (permalink)
                        0
                        1) Hard to say.  Comment out the On Error Resume Next and see what error pops up
                        2) You could specify a group that every user is a member of in the XML file (e.g. Domain Users)
                        3) Yup, the RemoveNetworkDrive method
                        Author of ManagePC - http://managepc.net

                         
                        #12
                          erratum

                          • Total Posts : 33
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 2/23/2010
                          • Status: offline
                          Re:XML-based Mapped Drives script for logon scripts Thursday, August 04, 2011 10:14 PM (permalink)
                          0
                          Hey there,
                           
                          I've build such a VBS-XML-Based logonscript for my company these days. It works really great, with one exception.
                           
                          It won't map administrative releases (these ones with a "$" -> dollarsign at the end). Is ignoring such strings typical for XML or what's goin' on there?
                           
                          Looking forward to you helpfull replies :)
                           
                          wbr erratum
                          (if code is needed I'll post it, but atm I think the problem is more general. Isn't it?)
                           
                          #13
                            erratum

                            • Total Posts : 33
                            • Scores: 0
                            • Reward points : 0
                            • Joined: 2/23/2010
                            • Status: offline
                            Re:Help with your XML-based Mapped Drives script for logon scripts Sunday, August 07, 2011 5:01 PM (permalink)
                            0
                            Okay, Problem is not the Dollar-Sign. But the Problem is still there. By using the following code
                            public function fct_specialmappings  
                             v_username = Ucase([Username])  
                             Set obj_node = obj_XMLDoc.SelectSingleNode("//root/SpecialMappings/user[@name='" & v_username & "']")  
                             
                             If not obj_node is nothing then  
                             wscript.echo obj_node.childnodes.length  
                             For h_i = 0 to obj_node.childnodes.length/2 Step 2  
                             on error resume next  
                             if exists(obj_node.childnodes(h_i).text & ":") = true then Obj_Network.RemoveNetworkDrive obj_node.childnodes(h_i).text & ":", True, True  
                             Obj_Network.MapNetworkDrive obj_node.childnodes(h_i+1).text & ":", obj_node.childnodes(h_i).text  
                             on error goto 0  
                             next  
                             Set obj_node = nothing  
                             end if  
                             end function

                            And the xml:
                            ... <user name=[USERNAME]>  
                             <UNCPath>UNC1</UNCPath>  
                             <DriveLetter>W</DriveLetter>  
                             <UNCPath>UNC2</UNCPath>  
                             <DriveLetter>X</DriveLetter>  
                             <UNCPath>UNC3</UNCPath>  
                             <DriveLetter>Y</DriveLetter>  
                             <UNCPath>\UNC4</UNCPath>  
                             <DriveLetter>Z</DriveLetter>  
                             <UNCPath>UNC5</UNCPath>  
                             <DriveLetter>V</DriveLetter>  
                             </user> ...


                            it just gives me the first 3 mappings. Any idea why the script is behaving like that? Btw. it's not relevant in what order I put the mappings in, it's everytime just the first 3 mappings...

                            EDITH: SOLVED!
                            Problem was the line:
                            For h_i = 0 to obj_node.childnodes.length/2 Step 2

                            Having 5 Networkdrives to be mapped the length is 10, devided by 2 is 5. As h_i is raised by 2 each time it never goes over 4.  Hoping, that this works I tried the following working code:
                            For h_i = 0 to obj_node.childnodes.length-1 Step 2
                            ...

                            @Gino:
                            For what reason did you put in the "devided by 2" statement?

                            wbr Erratum
                            <message edited by erratum on Sunday, August 07, 2011 6:34 PM>
                             
                            #14

                              Online Bookmarks Sharing: Share/Bookmark

                              Jump to:

                              Current active users

                              There are 0 members and 1 guests.

                              Icon Legend and Permission

                              • New Messages
                              • No New Messages
                              • Hot Topic w/ New Messages
                              • Hot Topic w/o New Messages
                              • Locked w/ New Messages
                              • Locked w/o New Messages
                              • Read Message
                              • Post New Thread
                              • Reply to message
                              • Post New Poll
                              • Submit Vote
                              • Post reward post
                              • Delete my own posts
                              • Delete my own threads
                              • Rate post

                              2000-2012 ASPPlayground.NET Forum Version 3.9