none
Change local ip printer ports RRS feed

  • Question

  • So we are doing a complete ip changeover and I need a script to reference a list of old and new printer ip addresses, create a new port and then add the existing printer to that port.  I wrote a script in Powershell to handle this and then found out these cmdlets are only available in Windows 8+ and most of my clients are still on Windows 7.

    import-csv c:\test.csv |
     ForEach-Object{
      Try{
      if(Get-PrinterPort -Name $_.OldIP){
                    if($p=Get-Printer -Name $_.Name){
        Add-PrinterPort -Name $_.NewIP -PrinterHostAddress $_.NewIP -PortNumber 9100 -ea Stop
        $p| Set-Printer -PortName $_.NewIP -ea Stop
        Remove-PrinterPort -Name $_.OldIP -ea Stop
    }
       }
      }
      Catch{
       Throw $_
            }
        }

    Windows 7 has the built in printing admin scripts (prnport.vbs, prnmngr.vbs) so I'm thinking it would probably be best to use those or maybe use the win32_printer class, not really sure how to handle this and would really appreciate any help.

    Wednesday, February 24, 2016 3:57 PM

Answers

  • function Replace-PrinterPort
    {
        [CmdletBinding()]
       
        Param
        (
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$Computername,
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$OldIP,
               
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$NewIP,
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$Old_Port_Name
        )
        Begin
        {
        }
        Process
        {
         foreach($computer in $computername){
            write-verbose "Checking $computername"
            if ($this_old_port = Get-WmiObject -class win32_tcpipprinterport -ComputerName $computername -Property * | where HostAddress -EQ $oldIP){
                $old_port_name = $this_old_port.name
                Write-Verbose "$computername has a port on $oldIP named $old_port_name" -Verbose
                new-printerport -NewIP $NewIP -computername $computername
                write-verbose "A new port was made it's name and IP address are both $NewIP"
                if($this_printer = Get-WmiObject -class win32_printer -ComputerName $computername -Property * | where Portname -eq $old_port_name){
                    write-verbose "$computername has a printer attached to $old_port_name"
                    $this_printer.Portname = "$newIP"
                    write-verbose "now it is set to ($this_printer.PortName)"
                    $this_printer.put()
                    write-verbose "and the setting is written to the WMI"
                }
            }
         }
        }
        End
        {
        }
    }
    function  new-printerport {
        [CmdletBinding()]
        Param
        (
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $NewIP,
            $computername
        )
    $port = [wmiclass]"\\$computername\ROOT\cimv2:Win32_TcpIpPrinterPort"
    $port.psbase.scope.options.EnablePrivileges = $true
    $newPort = $port.CreateInstance()
    $newport.name = "$NewIP"
    $newport.Protocol = 1
    $newport.HostAddress = $NewIP
    $newport.PortNumber = "9100"
    $newport.SnmpEnabled = $false
    $newport.Put()
    }

    import-csv c:\users\bpconniff\desktop\IPMaster.csv | Replace-PrinterPort

    Seems to work...

    Wednesday, February 24, 2016 9:59 PM

All replies

  • I agree; it sounds like you need to restructure your solution so you have something that works on Windows 7.

    What have you tried so far, and with what results?

    We can help answer specific questions, but we don't have the resources to design a solution for you.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 24, 2016 5:21 PM
    Moderator
  • function Replace-PrinterPort
    {
        [CmdletBinding()]
       
        Param
        (
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$Computername,
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$OldIP,
               
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$NewIP,
                [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
                [string]$Old_Port_Name
        )
        Begin
        {
        }
        Process
        {
         foreach($computer in $computername){
            write-verbose "Checking $computername"
            if ($this_old_port = Get-WmiObject -class win32_tcpipprinterport -ComputerName $computername -Property * | where HostAddress -EQ $oldIP){
                $old_port_name = $this_old_port.name
                Write-Verbose "$computername has a port on $oldIP named $old_port_name" -Verbose
                new-printerport -NewIP $NewIP -computername $computername
                write-verbose "A new port was made it's name and IP address are both $NewIP"
                if($this_printer = Get-WmiObject -class win32_printer -ComputerName $computername -Property * | where Portname -eq $old_port_name){
                    write-verbose "$computername has a printer attached to $old_port_name"
                    $this_printer.Portname = "$newIP"
                    write-verbose "now it is set to ($this_printer.PortName)"
                    $this_printer.put()
                    write-verbose "and the setting is written to the WMI"
                }
            }
         }
        }
        End
        {
        }
    }
    function  new-printerport {
        [CmdletBinding()]
        Param
        (
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $NewIP,
            $computername
        )
    $port = [wmiclass]"\\$computername\ROOT\cimv2:Win32_TcpIpPrinterPort"
    $port.psbase.scope.options.EnablePrivileges = $true
    $newPort = $port.CreateInstance()
    $newport.name = "$NewIP"
    $newport.Protocol = 1
    $newport.HostAddress = $NewIP
    $newport.PortNumber = "9100"
    $newport.SnmpEnabled = $false
    $newport.Put()
    }

    import-csv c:\users\bpconniff\desktop\IPMaster.csv | Replace-PrinterPort

    Seems to work...

    Wednesday, February 24, 2016 9:59 PM