creamers

Printer Migration

Nov 29th, 2014 (edited)
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSYS
  3.     .
  4. .DESCRIPTION
  5.     Script designed to help automate printer migrations
  6.     Requires Powershell Version 4 on the client side
  7. .EXAMPLE
  8.     Printer-Migration.PS1
  9. .NOTES
  10.     Script to help automate printer migrations, was designed for 2003 x86 to 2008 R2 specifically buts work in other variations.
  11.     reddit.com/r/creamersrealm 11/29/2014
  12. #>
  13.  
  14. #Requires –Version 4
  15.  
  16. #Output Possible Printer Actions
  17. Write-host "1 = Export Printers"
  18. Write-host "2 = Import Printers"
  19.  
  20. #Variables
  21. $tempfolder = $env:TEMP
  22.  
  23. #Do not edit anything below this line
  24.  
  25. #Begin Functions
  26. function Get-TempFolder {
  27. #Function to Verify the temp folder exists for printers, this must be ran as a admin if you use a folder outside of your profile.
  28. $tempcheck = test-path $tempfolder -ErrorAction SilentlyContinue
  29. if ($tempcheck -eq $true)
  30.     {
  31.         #Do Nothing and continue
  32.     }
  33.     else
  34.     {
  35.         New-Item -Path $tempfolder -ItemType dir | Out-Null
  36.     }
  37. }
  38.  
  39. function show-csvhelp {
  40.     Write-host "Please open the the csv and remove any printers you do not want to import such as PDF and XPS printers."
  41.     Write-Host ""
  42.     Write-host "Open the File in Excel and make a column named IPAddress, this will be the ip addresses listed in the port name most likely."
  43.     Write-host "I suggest using Text-Columns to make this easier just keep a copy of PortName."
  44.     Write-Host ""
  45.     Write-host "Change the Drivers to match the imported drivers"
  46.     Write-host "That is it!"
  47.     Write-Host ""
  48.     Write-host "Rerun the script and chose option 2"
  49. }
  50.  
  51. function Get-CurrentPrinters {
  52.     #Verify the Temp Folder Exists
  53.     Write-Host '' #For Formatting
  54.     write-host "The Exported file is located in $tempfolder\$printserver-printerexport.csv"
  55.     Get-TempFolder
  56.     #Begin Printer Export
  57.     $printserver = Read-host "What is the current print server?"
  58.     Get-WMIObject -class Win32_Printer -computer $printserver | select Name,DriverName,PortName,ShareName,Comment | Export-CSV -path "$tempfolder\$printserver-printerexport.csv" -NoTypeInformation
  59.     #Show the Help Function, I could have put it here but I figured have the help in a function for a more moduler design
  60.     Show-CSVHelp
  61. }
  62.  
  63. function New-PrinterPort {
  64.     #From the Interwebs, this creates the printer port for us to use, when we add the printer.
  65.         $server = $args[0]
  66.         $port = ([WMICLASS]"\\$printserver\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance()
  67.         $port.Name = $args[1]
  68.         $port.SNMPEnabled = $false
  69.         $port.Protocol = 1
  70.         $port.HostAddress = $args[2]
  71.         $port.Put()
  72.     }
  73.  
  74. function New-FileItemSelection {
  75. #Function to build a menu from the $temp
  76. #From the interwebs
  77. #The output selection from this function is $file
  78. $menu = @{}
  79. $files =  Get-childItem -Path $tempfolder | Where-Object {$_.Name -like "*.csv"}
  80.     for ($i = 0; $i -lt $files.count; $i ++)
  81.     {
  82.         $menu.Add($i,$files[$i])
  83.         Write-Host ($i + 1) $files[$i]
  84.     }
  85.     [int]$ans = Read-Host 'Choose the csv to import'
  86.     write-host "Please select which CSV you would like to import."
  87.     $file = $menu.Item($ans - 1) | select name | Select-Object -First 1
  88.     $file.Name | Select-Object -First 1 | Set-Variable -Name file -scope 1 -ErrorAction SilentlyContinue
  89. }
  90.  
  91. function New-Printer {
  92. #Function to Add the Printer to the print server.
  93. #Also know as Function Do-Stuff
  94. New-FileItemSelection
  95. write-host ''
  96. write-host "This can take up to a minute a printer"
  97. $printserver = Read-Host "What is the Print Server we are installing on?"
  98. #We are appending the $tempfolder to $file
  99. $printers = import-csv $tempfolder\$file
  100.  
  101. #Checking if user added the right headers
  102. if ($printers.IPAddress -eq "")
  103.     {
  104.         write-host "It Seems like you didn't setup the csv correct, you should really read the directions."
  105.     }
  106.     else {}
  107.  
  108. foreach ($printer in $printers)
  109.     {
  110.         New-PrinterPort $printserver $printer.Portname $printer.IPAddress
  111.         Add-Printer -Name $printer.Name -ShareName $printer.ShareName -computername $printserver -drivername $printer.DriverName -PortName $printer.PortName -Comment $printer.Comment -Published:$false -Shared:$True
  112.         write-host -ForegroundColor White $printer.Name "has been added to $printserver"  
  113.     }
  114. }
  115.  
  116. #Printer Selection Input Message
  117. $printerSelection = Read-Host "Which Printer Action?"
  118. $whoami = whoami
  119. write-host ''
  120. write-host "You are currently $whoami, if this is not correct please exit."
  121.  
  122. #Replacing $printeraction with $printerSelection, this allows to expand the script later on if need be.
  123. $printeraction = $printerSelection
  124.  
  125. if(!($printeraction)) {
  126.     Throw "You did not select a valid printer option"
  127.     Exit
  128. }
  129.  
  130. Switch($printeraction) {
  131.     1 {Get-CurrentPrinters}
  132.     2 {New-Printer}
  133.     default {
  134.     $errorMessage
  135.     Exit
  136.     }
  137. }
  138. # *** End Menu Selection Code Block
Advertisement
Add Comment
Please, Sign In to add comment