Announcement

Collapse
No announcement yet.

Set Default Printer if not in a specific group

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

  • Set Default Printer if not in a specific group

    Hi all,

    Would like to have a script at logon that sets the default printer to a specific printer if the computer is NOT in a specific Active Directory OU.

    I'm guessing it would look like this but this doesn't work.

    Code:
    If group not ("BW Accounts") then
     objNetwork.SetDefaultPrinter strLocal3
    end if
    Please advise

    Mant thanks
    Last edited by cmcf; 30th March 2007, 11:59.

  • #2
    Re: Set Default Printer if not in a specific group

    "If group not ("BW Accounts")" will not work (you knew that of course)
    because you first have to determine the computername. Then enumerate the members of one group, looking If the computer is a member. Then set default printer End If

    If you planned to use a group enumeration more than one time in the same script, then it is better to write that part only one time as a routine for the script, it wil returns the value back to the main part of the script. That way you created a script "Function". A written script function is also more easy to copy to other scripts.

    examples of this particular script function can be found on the internet;
    - search: vbs IsMember function
    - or: vbs MemberOf function
    - or: vbs enumerate members of a group

    http://www.rlmueller.net/freecode2.htm <<=script #3:=>> "Add a network printer if the computer is a member of the group. Make this printer the default."
    The beauty of the function in this script is it can used for both type of members usersobjects or computerobjects.


    If you would have been looking for a way to set a defaultprinter for useraccounts only, then there is a faster way by searching the usersobject and then enumarate the account its 'member of' groupnames and look if the group is there.
    - google for: objUser.groups
    - Or: objUser.memberof


    \Rem
    Last edited by Rems; 31st March 2007, 18:16.

    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


    • #3
      Re: Set Default Printer if not in a specific group

      Thank you for your help here Rems. Have been playing around with that script for a few days but am unable to get it working.

      This is what i've got

      Code:
      Option Explicit
      
      Dim objNetwork, objSysInfo, strUserDN
      Dim objGroupList, objUser, objFSO
      Dim strComputerDN, objComputer
      
      Set objNetwork = CreateObject("Wscript.Network")
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objSysInfo = CreateObject("ADSystemInfo")
      strUserDN = objSysInfo.userName
      strComputerDN = objSysInfo.computerName
      
      ' Bind to the user and computer objects with the LDAP provider.
      Set objUser = GetObject("LDAP://cn=Not Accounts,OU=BW Computers,dc=companyname,dc=com" & strUserDN)
      Set objComputer = GetObject("LDAP://cn=Not Accounts,OU=BW Computers,dc=burrenandwhitaker,dc=office" & strComputerDN)
      
      
      ' Add a network printer if the computer is a member of the group.
      ' Make this printer the default.
      If (IsMember(objComputer, "Not Accounts") = True) Then
          objNetwork.AddWindowsPrinterConnection "\\print-server\Ricoh Aficio 1224C PCL 5c"
          objNetwork.SetDefaultPrinter "\\print-server\Ricoh Aficio 1224C PCL 5c"
      End If
      
      ' Clean up.
      Set objNetwork = Nothing
      Set objFSO = Nothing
      Set objSysInfo = Nothing
      Set objGroupList = Nothing
      Set objUser = Nothing
      Set objComputer = Nothing
      
      Function IsMember(objADObject, strGroup)
          ' 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(objPriObject, 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://cn=Not Accounts,OU=BW Computers,dc=companyname,dc=com" & 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://cn=Not Accounts,OU=BW Computers,dc=companyname,dc=com" & 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
      I get an error on line 14 character 1,error- 0x80005000,code- 80005000

      The computer I'm testing the script on is a member of the Not Accounts group which is located in an OU named BW Computers.

      The scenario is that i don't want the default printer set on Accounts computers as this screws up the printer for the accounts software. So all i want is for the default printer to be set on all other computers. Seems simple but the solution doesn't look very simple!

      Thanks for your help with this.
      Last edited by cmcf; 2nd April 2007, 13:46.

      Comment


      • #4
        Re: Set Default Printer if not in a specific group

        If that are real domain and group names, then edit your reply. It is for security to publish as less as possible information about your domain on a public site.

        What about the example script, there is not much to play around with that script. It is ready for use as it is.

        The script determines by it self the name of the computer used for checking the groups (and also determines the name of the current user for checking user groups).
        In the main part of the script you only have to provide the name of the group to check. The script uses that name as a parameter to the function, the function does the rest by it self. Do not edit the function.

        \Rem

        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: Set Default Printer if not in a specific group

          Thanks Rems, have edited it.

          I haven't edited any of the scipt. Only changed my LDAP path and this line -

          If (IsMember(objComputer, "Not Accounts") = True) Then

          Have also taken out the drive mapping section as this isn't needed.

          Should the functions IsMember and LoadGroups be part of the same script?

          Comment


          • #6
            Re: Set Default Printer if not in a specific group

            The script does not need the LDAP path, only the name of the group.

            This is what you need for setting the default printer (you only need to edit the blue parameters)
            Code:
            '// Bind to the user and computer objects with the LDAP provider
            Set objSysInfo      = CreateObject("ADSystemInfo")
                strUserDN       = objSysInfo.userName
                strComputerDN   = objSysInfo.computerName
                Set objUser     = GetObject("LDAP://" & strUserDN)
                Set objComputer = GetObject("LDAP://" & strComputerDN)
                Dim objGroupList
            Set objSysInfo = Nothing
            
            
            '// Add network printers
            Set objNetwork = CreateObject("Wscript.Network")
            objNetwork.AddWindowsPrinterConnection "\\PrintServer\HPLaser2"
            objNetwork.AddWindowsPrinterConnection "\\PrintServer\HPLaser4"
            
            '// If the computer is a member of the group, make this printer the default.
            If (IsMember(objComputer, "Groupxxx") = True) Then
               objNetwork.SetDefaultPrinter "\\PrintServer\HPLaser2"
            End If
            
            If (IsMember(objComputer, "Groupyyy") = True) Then
               objNetwork.SetDefaultPrinter "\\PrintServer\HPLaser4"
            End If
            
            Set objNetwork = Nothing  '<--- after all mappings are done, there is no further need for this objNetwork any more, so you can remove it from memory like this.
            
            
            ' Clean up what is still left in memory
            Set objUser      = Nothing
            Set objComputer  = Nothing
            Set objGroupList = Nothing
            Wscript.quit(0)  '<---all what needs to be done is done now, end the script 
            
            
            'You have to add this 'Function' and 'Sub' to the very bottom of the script
            'this will do the membership checking as a routine, it uses automaticly the
            'objAccountname and Groupname it gets from the main part of the script
            '------------------------------------------
            Function IsMember(objADObject, strGroup)
                '// Function to test for group membership (for users as wel as computers)
                '// 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)  '<-- populate objGroupList now
                   objGroupList.Add objADObject.sAMAccountName & "\", True
                End If
                IsMember = objGroupList.Exists(objADObject.sAMAccountName & _
                           "\" & strGroup)
            End Function
            
            Sub LoadGroups(objPriObject, 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
            '------------------------------------------

            \Rem



            (07 9 2007) EDIT -
            see also:
            http://forums.petri.com/showthread.php?t=16886
            Last edited by Rems; 9th July 2007, 18:57. Reason: add the line: Dim objGroupList

            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: Set Default Printer if not in a specific group

              Brilliant! Works a treat. Thanks for your help Rems

              Comment


              • #8
                Re: Set Default Printer if not in a specific group

                works a treat.......well at the time i thought it did.

                Still got a problem with this. Here's the situation and what happens

                A user logs onto a non accounts PC and so therefore the script above will set her default printer to Printer 1 (for example). No problems there, but when she logs onto an Accounts PC the default printer remains at Printer 1 when it should be Printer 2 (for example).

                It seems that once Printer 1 has been set as default printer it wont change no matter what.

                Could the problem be that i'm not disconnecting the printers at log off? so it still thinks that Printer 1 is my default?

                If so, How can i disconnect networked printers at log off?

                Comment


                • #9
                  Re: Set Default Printer if not in a specific group

                  well, i created a log off script that disconnects all the networked printers. Thats works well. Doesn't solve the problem though.

                  This is what happens now. Got 5 mapped networked printers that are connected at log on and disconnected at log off and 1 locally installed printer that is used to print accounts stuff (Accounts Printer).

                  If the computer is in a specified LDAP group then it will set the default to one of the networked printers. No problem there.

                  Because of the nature of our accounts software it wont print to the Accounts printer if the default printer has been set by a script, it will only work if set by default by the user. Usually if you manually set the default printer in XP it should stay set even whn you log back in again. This isnt the case if you log onto a non accounts computer on my network as if its not an accounts computer (i.e in the specified LDAP group) it will set the a default networked printer via a script.

                  So here's what happens. I log into my NON accounts computer (so is in the specified LDAP group). The script sets my default printer to a mapped network printer. No problem. I log out

                  I then log into an Accounts PC (not in the specified LDAP group) so it doesn't set a default printer. Still good. I set the locally installed Accounts printer to default myself. I then restart the Accounts PC for testing purposes.

                  I then log into my other NON accounts computer and it sets the default printer to the networked printer via script. All good still!!!! I log off

                  This is where it goes wrong!!! Log back onto the accounts PC and the default printer is no longer the Accounts Printer but some randomly assigned mapped network printer.

                  Remember though, i can't have a script to set my default printer as accounts as it wont print.

                  Anyone got any ideas? Sorry its a long description but i felt i had to detail it the best i could as the situation is bizarre.

                  Thanks people!

                  Comment


                  • #10
                    Re: Set Default Printer if not in a specific group

                    Dis-connecting and re-connecting the printers won't work because, when the logonscript add new printer mappings, the last added printer will in most cases automaticaly set as default printer. The command to set the default printer only let you re-set the default printer after the mappings are made.

                    I dont know if the following will solve this behavior, but you can give it a try:
                    Add these lines into the script -=before=- the lines where the printer mappings are made:
                    Code:
                    Const HKEY_CURRENT_USER = &H80000001
                    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
                    strKeyPath = "Printers\Settings\Wizard"
                    objReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,"Set As Default",0
                    This will change the defaults that are used in the "printer installation Wizard", so manualy added printers are not automacticaly set to default-printer. Maybe (???) this wil have the same affect on AddWindowsPrinterConnection too? I don't know.

                    -----------------

                    But tell a bit more about the local printer, is that printer connected to the LPT1-port? In that case you can try this script to set the local printer the default-printer:
                    Code:
                    'What is the port the local-printer is plugged-in to? 
                    strPort = "LPT1:"  '<--- or USB001 or ...? (check with printers&fx/File/ServerPropperties/ports)
                    
                    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                    strPort = replace(strPort,":",chr(58))
                    Set colItems = objWMIService.ExecQuery("Select Name from Win32_Printer where PortName='" & strPort & "'",,48)
                        For Each objItem In colItems
                            strPrinterName = objItem.Name
                            Exit For
                        Next
                    Set objNetwork = WScript.CreateObject("WScript.Network")
                    objNetwork.SetDefaultPrinter strPrinterName
                    If the accounting-application can only work with local ports for printing?, then you can try if local-port-remapping might work as well. First add a network printer connection and then map a free localprinter-port to that network printer.
                    Like in this example:
                    Code:
                    'This script example maps the virtual LPT2 to a certain networkprinter, then made it the default-printer.
                    
                    sPrinterNetworkPath = "\\server\printersharename"
                    sReMapLocalPort = "LPT2:"
                     
                    Set objNetwork = WScript.CreateObject("WScript.Network")
                    sReMapLocalPort = Replace(sReMapLocalPort,":","")
                    On Error Resume Next
                    'Return = objNetwork.RemovePrinterConnection(sPrinterNetworkPath, true, true)
                    'Return = objNetwork.AddWindowsPrinterConnection(sPrinterNetworkPath)
                    objNetwork.AddPrinterConnection sReMapLocalPort,sPrinterNetworkPath, False 
                    objNetwork.SetDefaultPrinter sPrinterNetworkPath
                    \Rems

                    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


                    • #11
                      Re: Set Default Printer if not in a specific group

                      Thanks again Rems,

                      The Accounts Printer is locally installed to a TCP/IP port and then pointed to a printer with an IP address. The same printer is also mapped at my logon script we did before but this won't do all the neccessary accounts printing where as the locally installed one does. I'm guessing it prefers the print drivers on the local PC rather than the server. It's very annoying what ever the reason it is for it not to be working.

                      Comment


                      • #12
                        Re: Set Default Printer if not in a specific group

                        I think that the software just cannot work with a printer that is mapped to a network printer. Therefore a local (or locally created) port must be mapped to the printer.


                        You can set a local printer as default-printer by using a VBscript. Have you tried to manualy run one of these scripts on the accounting computer before opening the accounting software?;
                        *1 A script using the printer name (! names of local printers can be changed, so can be different on different computers);
                        Code:
                        strPrinterName = "display name"
                        
                        Set objNetwork = WScript.CreateObject("WScript.Network")
                        objNetwork.SetDefaultPrinter strPrinterName
                        *2 Or, by using the port_name (! tcpip_ports can be re-created with other names);
                        Code:
                        strPortName = "IP_169.254.110.14"
                        
                        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                        strPortName = replace(strPort,":",chr(58))
                        Set colItems = objWMIService.ExecQuery("Select Name from Win32_Printer where PortName='" & strPortName & "'",,48)
                            For Each objItem In colItems
                                strPrinterName = objItem.Name
                                Exit For
                            Next
                        Set objNetwork = WScript.CreateObject("WScript.Network")
                        objNetwork.SetDefaultPrinter strPrinterName
                        *3 Using the IP-address of the host that is attached to the portname (! the IPaddress is the same on every computer);
                        Code:
                        strTCPIP = "169.254.110.14"
                        
                        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
                        Set colItems = objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort where HostAddress='" & strTCPIP & "'",,48)
                            For Each objItem In colItems
                                strPortName = replace(objItem.Name,":",chr(58))
                                Exit For
                            Next
                        Set colItems = objWMIService.ExecQuery("Select Name from Win32_Printer where PortName='" & strPortName & "'",,48)
                            For Each objItem In colItems
                                strPrinterName = objItem.Name
                                Exit For
                            Next
                        Set objNetwork = WScript.CreateObject("WScript.Network")
                        objNetwork.SetDefaultPrinter strPrinterName
                        *4 Or make changes in the current user part of the registry;
                        http://forums.petri.com/showthread.php?t=14109

                        If you can set the local printer as the default by script, then what is the reaction exactly of the accounting software to it?

                        \Rems

                        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


                        • #13
                          Re: Set Default Printer if not in a specific group

                          I will get round to testing those methods a little later on.

                          I have run a script thats sets the default printer to the local accounts printer on accounts PC. The behaviour is very strange though. It prints everything apart from audit trails perfectly in Winpay on the 1st attempt after the script has been run. On the 2nd attempt of printing it prints everything fine. Obviously if i set the printer to default manually (i.e go to printer and faxes) it works with no problems (even on first attempt)

                          I think you can download the accounts software and try it for yourself if you so wish. Its called Winaccs/Winpay 6.1

                          http://www.compact-software.co.uk/Pages/Downloads.htm

                          Comment

                          Working...
                          X