none
Powershell script for 2008 R2 to change and select new port / ip address RRS feed

  • Question

  • I currently have a script to add a new standard tcp/ip port and ip address to a printer but I can't figure out a way to have it select the new port / ip address.  I saw a few scripts for 2012 but this print server is running 2008 R2.  I am going to be changing the ip address of many printers on a print server and need a way to do this quick.  Is there a command to do this?

    Here is the code I am using

    # ------------------------------------------------------------------------
    # NAME: AddPrinterPort.ps1
    # AUTHOR: ed wilson, Microsoft
    # DATE: 10/23/2009
    #
    # KEYWORDS: wmi, printing, printer ports and printer drivers
    #
    # COMMENTS: This script uses the wmi class accelerator
    # to create a new tcp / ip printer port on a local comptuer.
    # To use you will need to modify the ip address of the port
    #
    # ------------------------------------------------------------------------
    $ip = "10.5.6.80"
    $port = [wmiclass]"Win32_TcpIpPrinterPort"
    $port.psbase.scope.options.EnablePrivileges = $true
    $newPort = $port.CreateInstance()
    $newport.name = "$ip"
    $newport.Protocol = 1
    $newport.HostAddress = $ip
    $newport.PortNumber = "9100"
    $newport.SnmpEnabled = $false
    $newport.Put()

    Wednesday, February 25, 2015 5:50 PM

Answers

  • Hi,

    try this:

    $Printer = Get-WmiObject Win32_Printer | Where {$_.Name -like "NameOfPrinter" }
    $Printer.PortName = $ip
    $Printer.Put()

    Cheers,
    Fred


    There's no place like 127.0.0.1



    • Proposed as answer by jrv Wednesday, February 25, 2015 9:04 PM
    • Edited by FWN Thursday, February 26, 2015 6:12 PM
    • Marked as answer by mycow1 Thursday, February 26, 2015 7:43 PM
    Wednesday, February 25, 2015 7:19 PM

All replies

  • Hi,

    try this:

    $Printer = Get-WmiObject Win32_Printer | Where {$_.Name -like "NameOfPrinter" }
    $Printer.PortName = $ip
    $Printer.Put()

    Cheers,
    Fred


    There's no place like 127.0.0.1



    • Proposed as answer by jrv Wednesday, February 25, 2015 9:04 PM
    • Edited by FWN Thursday, February 26, 2015 6:12 PM
    • Marked as answer by mycow1 Thursday, February 26, 2015 7:43 PM
    Wednesday, February 25, 2015 7:19 PM
  • I have an old VB script that did something similar.  After creating the port it creates a printer object assigning the new port to it.  Since it uses WMI I am sure something similar can be done in Powershell.

    Set objPrinter = WMIService.Get("Win32_Printer").SpawnInstance_
    objPrinter.DriverName = strDriver
    objPrinter.PortName = strPortName
    objPrinter.DeviceID = strWhateverName
    objPrinter.Network = True
    objPrinter.Shared = False
    objPrinter.Put_

    Wednesday, February 25, 2015 7:22 PM
  • I used your code but it did not work.  See what I did below.  I'm not very good at powershell.

    I simply need it to add a new port with new ip address and assign it to the printer, not affecting users.

    $ip = "10.5.6.82"
    $Printer = Get-WmiObject | Where {$_.Name -like "isdlab1" }
    $Printer.PortName = $ip
    $Printer.Put()

    Thursday, February 26, 2015 5:07 PM
  • Hi,

    just to be sure: You did create the tcpipprinterport, as shown in your own code, right (so there existed a port named "10.5.6.82" when you ran the part you just posted)?

    Did it throw any error?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 26, 2015 5:36 PM
  • Ok, I added the code you gave me to the bottom of my code and it came back asking for a class value.

    cmdlet Get-Wmiobject at command pipeline position 1

    Supply values for the following parameters:

    Class:

    Thursday, February 26, 2015 5:47 PM
  • I am using powershell v2
    Thursday, February 26, 2015 6:01 PM
  • Hi,

    hm, something is a bit odd with Technet these days - I'm fairly sure I specified the class when I posted it. The Class is "Win32_Printer", you add it behind the Get-WmiObject cmdlet (I updated my post - let's hope it stays fixed).

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 26, 2015 6:14 PM
  • Thanks it worked.
    Thursday, February 26, 2015 7:44 PM