header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Printer Ports for a newbie
Last Post 24 Nov 2010 12:18 PM by Drack69. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
seroklesUser is Offline
New Member
New Member
Posts:9
Avatar

--
19 Aug 2010 08:13 AM
    We have a fair amount of citrix servers in our environment.  Over the years VLANs have been changed, address schemes changed and various other bits and pieces.  Some housework has been omitted during these changes and we now have a somewhat confusing printer situation in terms of ports.

    What I was looking to do with Powershell was remotely remove all the existing printer ports, create new ones and then assign these to the respective printers. 

    Using the script repository at microsoft I have found many scripts but not many in Powershell.  So far I can create TCP/IP printer ports and get a list of the printer ports.  I was wondering if a tweak of the creation script might allow it to delete a specific port though consulting the pocket guide to Powershell I may have jumped the gun on that.  Also I am struglging to work out how to assign the newly created port to a particular printer?  Does anyone have any help or guidance for someone who is desperately out of their depth?

    Regards

    portcreate.txt

    Drack69User is Offline
    New Member
    New Member
    Posts:24
    Avatar

    --
    18 Nov 2010 08:54 PM
    Not sure if you are still looking for this but here is a basic create a printer and port that I use.
    It can be modified with some more variables and such this is just to get the basics.

    #============== Start Script ================
    $PortName = MyPortName"
    $PrintDriver = "HP Laserjet 5500"

    ## Create the port
    $objPort = [wmiclass]"Win32_TcpIpPrinterPort"
    $objNewPort = $objPort.CreateInstance()
    $objNewPort.psbase.scope.options.EnablePrivileges = $true ##if using W2K3
    $objNewPort.Name = $PortName
    $objNewPort.HostAddress = 10.10.10.10
    $objNewPort.Protocol = 1 #1=RAW (default) 2=LPR
    $objNewPort.put()

    ## Creat Printer
    $objPrint = [WMICLASS]"Win32_Printer"
    $objPrint.psbase.scope.options.EnablePrivileges = $true
    $objNewPrinter = $objPrint.createInstance()
    $objNewPrinter.DeviceID = "My_Printer_Name"
    $objNewPrinter.DriverName = $PrintDriver
    $objNewPrinter.PortName = $PortName
    $objNewPrinter.Shared = $true
    $objNewPrinter.ShareName = "PrinterName"
    $objNewPrinter.Put()

    #============== End Script ================

    After creating the instance for the port and/or printer you can type in
    $objPort
    or
    $objPrint
    This will show you all the other properties you vcan add like the comment section on the printer.
    I have a full script if you are interested that will use a CSV file and loop through creating printers and queues.
    Takes a few seconds for each printer. I use it when I have a large number of printers to create and as a way for DR on print servers.



    seroklesUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    24 Nov 2010 06:54 AM
    Thanks for that it looks exactly like what I was trying to achieve at the time.  Unfortunately the time it was taken me to work all that out outstripped the time to just go through all the servers manually so I had to bite the bullet.  I would be very interested in your script that communicates from a csv though as we have an office move coming up and no doubt some level of recovery will have to be implemented at some stage during the move!

    Regards

    Tim


    Drack69User is Offline
    New Member
    New Member
    Posts:24
    Avatar

    --
    24 Nov 2010 12:18 PM
    It works well for creating multiple printers really quick. It's a decent backup/DR method but an even better one would be using the PrintMig utility from Microsoft that will zip up queues, drivers, shares, etc. Then you can use it to dump everything back. I have is scripted out in a BAT file. I schedule a task to run the backup and RoboCopy it off to a remote location once a month on all out print servers.
    I'll try to attach my printer creating script. if you want more info on the PrintMig let me know.

    There is one field at the bottom you will want to modify and that's the variable that give the location of the CSV file
    $script:strCSVInputFile = "c:\temp\Printer_Input.csv" change it to whatever you want.



    #* FileName: Create_Print_Queue.ps1
    #*=============================================================================
    #* Script Name: [Create_Print_Queue]
    #* Created: [3/25/10]
    #* Author: [Johnny Leuthard]
    #* Reqrmnts: [Drivers have to be on server. W2k-w2k8, win7, vista, W2K3]
    #* Keywords: [print queue port]
    #*=============================================================================
    #* Purpose: Create print queues from an input file (CSV)
    #*=============================================================================
    #*=============================================================================
    #* Beginning OF SCRIPT
    #*=============================================================================
    Clear-Host
    Write-Host

    #*=============================================================================
    #* FUNCTION LISTING
    #*=============================================================================
    #* Function: [Get_Info_From_CSV]
    #* Created: [3/28/10]
    #* Author: [Johnny Leuthard]
    #* Arguments:
    #*=============================================================================
    #* Purpose: Get info from an XLS file
    #*=============================================================================
    Function Get_Info_From_CSV
    {
    ## Create arrays to store info
    $script:strPrinterName = @()
    $script:strPortName = @()
    $script:strIPAddy = @()
    $script:strLocation = @()
    $script:strComment = @()
    $script:strPrintDriver = @()

    #Import data from a CSV ignoring any blank lines
    $strCSVdata = Import-Csv $strCSVInputFile | where {($_.Printername -ne "")}
    #Export data to XML
    #($strCSVdata | convertto-xml).save("\\dal1amspif006\d$\Admin\Tools\PowerShell_Scripts\TextFiles\test.xml")

    ## loop through and load up the arrays with the data from the CSV
    Foreach ($i in $strCSVdata)
    {
    $script:strPrinterName += $i.PrinterName
    $script:strPortName += $i.PortName
    $script:strIPAddy += $i.IPAddress
    $script:strLocation += $i.Location
    $script:strComment += $i.Comment
    $script:strPrintDriver += $i.PrintDriver
    }
    }#End Function


    #*=============================================================================
    #* FUNCTION LISTING
    #*=============================================================================
    #* Function: [Create_Port]
    #* Created: [3/25/10]
    #* Author: [Johnny Leuthard]
    #* Arguments:
    #*=============================================================================
    #* Purpose: Creat a TCP/IP port
    #*=============================================================================
    Function Create_Port
    {
    ##?? This works for both win7, 2008, w2k3.
    Write-Host ("`tCreating " + $strPrinterName.Count + " Printer Port's`t") -backgroundcolor black -ForegroundColor Green
    $objPort = [wmiclass]"Win32_TcpIpPrinterPort"
    $objPort.psbase.scope.options.EnablePrivileges = $true

    #Loop through and create each port
    For($i=0; $i -lt $strPortName.count; $i++)
    {
    $objNewPort = $objPort.CreateInstance()
    $objNewPort.psbase.scope.options.EnablePrivileges = $true
    $objNewPort.Name = $strPortName[$i]
    $objNewPort.HostAddress = $strIPAddy[$i]
    $objNewPort.Protocol = 1 #1=RAW (default) 2=LPR
    #$objNewPort.put() | Out-Null
    $objNewPort.put()
    }

    }#End Function

    #*=============================================================================
    #* FUNCTION LISTING
    #*=============================================================================
    #* Function: [Create_Queue]
    #* Created: [3/25/10]
    #* Author: [Johnny Leuthard]
    #* Arguments:
    #*=============================================================================
    #* Purpose: Create a print queue
    #*=============================================================================
    Function Create_Queue
    {
    ## ?? works for Win7, W2k8,W2K3
    Write-Host ("`tCreating " + $strPrinterName.Count + " Printer's`t") -backgroundcolor black -ForegroundColor Green
    $objPrint = [WMICLASS]"Win32_Printer"
    $objPrint.psbase.scope.options.EnablePrivileges = $true

    ## Loop through and create each printer
    For($i=0; $i -lt $strPrinterName.count; $i++)
    {
    $objNewPrinter = $objPrint.createInstance()
    $objNewPrinter.DeviceID = $strPrinterName[$i] ## This is the printer name
    $objNewPrinter.DriverName = $strPrintDriver[$i]
    $objNewPrinter.PortName = $strPortName[$i]
    $objNewPrinter.Location = $strLocation[$i]
    $objNewPrinter.Comment = $strComment[$i]
    $objNewPrinter.Shared = $true
    $objNewPrinter.ShareName = $strPrinterName[$i]
    $objNewPrinter.KeepPrintedJobs = $false
    $objNewPrinter.Published = $true ## Publish in AD
    $objNewPrinter.DoCompleteFirst = $true ## Print spooled doc's first
    $objNewPrinter.Queued = $true ## Start printng after last page is spooled
    #$objNewPrinter.WorkOffline = $true ## Allows jobs to be sent to queue if printer is off-line
    #$objNewPrinter.Default = $false
    $objNewPrinter.Put()

    ## Add Security group
    ## Not completed yet
    }
    }#End Function

    #*=============================================================================
    #* SCRIPT BODY
    #*=============================================================================

    ## location of CSV file
    $script:strCSVInputFile = "C:\Temp\Printer_Input.csv"

    ## Call Get_Info_From_CSV
    Get_Info_From_CSV
    ## Call Create_Port Function
    Create_Port
    ## Call Create_Queue function
    Create_Queue

    #*=============================================================================
    #* END OF SCRIPT
    #*=============================================================================
    #*=============================================================================
    #* Notes: All text below here is commented out
    #*=============================================================================
    <#

    Driver names have to be exactly as they appear on a system. If you right click the printer, choose properties, it's exactly what is says for "Model"
    I just hilight that and copy and paste so i have it rught.

    Also all drivers have to be preinstalled on the server. Future revisions will hopefully grab them from a pool of drivers and install when needed but not right now

    Example of CSV file
    =======================
    PrinterName,PortName,IPAddress,Location,Comment,PrintDriver
    Test_Printer_1,Test_Port_1,127.0.0.1,Nowhere_1,Comment 1,HP Laserjet 4300 PCL 6
    Test_Printer_2,Test_Port_2,127.0.0.2,Nowhere_2,Comment 2,HP Laserjet 4300 PCL 6

    #>







    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer