Announcement

Collapse
No announcement yet.

Need a Script to Map and Unmap Printers

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

  • Need a Script to Map and Unmap Printers

    Hello all.

    I need a script that can unmap and map printers based on AD computer group membership.

    I have various users, and they're members of department groups.
    There are lots of computers, and a few laptops, I've started creating groups in AD for the computers based on "Department Name" PCs and adding them in there.

    I figured, the script needs to look if the computer is not in a group, unmap these printers, if it is in the group, map the printers.

    Then proceed down the list of Department PCs.

    I have one other question though, what happens if this is the second time they've logged on and the printer is already mapped from the last login, will it throw up and error or just leave it mapped?

    I've looked around, and there's various different .bat or .vbs files, some require additional .exe files, I need something pretty simple, works in a login script in Group Policy.

    The one or two I've tried haven't worked, nothing is mapped at all.

    Can someone help me out, thanks.

  • #2
    Re: Need a Script to Map and Unmap Printers

    Have you searched through this forum? I know something close, if not spot on, has been handled before most probably by our scripting guru REMS or one of our other capable contributors.

    Would be helpful if you found something in range and then came back w/ specific questions to tailor it to your needs. Otherwise, I'm certain there are capable scripters in your region that can be hired to do all your work in an hourly, retainer, or lump sum turn key manner.
    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


    • #3
      Re: Need a Script to Map and Unmap Printers

      This is the last one I tried, didn't work, it complains about Line 5 Char 1, which as far as I can see is the "S" of set ??

      I've already customised it for my OU structure.

      Code:
      Dim objNetwork, objComputer, strDomain, strComputerName
      strDomain = "MyDomain"
      Set objNetwork = CreateObject("Wscript.Network")
      strComputerName = objNetwork.ComputerName
      Set objComputer = GetObject("WinNT://" & strDomain & "/Devices/Computers/Workstations/" & strComputerName & ",computer")
      If IsGroupMember(strDomain, "IT PCs") Then
          objNetwork.AddPrinterConnection "\\GVS-PS1\HP2550L"
          objNetwork.SetDefaultPrinter "\\GVS-PS1\HP2550L"
      End If
      Set objNetwork = Nothing
      Set objComputer = Nothing
      Wscript.Quit
      
      Function IsGroupMember(strDomain, strGroup)
          Dim objGroup
          Set objGroup = GetObject("WinNT://" & strDomain & "/Devices/Computers/Groups/" & strGroup & ",group")
          If objGroup.IsMember(objComputer.ADsPath & "$") Then
              IsGroupMember = True
          Else
              IsGroupMember = False
          End If
          Set objGroup = Nothing
      End Function

      Comment


      • #4
        Re: Need a Script to Map and Unmap Printers

        Originally posted by AJStevens
        This is the last one I tried, didn't work, it complains about Line 5 Char 1, which as far as I can see is the "S" of set ??
        I've already customised it for my OU structure.
        Most likely though that an incorrect path or object Name was typed??
        Anyways, the script below will work - and is friendlier to use (I think).


        Originally posted by AJStevens
        I have one other question though, what happens if this is the second time they've logged on and the printer is already mapped from the last login, will it throw up and error or just leave it mapped?
        Yes, it will process the line at every logon - but it just leave this, and any other existing printer connection mapped - w/out messages when re-mapping.


        If you want to be able to change the printer connections for a user (including to disconnect printers), there are two methods you could use;

        *1. Using the Win32_Printer WMI class. Example:
        Code:
        '// Delete all previous connected networkprinters
         Dim strComputer, objWMIService, colInstalledPrinters, objPrinter
          strComputer = "."
          Set objWMIService = GetObject("winmgmts:\\" & strComputer & _
            "\root\cimv2")
          Set colInstalledPrinters =  objWMIService.ExecQuery _
            ("Select * from Win32_Printer Where Network = TRUE")
          For Each objPrinter in colInstalledPrinters
            objPrinter.Delete_
          Next
          Set objWMIService = Nothing
          Set colInstalledPrinters = Nothing
        
         'next is to (re-)connect the right printers for the user
        *2. Using the WshNetwork object. Example:
        Code:
        '// Remove the specified printerconnection if not match the condition
        'syntax:
        ' object.AddWindowsPrinterConnection PrinterPath
        ' object.RemovePrinterConnection PrinterPath, [bForce], [bUpdateProfile]
        ' object.SetDefaultPrinter PrinterPath
        
        Set WshNetwork = WScript.CreateObject("WScript.Network")
        
        strPrinterPath = "\\PrintServer\printer1"
         If condition=True Then
            WshNetwork.AddWindowsPrinterConnection strPrinterPath
            WshNetwork.SetDefaultPrinter strPrinterPath
         Else
            WshNetwork.RemovePrinterConnection strPrinterPath, true, true
         End If
        The problem with the second method is that this function will give Error messages when the printer connection does not exist. To avoid error messages, you can start the script with the line: On Error Resume Next



        Originally posted by AJStevens
        I need a script that can unmap and map printers based on AD computer group membership.
        This script search a certain Group for the current username (objUser), OR it can check whether the computername (objComputer) is on the members list.
        Code:
        '// Bind to the user and computer objects with the LDAP provider.
         Dim objGroupList : 
          With CreateObject("ADSystemInfo")
           Set objUser     = GetObject("LDAP://" & .UserName)
           Set objComputer = GetObject("LDAP://" & .ComputerName)
          End With
        
        ' Add networkprinter connections
        Set WshNetwork = CreateObject("Wscript.Network")
        
        strPrinterPath = "\\PrintServer\printer1"
         If IsMember(objComputer, "ComputerGroup-xxx")  Then
            WshNetwork.AddWindowsPrinterConnection strPrinterPath
         Else
           DeletePrinterObj(strPrinterPath)  ' This Sub_routine is using the Win32_Printer  WMI class (see method 1 )
         End If
        
        strPrinterPath = "\\PrintServer\printer2"
         If IsMember(objUser, "UserGroup-yyy") Then
            WshNetwork.AddWindowsPrinterConnection strPrinterPath
         Else
           DeletePrinterObj(strPrinterPath)
         End If
        
        ' Clean up.
        Set objGroupList = Nothing
        Set objUser = Nothing
        Set objComputer = Nothing
        Set WshNetwork = Nothing
        
        
        Wscript.quit
        '  ~ ~ ~
        
        
        '------------------------------------------
        Function IsMember(ByVal objADObject, ByVal strGroup)
            '(from: http://www.rlmueller.net/freecode2.htm : Logon Script 3)
            ' Function to test for group membership.
            ' objGroupList is a dictionary object with global scope.
            If (IsEmpty(objGroupList) = True) Then 
                 Set objGroupList = CreateObject("Scripting.Dictionary")
            End If
            If (objGroupList.Exists(objADObject.sAMAccountName & "\") _
                 = False) Then
                Call LoadGroups(objADObject, objADObject)
                objGroupList.Add objADObject.sAMAccountName & "\", True
            End If
            IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
                & strGroup)
        End Function
        
        Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
            ' Recursive subroutine to populate dictionary object objGroupList.
            Dim colstrGroups, objGroup, j
        
            objGroupList.CompareMode = vbTextCompare
            colstrGroups = objADSubObject.memberOf
            If (IsEmpty(colstrGroups) = True) Then
                Exit Sub
            End If
            If (TypeName(colstrGroups) = "String") Then
                Set objGroup = GetObject("LDAP://" & colstrGroups)
                If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                        & objGroup.sAMAccountName) = False) Then
                    objGroupList.Add objPriObject.sAMAccountName & "\" _
                        & objGroup.sAMAccountName, True
                    Call LoadGroups(objPriObject, objGroup)
                End If
                Set objGroup = Nothing
                Exit Sub
            End If
            For j = 0 To UBound(colstrGroups)
                Set objGroup = GetObject("LDAP://" & colstrGroups(j))
                If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                        & objGroup.sAMAccountName) = False) Then
                    objGroupList.Add objPriObject.sAMAccountName & "\" _
                        & objGroup.sAMAccountName, True
                    Call LoadGroups(objPriObject, objGroup)
                End If
            Next
            Set objGroup = Nothing
        End Sub
        '------------------------------------------
        
        Sub DeletePrinterObj(strPrinterPath)
          strComputer = WScript.CreateObject("WScript.Network").ComputerName
          With GetObject("winmgmts:\\" & strComputer & _
            "\root\cimv2")
          Set colInstalledPrinters = .ExecQuery _
            ("Select * from Win32_Printer Where Network = TRUE")
          For Each objPrinter in colInstalledPrinters
            If strPrinterPath = objPrinter.ServerName & _
                "\" &objPrinter.shareName Then
            objPrinter.Delete_
            Exit For
            End If
          Next
          End With
          Set colInstalledPrinters = Nothing
        End Sub
        '------------------------------------------
        Test and choose the best method to remove printerconnections.

        \Rems
        Last edited by Rems; 9th July 2007, 21:51. Reason: typo

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Need a Script to Map and Unmap Printers

          Rems, thank you soo much, I understand the first parts, but the functions below are some nice coding, take me a while to fathem that.

          I forgot one other thing though, which I'm trying to do myself, but unsure of value/syntax.

          Basically, I've entered all the printers for two of the departments (two printers per deaprtment), now I could be lazy and just pick one printer per department to be the default printer, which would re-apply at every login I'm presuming.

          Problem is, the Customer Services have two printers, one at each end of the room, some will be closer to one, and others closer to the other. You just know they'll complain if everytime they login they have to reset their default printer, or they'll call us to do it.

          I'm thinking another If statement per department after the mapping of printers, that if the computer is a member of that department group, then check if the default printer is already set as one of it's printers (each department will have at least 2, possibly more printers) then to do nothing, otherwise then set the default printer as the primary printer for the department.

          Then, after a user logs in, they can change their default printer, and it will not change it, unless they login on a PC in a different department (unlikely, but worth factoring in). This should create a pretty automonous printer setup.

          Code:
           If IsMember(objComputer, "IT PCs")  Then
            strPrinterPath1 = "\\PrintServer\HP2550L"
            strPrinterPath2 = "\\PrintServer\HP1320N"
            If DefaultPrinter strPrinterPath1  Then
            ElseIf DefaultPrinter strPrinterPath2 Then
            Else
            WshNetwork.SetDefaultPrinter PrinterPath1
            End If
           End If
          For departments with more printers, I can add additional strPrinterPath#'s and ElseIf's.

          What do I use for DefaultPrinter? Is this the right way, or would you recommend something else?
          Last edited by AJStevens; 10th July 2007, 10:08.

          Comment


          • #6
            Re: Need a Script to Map and Unmap Printers

            Originally posted by AJStevens View Post
            For departments with more printers, I can add additional strPrinterPath#'s and ElseIf's.
            Code:
             If IsMember(objComputer, "IT PCs")  Then
              strPrinterPath1 = "\\PrintServer\HP2550L"
              strPrinterPath2 = "\\PrintServer\HP1320N"
              If DefaultPrinter strPrinterPath1  Then
              ElseIf DefaultPrinter strPrinterPath2 Then
              Else
              WshNetwork.SetDefaultPrinter PrinterPath1
              End If
             End If
            What do I use for DefaultPrinter? Is this the right way, or would you recommend something else?
            It seems like is a good idea to me.

            Here is an example how to determine the present default printer (added an extra function to the script):
            Code:
            '// Bind to the user and computer objects with the LDAP provider
            '// (do not make changes to the next lines. The ADObjects will be used
            '// as input for the 'IsMember function')
             Dim objGroupList :  
              With CreateObject("ADSystemInfo")
               Set objUser     = GetObject("LDAP://" & .UserName)
               Set objComputer = GetObject("LDAP://" & .ComputerName)
              End With
            
            
            ' Add networkprinter connections
            Set WshNetwork = WScript.CreateObject("Wscript.Network")
            sDefaultPrinter = GetCurrentDefaultPrinter(".")
            
             If IsMember(objComputer, "IT PCs")  Then
               strPrinterPath1 = "\\PrintServer\HP2550L"
               strPrinterPath2 = "\\PrintServer\HP1320N"
               strPrinterPath3 = "\\PrintServer\HP2200dtn"
              '_Add the printers
               WshNetwork.AddWindowsPrinterConnection strPrinterPath1
               WshNetwork.AddWindowsPrinterConnection strPrinterPath2
               WshNetwork.AddWindowsPrinterConnection strPrinterPath3
              '_Set the Default printer
               If     LCase(sDefaultPrinter) = LCase(strPrinterPath3) Then 'skip
               ElseIf LCase(sDefaultPrinter) = LCase(strPrinterPath2) Then 'skip
               Else   WshNetwork.SetDefaultPrinter   strPrinterPath1
               End If
             End If
            
            Set WshNetwork = Nothing
            
            
            
            ' add an extra function to the script
            Function GetCurrentDefaultPrinter(strComputer)
              Const DEFAULT = 4
                With GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                 Set colInstalledPrinters = .ExecQuery _
                   ("Select * from Win32_Printer")
                End With
              For Each objPrinter in colInstalledPrinters
                If objPrinter.Attributes And 4 Then
                    GetCurrentDefaultPrinter = objPrinter.ServerName & _
                    "\" &objPrinter.shareName
                    Exit For
                End If
              Next
              Set colInstalledPrinters = Nothing
            End Function
            
            
            '------------------------------------------
            Function IsMember(ByVal objADObject, ByVal strGroup)
                '(from: http://www.rlmueller.net/freecode2.htm : Logon Script 3)
                ' Function to test for group membership.
                ' objGroupList is a dictionary object with global scope.
                If (IsEmpty(objGroupList) = True) Then 
                     Set objGroupList = CreateObject("Scripting.Dictionary")
                End If
                If (objGroupList.Exists(objADObject.sAMAccountName & "\") _
                     = False) Then
                    Call LoadGroups(objADObject, objADObject)
                    objGroupList.Add objADObject.sAMAccountName & "\", True
                End If
                IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
                    & strGroup)
            End Function
            
            Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
                ' Recursive subroutine to populate dictionary object objGroupList.
                Dim colstrGroups, objGroup, j
            
                objGroupList.CompareMode = vbTextCompare
                colstrGroups = objADSubObject.memberOf
                If (IsEmpty(colstrGroups) = True) Then
                    Exit Sub
                End If
                If (TypeName(colstrGroups) = "String") Then
                    Set objGroup = GetObject("LDAP://" & colstrGroups)
                    If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                            & objGroup.sAMAccountName) = False) Then
                        objGroupList.Add objPriObject.sAMAccountName & "\" _
                            & objGroup.sAMAccountName, True
                        Call LoadGroups(objPriObject, objGroup)
                    End If
                    Set objGroup = Nothing
                    Exit Sub
                End If
                For j = 0 To UBound(colstrGroups)
                    Set objGroup = GetObject("LDAP://" & colstrGroups(j))
                    If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                            & objGroup.sAMAccountName) = False) Then
                        objGroupList.Add objPriObject.sAMAccountName & "\" _
                            & objGroup.sAMAccountName, True
                        Call LoadGroups(objPriObject, objGroup)
                    End If
                Next
                Set objGroup = Nothing
            End Sub
            '------------------------------------------

            \Rems


            BTW
            If the subroutine ' DeletePrinterObj(strPrinterPath) ' in the previous script has problems deleting the printer (I dont know, but just in case.) then try replacing the subroutine with this Sub:
            Code:
            Sub RemovePrinterConnection(strPrinterPath)
              With WScript.CreateObject("WScript.Network")
                strComputer = .ComputerName
              With GetObject("winmgmts:\\" & strComputer & _
                   "\root\cimv2")
                Set colInstalledPrinters =  .ExecQuery _
                   ("Select * from Win32_Printer Where Network = TRUE")
              End With
              On Error Resume Next
              For Each objPrinter in colInstalledPrinters
                If strPrinterPath = objPrinter.ServerName & _
                    "\" &objPrinter.shareName Then
                objPrinter.Delete_
                .RemovePrinterConnection strPrinterPath, true, true
                'objPrinter.Delete_
                Exit For
                End If
              Next
              On Error GoTo 0
              Set colInstalledPrinters = Nothing
              End With
            End Sub

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment


            • #7
              Re: Need a Script to Map and Unmap Printers

              Thanks Rems, it's working like a charm, I'm using the first printer delete subroutine and it's removed them without a problem.

              It's remembering if the default printer is changed to another printer too.

              Only change though for anyone reading this is to move the StrPrinterPath's above the If IsMember if you then want to add the Else, DeletePrinterobj(str.. before the End If.

              Code:
              'Sales Admins Printers
              
              Set WshNetwork = WScript.CreateObject("Wscript.Network")
              sDefaultPrinter = GetCurrentDefaultPrinter(".")
              
                 strPrinterPath1 = "\\PS1\HP4050n"
                 strPrinterPath2 = "\\PS1\HP4650n"
                 strPrinterPath3 = "\\PS1\HP3550n"
              
               If IsMember(objComputer, "Sales Admins PCs")  Then
                '_Add the printers
                 WshNetwork.AddWindowsPrinterConnection strPrinterPath1
                 WshNetwork.AddWindowsPrinterConnection strPrinterPath2
                 WshNetwork.AddWindowsPrinterConnection strPrinterPath3
                '_Set the Default printer
                 If     LCase(sDefaultPrinter) = LCase(strPrinterPath3) Then 'skip
                 ElseIf LCase(sDefaultPrinter) = LCase(strPrinterPath2) Then 'skip
                 Else   WshNetwork.SetDefaultPrinter   strPrinterPath1
                 End If
               Else
                 DeletePrinterObj(strPrinterPath1)
                 DeletePrinterObj(strPrinterPath2)
                 DeletePrinterObj(strPrinterPath3)
               End If
              
              Set WshNetwork = Nothing

              Comment


              • #8
                Re: Need a Script to Map and Unmap Printers

                Only change though for anyone reading this ...
                excellent! thanks AJStevens


                In addition
                You could create the a maximum of variables for each existing printer first. Then choose for each group from all these created variables only the right variables# for them.
                Or you can re-use the name of the variables for each group, like in this example:
                Code:
                '<<<...cut>
                
                'Assign the Printers
                Set WshNetwork = WScript.CreateObject("Wscript.Network")
                sDefaultPrinter = GetCurrentDefaultPrinter(".")
                
                '------------------------------------------
                strGroup = "Sales Admins PCs"
                '-------------------------------------------
                   strPrinterPath1 = "\\PS1\HP4050n"
                   strPrinterPath2 = "\\PS1\HP4650n"
                   strPrinterPath3 = "\\PS1\HP3550n"
                 If IsMember(objComputer, strGroup)  Then
                  '_Add the printers
                   WshNetwork.AddWindowsPrinterConnection strPrinterPath1
                   WshNetwork.AddWindowsPrinterConnection strPrinterPath2
                   WshNetwork.AddWindowsPrinterConnection strPrinterPath3
                  '_Set the Default printer
                   If     LCase(sDefaultPrinter) = LCase(strPrinterPath3) Then 'skip
                   ElseIf LCase(sDefaultPrinter) = LCase(strPrinterPath2) Then 'skip
                   Else   WshNetwork.SetDefaultPrinter   strPrinterPath1
                   End If
                 Else
                   DeletePrinterObj(strPrinterPath1) 
                   DeletePrinterObj(strPrinterPath2)
                   DeletePrinterObj(strPrinterPath3)
                 End If
                
                
                '------------------------------------------
                strGroup = "IT PCs"
                '-------------------------------------------
                   strPrinterPath1 = "\\PrintServer\HP2550L"
                   strPrinterPath2 = "\\PrintServer\HP1320N"
                   strPrinterPath3 = Empty  '(not really nesserary to clear the unused variable, you can delete this lines )
                 If IsMember(objComputer, strGroup)  Then
                  '_Add the printers
                   WshNetwork.AddWindowsPrinterConnection strPrinterPath1
                   WshNetwork.AddWindowsPrinterConnection strPrinterPath2
                  '_Set the Default printer
                   If     LCase(sDefaultPrinter) = LCase(strPrinterPath2) Then 'skip
                   Else   WshNetwork.SetDefaultPrinter   strPrinterPath1
                   End If
                 Else
                   DeletePrinterObj(strPrinterPath1) 
                   DeletePrinterObj(strPrinterPath2)
                 End If
                
                
                Set WshNetwork = Nothing
                
                '<cut...>>>
                (don't forget to add 'Bind to the user and computer objects with the LDAP provider' at the beginning of this code
                and to add (all) the FUNCTIONs (2x) and SUBs (2x) at the end of the script.)
                Functions used:
                Function IsMember(ByVal objADObject, ByVal strGroup)
                Function GetCurrentDefaultPrinter(strComputer)

                Subs used:
                Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
                Sub RemovePrinterConnection(strPrinterPath)
                (<-- only one of the two variants)

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Need a Script to Map and Unmap Printers

                  You could create the a maximum of variables for each existing printer first. Then choose for each group from all these created variables only the right variables# for them
                  Thanks Rems, that looks tidier in my script, I wouldn't know how to do it that way.

                  I'm impressed that I've managed to use your script, and what I've searched for on this forum and created a vbs file that maps a global set of mapped drives, and then additional mapped drives depending on the user's AD membership.

                  See what you think, improvements welcome.
                  Note: I could have added in to map the user's home directory, but this is already done in AD. If you search these forums a script uses it, but I believe it would be a like this:

                  Code:
                     strNetworkDriveLetter1 = "Z:"
                     strNetworkDrivePath1 = "\\Server\Users\" + objUser
                  Map a set of Global Network Drives & Additional User Group Membership Drives

                  Code:
                  '// Bind to the user and computer objects with the LDAP provider
                  '// (do not make changes to the next lines. The ADObjects will be used
                  '// as input for the 'IsMember function')
                   Dim objGroupList :  
                    With CreateObject("ADSystemInfo")
                     Set objUser     = GetObject("LDAP://" & .UserName)
                    End With
                  
                  
                  ' Add networkdrive connections
                  Set WshNetwork = WScript.CreateObject("Wscript.Network")
                  ' Forces script to skip errors (rem below line to see errors)
                  on error resume next
                  
                  '-------------------------------------------
                  'Global Mapped Drives
                  '-------------------------------------------
                  
                     strNetworkDriveLetter1 = "O:"
                     strNetworkDrivePath1 = "\\Server\Company"
                  
                     strNetworkDriveLetter2 = "T:"
                     strNetworkDrivePath2 = "\\Server\Transfer"
                  
                    '_Remove the network drive if it exists already
                     WshNetwork.RemoveNetworkDrive strNetworkDriveLetter1, True, True
                     WshNetwork.RemoveNetworkDrive strNetworkDriveLetter2, True, True
                  
                    '_Add the network drive
                     WshNetwork.MapNetworkDrive strNetworkDriveLetter1, strNetworkDrivePath1,True
                     WshNetwork.MapNetworkDrive strNetworkDriveLetter2, strNetworkDrivePath2,True
                  
                     strNetworkDriveLetter1 = Empty
                     strNetworkDrivePath1 = Empty
                     strNetworkDriveLetter2 = Empty
                     strNetworkDrivePath2 = Empty
                  
                  '-------------------------------------------
                  strGroup = "Marketing Users"
                  '-------------------------------------------
                  
                     strNetworkDriveLetter1 = "M:"
                     strNetworkDrivePath1 = "\\Server\Marketing"
                  
                   If IsMember(objUser, strGroup)  Then
                    '_Add the network drive
                     WshNetwork.MapNetworkDrive strNetworkDriveLetter1, strNetworkDrivePath1,True
                  
                   Else
                    '_Remove the network drive
                     WshNetwork.RemoveNetworkDrive strNetworkDriveLetter1, True, True
                  
                   End If
                     strNetworkDriveLetter1 = Empty
                     strNetworkDrivePath1 = Empty
                  
                  '-------------------------------------------
                  strGroup = "Sales Users"
                  '-------------------------------------------
                  
                     strNetworkDriveLetter1 = "S:"
                     strNetworkDrivePath1 = "\\Server\Sales"
                  
                   If IsMember(objUser, strGroup)  Then
                    '_Add the network drive
                     WshNetwork.MapNetworkDrive strNetworkDriveLetter1, strNetworkDrivePath1,True
                  
                   Else
                    '_Remove the network drive
                     WshNetwork.RemoveNetworkDrive strNetworkDriveLetter1, True, True
                  
                   End If
                     strNetworkDriveLetter1 = Empty
                     strNetworkDrivePath1 = Empty
                  
                  '------------------------------------------
                  
                  ' Clean up.
                  Set objGroupList = Nothing
                  Set objUser = Nothing
                  Set WshNetwork = Nothing
                  
                  
                  Wscript.quit
                  '  ~ ~ ~
                  
                  '------------------------------------------
                  Function IsMember(ByVal objADObject, ByVal strGroup)
                      '(from: http://www.rlmueller.net/freecode2.htm : Logon Script 3)
                      ' Function to test for group membership.
                      ' objGroupList is a dictionary object with global scope.
                      If (IsEmpty(objGroupList) = True) Then 
                           Set objGroupList = CreateObject("Scripting.Dictionary")
                      End If
                      If (objGroupList.Exists(objADObject.sAMAccountName & "\") _
                           = False) Then
                          Call LoadGroups(objADObject, objADObject)
                          objGroupList.Add objADObject.sAMAccountName & "\", True
                      End If
                      IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
                          & strGroup)
                  End Function
                  
                  Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
                      ' Recursive subroutine to populate dictionary object objGroupList.
                      Dim colstrGroups, objGroup, j
                  
                      objGroupList.CompareMode = vbTextCompare
                      colstrGroups = objADSubObject.memberOf
                      If (IsEmpty(colstrGroups) = True) Then
                          Exit Sub
                      End If
                      If (TypeName(colstrGroups) = "String") Then
                          Set objGroup = GetObject("LDAP://" & colstrGroups)
                          If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                                  & objGroup.sAMAccountName) = False) Then
                              objGroupList.Add objPriObject.sAMAccountName & "\" _
                                  & objGroup.sAMAccountName, True
                              Call LoadGroups(objPriObject, objGroup)
                          End If
                          Set objGroup = Nothing
                          Exit Sub
                      End If
                      For j = 0 To UBound(colstrGroups)
                          Set objGroup = GetObject("LDAP://" & colstrGroups(j))
                          If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                                  & objGroup.sAMAccountName) = False) Then
                              objGroupList.Add objPriObject.sAMAccountName & "\" _
                                  & objGroup.sAMAccountName, True
                              Call LoadGroups(objPriObject, objGroup)
                          End If
                      Next
                      Set objGroup = Nothing
                  End Sub
                  '------------------------------------------
                  You could add " Set objComputer = GetObject("LDAP://" & .ComputerName)" and use objComputer to map drives depending on the Computer's membership too, so that all PCs in Customer Services have a mapped drive to an ISO standard's folder. However, probably only best for shares that are open to all to read, as most of the time you assign permissions by usergroup and user.

                  Andy
                  Last edited by AJStevens; 11th July 2007, 14:54.

                  Comment


                  • #10
                    Re: Need a Script to Map and Unmap Printers

                    Sorted it, that should work now.
                    Last edited by AJStevens; 11th July 2007, 14:52.

                    Comment


                    • #11
                      Re: Need a Script to Map and Unmap Printers

                      Hi there

                      Edited by Moderator and user banned for life for spamming the site.
                      Last edited by biggles77; 19th July 2007, 15:55. Reason: Spam

                      Comment


                      • #12
                        Re: Need a Script to Map and Unmap Printers

                        wilkin posts reported to admins.
                        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

                        Working...
                        X