Powershell script to change printer IP address


netprintA question was posted in Powershell.org Q&A forum:

I have a weird question. Can you modify object properties. the command
Get-PrinterPort 192.* | fl *
prints out all the attributes of the printer port. but I can’t modify them. I don’t want to create a new port and start from scratch, just modify the existing object. Or maybe export the export and then import it, modified, into the new one.

This script does just that. It can be downloaded from the Microsoft Script Center Repository.

The reason we cannot modify the existing printer port object is that the Get-Printer commandlet uses the MSFT_TCPIpPrinterPort class. This class has the 2 properties we want to change (HostAddress and Name) but they’re read-only.

Even if we try to use the Win32_TCPIpPrinterPort class, as in:

$OldIP = “192.168.0.95”
$NewIP = “192.168.0.99”
$objPrinterPort = Get-WmiObject -Class Win32_TCPIpPrinterPort | Where-Object {$_.Name -eq “IP_$OldIP”}
$objPrinterPort.psbase.scope.options.EnablePrivileges = $true
$objPrinterPort.HostAddress = $NewIP
$objPrinterPort.Name = “IP_$NewIP”
$objPrinterPort.Put()

This does not modify the existing Printer Port, it creates a new one..

Example script usage:

.\Change-PrinterIP.ps1 192.168.0.95 192.168.0.99

Script output looks like:

Change-PrinterIP

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s