Logon script mapping drives according to computer name

Author Message
dhalvorsen

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 11/17/2011
  • Status: offline
Logon script mapping drives according to computer name Thursday, November 17, 2011 7:17 AM (permalink)
0
I am trying to create a script to map drives for employees that go to various buildings.  The computer names all start with 2 letters that identify which building it is at.  I want the script to take those to letters and assign it a batch file to map the drives at that building.
 
Below are examples of things I have tried to test just to map a drive based on computer name, but no matter what the computer name is, it always chooses the first choice.  There were some other scripts I tried too, but all failed.
------------------------------------------------------------------------------
@ECHO OFF
IF /I "%computername%" == "AB*" THEN
NET USE G: \\AB\Software
IF /I "%computername%" == "CD*" THEN
NET USE G: \\CD\Software
END IF
------------------------------------------------------------------------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim computerName
computerName = LCase(WshNetwork.ComputerName)
Next
Select Case (computerName)
 Case "AB*"
  NET USE G: \\AB\SOFTWARE
 Case "CD*"
  NET USE G: \\CD\SOFTWARE
 Case Else
  Do nothing
End Select
------------------------------------------------------------------------------
 
#1
    59cobalt

    • Total Posts : 975
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Logon script mapping drives according to computer name Thursday, November 17, 2011 9:11 AM (permalink)
    0
    Please decide what you want to use: batch OR VBScript. Mixing the two simply WON'T WORK.

    dhalvorsen
    @ECHO OFF
    IF /I "%computername%" == "AB*" THEN
    NET USE G: \\AB\Software
    IF /I "%computername%" == "CD*" THEN
    NET USE G: \\CD\Software
    END IF
    "if" statements in batch have neither a "then" nor an "end if" keyword. They also don't support comparisons with wildcards. See "help if" for specifics.
    net use G: \\%COMPUTERNAME:~0,2%\Software

    dhalvorsen
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Dim computerName
    computerName = LCase(WshNetwork.ComputerName)
    Next
    Select Case (computerName)
     Case "AB*"
     NET USE G: \\AB\SOFTWARE
     Case "CD*"
     NET USE G: \\CD\SOFTWARE
     Case Else
     Do nothing
    End Select
    VBScript does not support direct execution of external commands. You need the Run() or the Exec() method for that.
    Set sh = CreateObject("WScript.Shell")
    Set net = CreateObject("WScript.Network")
    sh.Run "%COMSPEC% /c net use G: \\" & Left(net.ComputerName, 2) & "\Software"
    Or, you could do it the VBScript way (since you're using VBScript anyway).
    Set net = CreateObject("WScript.Network")
    net.MapNetworkDrive "G:", "\\" & Left(net.ComputerName, 2) & "\Software"

     
    #2
      dhalvorsen

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 11/17/2011
      • Status: offline
      Re:Logon script mapping drives according to computer name Thursday, November 17, 2011 9:24 AM (permalink)
      0
      I can't say I know a lot about vb scripts, but I was trying to follow examples I found.  The examples I put probably were not the best either.  If I cannot use a vbs to execute a batch file, then I can create the drive mappings in the vbs.
       
      The computer names are created with a building identifier and then point to a server that is not the same.
      Say computer "AB-T113" would map a drive to "\\BCD\Software".  I am working with 8 different buildings and each one would have about 6-7 mapped drives unique to that building.
       
      It is hard to find examples of scripts for my purpose and had been trying to base it off of someone's printer scripts.  Right now we have an exe that was built with Symmantec AI builder and then it executes the batch file from a remote server to map their drives.  I would rather have a vbs in it's place.
       
      #3
        59cobalt

        • Total Posts : 975
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Logon script mapping drives according to computer name Thursday, November 17, 2011 9:37 AM (permalink)
        0
        dhalvorsen
        The computer names are created with a building identifier and then point to a server that is not the same.
        Say computer "AB-T113" would map a drive to "\\BCD\Software".
        Set bldgServers = CreateObject("Scripting.Dictionary")
        bldgServers.Add "ab", "bcd"
        bldgServers.Add "cd", "efg"
        bldgServers.Add ...
        
        Set net = CreateObject("WScript.Network")
        net.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(net.ComputerName, 2))) & "\Software"
        net.MapNetworkDrive ...

         
        #4
          dhalvorsen

          • Total Posts : 5
          • Scores: 0
          • Reward points : 0
          • Joined: 11/17/2011
          • Status: offline
          Re:Logon script mapping drives according to computer name Friday, November 18, 2011 4:50 AM (permalink)
          0
          Is there something I'm missing?  I cannot make this work.
           
          Actual script (which returns error on line 5 the network name cannot be found):
          Set bldgServers = CreateObject("Scripting.Dictionary")
          bldgServers.Add "HS", "HSA"
           
          Set net = CreateObject("WScript.Network")
          net.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(net.ComputerName, 2))) & "\Software"
           
           
          I have also tried changing net to objNetwork which doesn't return an error, but also does not map the network drive.
           
          Set bldgServers = CreateObject("Scripting.Dictionary")
          bldgServers.Add "HS", "HSA"
           
          Set objNetwork = CreateObject("WScript.Network")
          objNetwork.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(ComputerName, 2))) & "\Software"
           
          #5
            ebgreen

            • Total Posts : 8227
            • Scores: 98
            • Reward points : 0
            • Joined: 7/12/2005
            • Status: offline
            Re:Logon script mapping drives according to computer name Friday, November 18, 2011 6:17 AM (permalink)
            0
            Change it to this to make troubleshooting easier:
             
            Set objNetwork = CreateObject("WScript.Network")
            strShareName = "\\" & bldgServers(LCase(Left(ComputerName, 2))) & "\Software"
            WScript.Echo strShareName
            objNetwork.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(ComputerName, 2))) & "\Software"
             
            See if you are building a valid string.
            "... 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
             
            #6
              ebgreen

              • Total Posts : 8227
              • Scores: 98
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              Re:Logon script mapping drives according to computer name Friday, November 18, 2011 6:18 AM (permalink)
              0
              Although I can tell you that you aren't. I'll see if you can figure it out though. I will give you a hint that you will never find the left two letters of the machine name in the dictionary as you have it now.
              "... 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
               
              #7
                dhalvorsen

                • Total Posts : 5
                • Scores: 0
                • Reward points : 0
                • Joined: 11/17/2011
                • Status: offline
                Re:Logon script mapping drives according to computer name Friday, November 18, 2011 7:01 AM (permalink)
                0
                Comes back with a \\\Software and the network name cannot be found.  I know very little when it comes to scripts.  The first posts should show that when I was combining vbs with batch.  Thanks for the help.
                 
                #8
                  ebgreen

                  • Total Posts : 8227
                  • Scores: 98
                  • Reward points : 0
                  • Joined: 7/12/2005
                  • Status: offline
                  Re:Logon script mapping drives according to computer name Friday, November 18, 2011 7:12 AM (permalink)
                  0
                  So read the documentation for the functions that you are using and understand them. Otherwise you will get this working and still not understand VBScript. Specifically read how Dictionaries work and how they match things. Next read the documentation for LCase.
                  "... 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
                   
                  #9
                    dhalvorsen

                    • Total Posts : 5
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 11/17/2011
                    • Status: offline
                    Re:Logon script mapping drives according to computer name Friday, November 18, 2011 8:38 AM (permalink)
                    0
                    Why would this need LCase if all it does is make everything lowercase?  Both dictionaries and lcase are easy to understand, the only trick really is pulling the first 2 letters of the computer name.  Could this be accomplished with an if statement and computername?

                    I actually found another solution.  Batch file makes things a whole lot simpler for my use.
                    SET _prefix=%COMPUTERNAME:~0,2%
                    IF %_prefix%==AL "\\AL\batch.bat"
                     
                    Thanks for the assistance.
                    <message edited by dhalvorsen on Friday, November 18, 2011 9:45 AM>
                     
                    #10
                      59cobalt

                      • Total Posts : 975
                      • Scores: 91
                      • Reward points : 0
                      • Joined: 7/17/2011
                      • Status: offline
                      Re:Logon script mapping drives according to computer name Friday, November 18, 2011 10:33 AM (permalink)
                      0
                      Your code:
                      Set objNetwork = CreateObject("WScript.Network")
                      objNetwork.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(ComputerName, 2))) & "\Software"

                      My code:
                      Set net = CreateObject("WScript.Network")
                      net.MapNetworkDrive "G:", "\\" & bldgServers(LCase(Left(net.ComputerName, 2))) & "\Software"

                      Now rename "objNetwork" to "net" in your code and see if you can spot any remaining differences between the two code snippets.
                       
                      #11
                        ebgreen

                        • Total Posts : 8227
                        • Scores: 98
                        • Reward points : 0
                        • Joined: 7/12/2005
                        • Status: offline
                        Re:Logon script mapping drives according to computer name Monday, November 21, 2011 3:46 AM (permalink)
                        0
                        You definitely need either LCase or UCase. You have no way to know what case the machine name will be presented with so you use LCase or UCase to be sure.
                        "... 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
                         
                        #12
                          59cobalt

                          • Total Posts : 975
                          • Scores: 91
                          • Reward points : 0
                          • Joined: 7/17/2011
                          • Status: offline
                          Re:Logon script mapping drives according to computer name Monday, November 21, 2011 8:41 AM (permalink)
                          0
                          There's another way: set the .CompareMode property of the dictionary to vbTextCompare, and the checks will become case-insensitive. However, that property is rather poorly documented, and I find the approach with explicit casing easier to understand. Particularly for someone who is unfamiliar with VBScript.
                           
                          #13
                            ebgreen

                            • Total Posts : 8227
                            • Scores: 98
                            • Reward points : 0
                            • Joined: 7/12/2005
                            • Status: offline
                            Re:Logon script mapping drives according to computer name Tuesday, November 22, 2011 3:28 AM (permalink)
                            0
                            Agreed. There is also the other method of putting every possible case combination in the dictionary. That is a stupid way to do it though
                            "... 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
                             
                            #14
                              59cobalt

                              • Total Posts : 975
                              • Scores: 91
                              • Reward points : 0
                              • Joined: 7/17/2011
                              • Status: offline
                              Re:Logon script mapping drives according to computer name Tuesday, November 22, 2011 11:32 AM (permalink)
                               
                              #15
                                ebgreen

                                • Total Posts : 8227
                                • Scores: 98
                                • Reward points : 0
                                • Joined: 7/12/2005
                                • Status: offline
                                Re:Logon script mapping drives according to computer name Wednesday, November 23, 2011 3:14 AM (permalink)
                                0
                                I love the daily WTF.
                                "... 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
                                 
                                #16

                                  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