PoshCode Logo PowerShell Code Repository

Add-NetworkPrinter.ps1 by Jan Egil Ring 7 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1494"></script>download | new post

Windows PowerShell script to map a network printer based on Active Directory group membership.

  1. ###########################################################################"
  2. #
  3. # NAME: Add-NetworkPrinter.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # EMAIL: jan.egil.ring@powershell.no
  7. #
  8. # COMMENT: Windows PowerShell script to map a network printer based on Active Directory group membership.
  9. #          The get-GroupMembership function are created by Andy Grogan, see his blogpost for more information on this function:
  10. #          http://www.telnetport25.com/component/content/article/15-powershell/127-quick-tip-determining-group-ad-membership-using-powershell.html
  11. #          Also see this blogpost for more information:
  12. #          http://blog.powershell.no/2009/11/28/mapping-printers-based-on-active-directory-group-membership-using-windows-powershell
  13. #          Tested with Windows PowerShell v 1.0/2.0 and Windows XP/Vista/7.
  14. #
  15. # You have a royalty-free right to use, modify, reproduce, and
  16. # distribute this script file in any way you find useful, provided that
  17. # you agree that the creator, owner above has no warranty, obligations,
  18. # or liability for such use.
  19. #
  20. # VERSION HISTORY:
  21. # 1.0 28.11.2009 - Initial release
  22. #
  23. ###########################################################################"
  24.  
  25. $strName = $env:username
  26.  
  27. function get-GroupMembership($DNName,$cGroup){
  28.        
  29.         $strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
  30.  
  31.         $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
  32.         $objSearcher.Filter = $strFilter
  33.  
  34.         $objPath = $objSearcher.FindOne()
  35.         $objUser = $objPath.GetDirectoryEntry()
  36.         $DN = $objUser.distinguishedName
  37.                
  38.         $strGrpFilter = "(&(objectCategory=group)(name=$cGroup))"
  39.         $objGrpSearcher = New-Object System.DirectoryServices.DirectorySearcher
  40.         $objGrpSearcher.Filter = $strGrpFilter
  41.        
  42.         $objGrpPath = $objGrpSearcher.FindOne()
  43.        
  44.         If (!($objGrpPath -eq $Null)){
  45.                
  46.                 $objGrp = $objGrpPath.GetDirectoryEntry()
  47.                
  48.                 $grpDN = $objGrp.distinguishedName
  49.                 $ADVal = [ADSI]"LDAP://$DN"
  50.        
  51.                 if ($ADVal.memberOf.Value -eq $grpDN){
  52.                         $returnVal = 1
  53.                         return $returnVal = 1
  54.                 }else{
  55.                         $returnVal = 0
  56.                         return $returnVal = 0
  57.        
  58.                 }
  59.        
  60.         }else{
  61.                         $returnVal = 0
  62.                         return $returnVal = 0
  63.        
  64.         }
  65.                
  66. }
  67.  
  68. $result = get-groupMembership $strName "Printer_group_01"
  69. if ($result -eq '1') {
  70. Invoke-Expression 'rundll32 printui.dll,PrintUIEntry /in /q /n"\\print-server\printer-share"'
  71. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me