Advertisement
Guest User

Change-PrinterPortIP.ps1

a guest
Apr 28th, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.  .Synopsis
  3.   Script to change printer IP address
  4.  
  5.  .Description
  6.   This script changes printer IP address
  7.   It leaves a log file in the current folder that lists script progress
  8.  
  9.  .Parameter OldIP
  10.   Example: 192.168.0.99
  11.  
  12.  .Parameter NewIP
  13.   Example: 192.168.0.95
  14.  
  15.  .Example
  16.    .\Change-PrinterIP.ps1 192.168.0.99 192.168.0.95
  17.    This example changes the printer attached to the print port with IP 192.168.0.99
  18.    It creates a new printer port with same settings as the old 192.168.0.99 printer port,
  19.    assigns the printer to the new port, and deletes the old printer port
  20.  
  21.  .Link
  22.   https://superwidgets.wordpress.com/category/powershell/
  23.  
  24.  .Notes
  25.   v1.0 - 07/27/2014
  26.  
  27. #>
  28.  
  29. #==============================================================================
  30. # Script Name:      Change-PrinterIP.ps1
  31. # DATE:             07/27/2014
  32. # Version:          1.0
  33. # COMMENT:          Script to change printer IP address
  34. #==============================================================================
  35. #    
  36. [CmdletBinding()]
  37. param(
  38.     [Parameter (ParameterSetName="p1",Mandatory=$true,Position=0,HelpMessage="Printer's old IP: ")][String]$OldIP,
  39.     [Parameter (ParameterSetName="p1",Mandatory=$true,Position=1,HelpMessage="Printer's new IP: ")][String]$NewIP,
  40.     [Parameter ()][array]$ComputerName = $env:COMPUTERNAME,
  41.     [Parameter ()][switch]$RemovePort = $false,
  42.     [Parameter ()][string]$PortPrefix,
  43.     [Parameter (ParameterSetName="p2",HelpMessage="CSV file with 'NewIP' and 'OldIP' columns.")][string]$List = ".\printerports.csv"
  44. )
  45. #
  46. # Log function
  47. function Log {
  48.     [CmdletBinding()]
  49.     param(
  50.         [Parameter (Mandatory=$true,Position=1,HelpMessage="String to be saved to log file and displayed to screen: ")][String]$String,
  51.         [Parameter (Mandatory=$false,Position=2)][String]$Color = "White",
  52.         [Parameter (Mandatory=$false,Position=3)][String]$Logfile = "\logs" + $myinvocation.mycommand.Name.Split(".")[0] + "_" + (Get-Date -format yyyyMMdd_hhmmsstt) + ".txt"
  53.     )
  54.     write-host $String -foregroundcolor $Color  
  55.     ((Get-Date -format "yyyy.MM.dd hh:mm:ss tt") + ": " + $String) | out-file -Filepath $Logfile -append
  56. }
  57.  
  58. function Create-PrinterPort {
  59. param(
  60.     [Parameter ()][String]$NewIP,
  61.     [Parameter ()][string]$ComputerName  
  62. )
  63.    
  64.     $printerip =$NewIP
  65.     $port = ([WMICLASS]"\\$ComputerName\ROOT\cimv2:Win32_TCPIPPrinterPort”).createInstance()
  66.    $port.Name = "$NewIP"
  67.    $port.SNMPEnabled=$false
  68.    $port.Protocol=1
  69.    $port.HostAddress=$NewIP
  70.    $port.Put()
  71.    #$port
  72. }
  73.  
  74. ForEach ($computer in $ComputerName) {
  75.    If (($OldIP) -and ($NewIP)) {
  76.  
  77.        $PrinterPorts = New-Object -TypeName PSObject
  78.        $PrinterPorts | Add-Member -Name oldIP -Value $OldIP -MemberType NoteProperty
  79.        $PrinterPorts | Add-Member -Name newIP -Value $NewIP -MemberType NoteProperty
  80.        Write-Output -Verbose "Processing individual port $($PrinterPorts.oldIP)"
  81.    }
  82.    Else {
  83.        Write-Output -Verbose "Processing list '$List'"
  84.        $PrinterPorts = Import-Csv $List
  85.    }
  86.    
  87.    ForEach ($Port in $PrinterPorts){
  88.          
  89.        $LogFile = (Get-Location).path + "\Change-PrinterIP_" + $Computer + (Get-Date -format yyyyMMdd_hhmmsstt) + ".txt"
  90.        $PortInfo = Get-PrinterPort -Name "$($PortPrefix)$($Port.oldip)" -ComputerName $Computer
  91.        
  92.        if (!$PortInfo) {
  93.            Write-Output -Verbose "Port $($PortPrefix)$($Port.oldip) not found on $Computer"
  94.            
  95.        }
  96.        else {
  97.            $Printers = Get-Printer -ComputerName $Computer | Where-Object { $_.PortName -match "$($PortPrefix)$($Port.OldIP)"}
  98.            
  99.            log "Old printer port:" Cyan $LogFile
  100.            log ("    Protocol:           " + $PortInfo.Protocol) Cyan $LogFile
  101.            log ("    Description:        " + $PortInfo.Description) Cyan $LogFile
  102.            log ("    Name:               " + $PortInfo.Name) Cyan $LogFile
  103.            log ("    Port Number:        " + $PortInfo.PortNumber) Cyan $LogFile
  104.            log ("    PrinterHostAddress: " + $PortInfo.PrinterHostAddress) Cyan $LogFile
  105.            log ("    Associated Printer: " + $Printers.name) Cyan $Logfile
  106.            #(Get-Printer -ComputerName $Computer | Where-Object { $_.PortName -match "$($PortPrefix)$($Port.OldIP)" }).PortName) Cyan $LogFile
  107.                
  108.            # Create new printer port
  109.            Create-PrinterPort -ComputerName $Computer -NewIP $Port.NewIP
  110.    
  111.            #
  112.            # Assign new printer port to the printer
  113.            $PrinterName = Get-Printer -ComputerName $Computer | Where-Object { $_.PortName -match "$($PortPrefix)$($Port.OldIP)" }
  114.  
  115.  
  116.            Get-Printer -ComputerName $Computer | Where-Object { $_.PortName -match "$($PortPrefix)$($Port.OldIP)" } | Set-Printer -ComputerName $Computer -PortName "$($Port.NewIP)"
  117.  
  118.            #
  119.            If ($RemovePort) {
  120.                write-output "Removing port..."
  121.                Remove-PrinterPort -Name "$($PortPrefix)$($Port.OldIP)" -ComputerName $Computer
  122.            }
  123.  
  124.            $PortInfo = Get-PrinterPort -Name "$($Port.NewIP)" -ComputerName $Computer
  125.  
  126.            log "New printer port:" Green $LogFile
  127.            log ("    Protocol:           " + $PortInfo.Protocol) Green $LogFile
  128.            log ("    Description:        " + $PortInfo.Description) Green $LogFile
  129.            log ("    Name:               " + $PortInfo.Name) Green $LogFile
  130.            log ("    Port Number:        " + $PortInfo.PortNumber) Green $LogFile
  131.            log ("    PrinterHostAddress: " + $PortInfo.PrinterHostAddress) Green $LogFile
  132.            log ("Printer $($PrinterName.name) is now attached to printer port: " + (Get-Printer -ComputerName $Computer | Where-Object { $_.PortName -like "$($Port.NewIP)" }).PortName) Green $LogFile
  133.  
  134.            Set-Printer -ComputerName $Computer -Name $PrinterName.name -Comment $($Port.NewIP)
  135.        }
  136.    }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement