Announcement

Collapse
No announcement yet.

Net drives map bat/vb script

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

  • Net drives map bat/vb script

    I am new to bat/vb scripting and need to create a file which will restore all network drives for my users. However, many of them utilize different mapped drives so a simple bat script with net use assigning static drives is not applicable. What I would like to do, and know is possible, is to have a script that will iterate through all current mapped drives and then when it is run again, re-map those drives. The only thing I am unsure how to do is to "record" the current mapped drives. Any suggestions?

    I am sure this is basic but googling provided nothing and I do not have much in the way of other resources yet.

    Thanks.

  • #2
    Re: Net drives map bat/vb script

    This IMO is a BAD way of doing it. Assign AD Security groups to the shared areas, set permissions if required, and add users to the groups they need to map drives for.

    Then, create a CMD file as a login script to map the drives each time they log in:

    usage: ifmember [/verbose][/list]groupname ...
    /verbose will print out all matches.
    /list will print out all groups user is a member of.
    Return Code shows number of groups this user is a member of.
    Example: ifmember /v /l "MyDomain\Domain Users" Users Everyone

    SO:

    Code:
    ifmember "mapdriveA"
    if not errorlevel 1 goto check2
    net use Z: \\server\driveA /persistent:no
    
    :check2
    ifmember "mapdriveB"
    if not errorlevel 1 goto end
    net use Y: \\server\driveB /persistent:no
    
    :end
    exit


    Tom
    For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

    Anything you say will be misquoted and used against you

    Comment


    • #3
      Re: Net drives map bat/vb script

      Originally posted by Stonelaughter View Post
      This IMO is a BAD way of doing it. Assign AD Security groups to the shared areas, set permissions if required, and add users to the groups they need to map drives for.
      Agreed.

      On the coding, we can get rid of those darn GOTO's like this:

      ifmember "mapdriveA"
      if errorlevel 1 (
      net use Z: \\server\driveA /persistent:no
      )

      ifmember "mapdriveB"
      if errorlevel 1 (
      net use Y: \\server\driveB /persistent:no
      )
      or like this:

      ifmember "mapdriveA"
      if errorlevel 1 net use Z: \\server\driveA /persistent:no

      ifmember "mapdriveB"
      if errorlevel 1 net use Y: \\server\driveB /persistent:no
      I have found IFMEMBER to be unsatisfactory (buggy) under certain situations so I have replaced it with "NET USER":

      net user %USERNAME% /domain | find /I "*mapdriveA" >nul
      if not errorlevel 1 ...
      Note the /domain switch for domain groups. Omit the switch for local machine membership.

      Also note the "IF" logic is the inverse of IFMEMBER as FIND returns an ERRORLEVEL of 0 if successful while a 0 for IFMEMBER effectively means "not a member".

      Finally, you'll see I prefix the group with an "*". Examine the output of NET USER and I believe you'll see why I do this -- just in case a group name is a subset of any of the rest of the text.
      Cheers,

      Rick

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

      2006-2099 R Valstar. This post is offered "as is" for discussion purposes only with no express or implied warranty of any kind including, but not limited to, correctness or fitness for use. Nothing herein shall be construed as advice. Attempting any activity based on information in this post is done at your own risk.

      Comment


      • #4
        Re: Net drives map bat/vb script

        Originally posted by Stonelaughter View Post
        Assign AD Security groups
        There should be an AD available. But the OP didn't say anything about it. Nor anything about the computers.
        Why are the mapping disappearing? Shouldn't /persistent:YES be used?
        I'm not feeling comfortable with the info we got so far. Some questions should be asked:
        - what does mean "different mapped drives"? That two users have the T:\ drive mapped to two different shares? Or one has T:\ and R:\ and the other one has J:\ and S:\?
        - why are the drives disconnecting? If you double click on a mapped drive that is disconnected, it should reconnect (if you have the /persistent:YES parameter).
        In a lot of cases, including in my daily work, I encounter people asking for a specific solution, without telling what's the problem. It doesn't always work...
        So sorry for being skeptic all the time...

        Sorin Solomon


        In order to succeed, your desire for success should be greater than your fear of failure.
        -

        Comment


        • #5
          Re: Net drives map bat/vb script

          Hah! Sorin, you're probably wiser than most of us


          Tom
          For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

          Anything you say will be misquoted and used against you

          Comment


          • #6
            Re: Net drives map bat/vb script


            Tom: it's not always a good thing, you know. But it's in my character. I hope you all will have patience with me on this...
            And I still don't know to which half to believe

            Sorin Solomon


            In order to succeed, your desire for success should be greater than your fear of failure.
            -

            Comment


            • #7
              Re: Net drives map bat/vb script

              Sorry, I hadn't had any coffee yet.

              We are running AD. It should be noted that while I can manage user permissions, I cannot create groups without going through quite a bit of bureaucracy. I am managing several departments which have common drives between all of them, but also have dept. specific drives which share the same letter. So for example, everyone shares a common T, O and U drives; however, there are 3 different L and Z drives that are accessed by users in different groups. There are also a handful of users who move between each department. The drives are disconnecting because it is a remote site and unfortunately a lot of the work is being done via citrix, and when a session hangs or is abruptly disconnected it causes issues with the drives. In addition, some of our users are connecting via VPN and thus the logon script is not mapping all network drives when the PC is booted up off of the network.

              I am certainly open to suggestions as to other ways to make this painless for the end-users. I just figured that a bat file that they can run which will always have the most current network drives would be the easiest solution.

              Comment


              • #8
                Re: Net drives map bat/vb script

                Thing is, if you map the drive (using a command) with the switch "/persistent:yes", then the drive will ATTEMPT to map at each logon, and will be displayed in Explorer with a red "X" through it if it's not possible to connect. If the network comes back during that session, clicking the drive in Explorer will reconnect you as long as your credentials have not expired (thus also logging you on to the Domain proper).

                The same can be achieved while mapping the drive via the GUI by selecting "Reconnect at Logon" in the "Map Network Drive" dialog.


                Tom
                For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

                Anything you say will be misquoted and used against you

                Comment


                • #9
                  Re: Net drives map bat/vb script

                  Originally posted by Stonelaughter View Post
                  Thing is, if you map the drive (using a command) with the switch "/persistent:yes", then the drive will ATTEMPT to map at each logon, and will be displayed in Explorer with a red "X" through it if it's not possible to connect. If the network comes back during that session, clicking the drive in Explorer will reconnect you as long as your credentials have not expired (thus also logging you on to the Domain proper).

                  The same can be achieved while mapping the drive via the GUI by selecting "Reconnect at Logon" in the "Map Network Drive" dialog.
                  I am aware of this- for some reason the drives are not being reconnected in the citrix session without kicking the session or manually disconnecting and reconnecting the drive.

                  Comment


                  • #10
                    Re: Net drives map bat/vb script

                    damnlamb, it is obvious you need to give your users a tool to revive their mapped drives.
                    rvalstar's code should solve you the problem (btw, great code, Rick!). Just save it as CMD file, not BAT.
                    Good luck and keep us posted.

                    Sorin Solomon


                    In order to succeed, your desire for success should be greater than your fear of failure.
                    -

                    Comment


                    • #11
                      Re: Net drives map bat/vb script

                      I agree- and I appreciate everyone's help thusfar. Rvalstar's code has certainly given me some ideas, however, I cannot create groups without filling out paperwork and submitting it overseas. Can I ask as to why my idea is a bad way of doing this? I also should have mentioned that I cannot modify the existing logon scripts. Again, thanks.

                      Comment


                      • #12
                        Re: Net drives map bat/vb script

                        It's a bad idea (a) because it's a horrible task to "get" the currently mapped drives, and (b) because the user can map drives manually and get all sorts of stuff going on... it's just messy and not a managed solution.


                        Tom
                        For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

                        Anything you say will be misquoted and used against you

                        Comment


                        • #13
                          Re: Net drives map bat/vb script

                          Originally posted by Stonelaughter View Post
                          It's a bad idea (a) because it's a horrible task to "get" the currently mapped drives, and (b) because the user can map drives manually and get all sorts of stuff going on... it's just messy and not a managed solution.
                          Fair enough, though frankly none of my users actually know how to map a drive- I agree that it is not a managed solution. Since I cannot modify logon scripts, I will simply create 3 seperate bat files, one for each department. However, if anyone knows how I would go about "getting" the current mapped drives I would be interested simply as a scripting novice.

                          Comment


                          • #14
                            Re: Net drives map bat/vb script

                            Originally posted by damnlamb View Post
                            how I would go about "getting" the current mapped drives
                            That's not a difficult task. By running net use (without parameters), you get the mapped drives' list.
                            The rest is of your choice.
                            You can run
                            Code:
                            net use >> c:\mapped.txt
                            to get the results into a txt file. Or write a CMD file that contains
                            Code:
                            echo ************ >> [shared drive]:\mapped.txt
                            echo %USERNAME% >> [shared drive]:\mapped.txt
                            net use >> [shared drive]:\mapped.txt
                            echo ************ >> [shared drive]:\mapped.txt
                            and run it from every user. This way, you will get a list of all drives mapped by all users, including their username. The *** are delimiters (since the list will be pretty havoc). The [shared drive] should be one of the drives everyone maps (T, O or U), but with Write permissions.
                            Hope that this is what you were looking for.
                            Last edited by sorinso; 23rd January 2007, 20:38.

                            Sorin Solomon


                            In order to succeed, your desire for success should be greater than your fear of failure.
                            -

                            Comment


                            • #15
                              Re: Net drives map bat/vb script

                              Thanks Sorin, actually I was familiar with that method and was looking to pipe the results as individual variables which could be further manipulated.

                              Thanks for all the help, however, I have come to an acceptable solution.

                              Comment

                              Working...
                              X