n4tuss3c

PT Printer Refresh

Jan 11th, 2022 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Test for Elevated Rights
  2. $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  3. $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  4. if ($testadmin -eq $false) {
  5. Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
  6. exit $LASTEXITCODE
  7. }
  8.  
  9. #Define Variables
  10. $ports = Get-Content '\\protaskadm\scripts\portArray.txt'
  11. $userPrinters = Get-Printer
  12. $printerArray = @()
  13. $printerCount = 0
  14. $currentPort = 0
  15. $currentName = 0
  16.  
  17. #Define variables for deployment waves
  18. $wave1 = New-Object System.Management.Automation.Host.ChoiceDescription '&1','Your Choice: Wave 1'
  19. $wave2 = New-Object System.Management.Automation.Host.ChoiceDescription '&2','Your Choice: Wave 2'
  20. $wave3 = New-Object System.Management.Automation.Host.ChoiceDescription '&3','Your Choice: Wave 3'
  21. $wave4 = New-Object System.Management.Automation.Host.ChoiceDescription '&4','Your Choice: Wave 4'
  22. $wave5 = New-Object System.Management.Automation.Host.ChoiceDescription '&5','Your Choice: Wave 5'
  23. $wave6 = New-Object System.Management.Automation.Host.ChoiceDescription '&6','Your Choice: Wave 6'
  24. $wave7 = New-Object System.Management.Automation.Host.ChoiceDescription '&7','Your Choice: Wave 7'
  25. $wave8 = New-Object System.Management.Automation.Host.ChoiceDescription '&8','Your Choice: Wave 8'
  26. $wave9 = New-Object System.Management.Automation.Host.ChoiceDescription '&9','Your Choice: Wave 9'
  27. $wave10 = New-Object System.Management.Automation.Host.ChoiceDescription '&10','Your Choice: Wave 10'
  28. $options = [System.Management.Automation.Host.ChoiceDescription[]]($wave1, $wave2, $wave3, $wave4, $wave5, $wave6, $wave7, $wave8, $wave9, $wave10)
  29.  
  30.  
  31.  
  32. #Title Banner
  33.  
  34. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
  35. Write-Host '                                                        '
  36. Write-Host '                   PT PRINTER REFRESH                   '
  37. Write-Host '                         .----.                         '
  38. Write-Host '                         |    |                         '
  39. Write-Host '                       __|____|__                       '
  40. Write-Host '                     `|  ______--|                      '
  41. Write-Host '                       -/.::::.\-                       '
  42. Write-Host '                      '`--------'                       '
  43. Write-Host '                                                        '
  44. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
  45.  
  46. #Get user input for wave deployment
  47. $title = 'Select your Wave'
  48. $message = 'Please select your wave. Reference the email if necessary'
  49. $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  50.  
  51. #enumerate user printers
  52. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++' -ForegroundColor Yellow
  53. Write-Host '                                                        ' -ForegroundColor Yellow
  54. Write-Host '       We are gathering your installed printers         ' -ForegroundColor Yellow
  55. Write-Host '                                                        ' -ForegroundColor Yellow
  56. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++' -ForegroundColor White
  57. Start-Sleep -s 5
  58. foreach ($userPrinter in $userPrinters){
  59.  
  60.     $enumeratePrt = [PSCustomObject]@{
  61.    
  62.         Name = $userPrinter.Name
  63.         Port = $userPrinter.PortName
  64.    
  65.     }
  66.  
  67.         $printerArray += $enumeratePrt
  68.  
  69. }
  70.  
  71. #step through printers
  72. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++  ' -ForegroundColor Yellow
  73. Write-Host '                                                        ' -ForegroundColor Yellow
  74. Write-Host '  We are checking for any printers to be uninstalled    ' -ForegroundColor Red
  75. Write-Host '                and uninstalling them                   ' -ForegroundColor Red
  76. Write-Host '                                                        ' -ForegroundColor Yellow
  77. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++' -ForegroundColor White
  78. :parent foreach ($printer in $printerArray){
  79.    
  80.       :child1 foreach ($port in $ports){
  81.    
  82.                 #write-host 'The current printer is ' $printer.Name
  83.                 #Write-Host 'The current port is ' $port
  84.  
  85.                 if (($printer.Port -eq $port) -or ($printer.Port -like 'WSD*')){
  86.        
  87.                     Remove-Printer -Name $printer.Name -ErrorAction Stop
  88.                     #Write-Host 'Printer ' $printer.Name ' found'
  89.                     $printerCount++
  90.                     break child1
  91.                 }else{
  92.  
  93.                 #do nothing
  94.                 }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++  ' -ForegroundColor Yellow
  101. Write-Host '                                                        ' -ForegroundColor Yellow
  102. Write-Host     $printerCount ' printer(s) were uninstalled          ' -ForegroundColor Green
  103. Write-Host '                                                        ' -ForegroundColor Yellow
  104. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++' -ForegroundColor White
  105.  
  106.  
  107. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++  ' -ForegroundColor Yellow
  108. Write-Host '                                                        ' -ForegroundColor Yellow
  109. Write-Host '      We are installing your new printer(s)             ' -ForegroundColor Green
  110. Write-Host '                                                        ' -ForegroundColor Yellow
  111. Write-Host '++++++++++++++++++++++++++++++++++++++++++++++++++++++++' -ForegroundColor White
  112.  
  113. #Add Printers
  114.  
  115. switch ($result){
  116.  
  117. 0 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  118.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  119.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  120.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  121.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  122.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  123.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  124.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  125.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  126.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  127. 1 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  128.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  129.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  130.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  131.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  132.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  133.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  134.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  135.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  136.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  137. 2 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  138.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  139.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  140.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  141.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  142.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  143.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  144.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  145.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  146.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  147. 3 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  148.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  149.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  150.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  151.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  152.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  153.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  154.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  155.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  156.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  157. 4 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  158.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  159.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  160.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  161.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  162.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  163.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  164.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  165.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  166.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  167. 5 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  168.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  169.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  170.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  171.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  172.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  173.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  174.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  175.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  176.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  177. 6 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  178.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  179.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  180.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  181.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  182.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  183.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  184.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  185.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  186.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  187. 7 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  188.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  189.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  190.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  191.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  192.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  193.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  194.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  195.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  196.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  197. 8 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  198.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  199.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  200.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  201.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  202.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  203.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  204.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  205.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  206.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  207. 9 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  208.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  209.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  210.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  211.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  212.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  213.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  214.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  215.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  216.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  217. 10 {Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  218.    Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  219.         Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  220.         Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  221.             Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  222.             Add-Printer -ConnectionName '\\<printserverhere>\<printernamehere>'
  223.                 Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Yellow
  224.                 Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'
  225.                     Write-Host 'Adding Printer <printernamehere>' -ForegroundColor Green
  226.                     Add-Printer -ConnectionName '<printserverhere>\<printernamehere>'}
  227. }
  228. Start-Sleep -s 3
Advertisement
Add Comment
Please, Sign In to add comment