Printer Install Script Hangs

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 6 months, 4 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #35640
    Profile photo of ian caine
    ian caine
    Participant

    I have just starting trying to teach myself Powershell and I am trying to write a script to use at work to install network printers. The script does work in that the printer is installed but it hangs at the add_printer stage before the $outputBox.text= "Printer with port $var installed" line. The printer is installed but the box never goes away and I have to kill Powershell ISE to get rid of it.
    Is there a way to end the install line to get it to move on ?

    Many thanks,
    Ian

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form = New-Object System.Windows.Forms.Form
    $Form.Size = New-Object System.Drawing.Size(600,400)

    ############################################## Start functions

    function addprinter {
    $var=$InputBox.text;
    pnputil.exe -a "D:\Powershell\Printer\pcl6-x64\hpbuio170l.inf" #get driver
    add-PrinterDriver -name "hp universal printing pcl 6" #install driver
    Add-PrinterPort -Name $var -printerhostaddress $var
    add-printer -name "HP LJ 400 $var" -portname $var -drivername "hp universal printing pcl 6"
    $outputBox.text= "Printer with port $var installed"

    } #end addprinter

    ############################################## end functions

    ############################################## Start text fields

    $InputBox = New-Object System.Windows.Forms.TextBox
    $InputBox.Location = New-Object System.Drawing.Size(20,100)
    $InputBox.Size = New-Object System.Drawing.Size(150,20)
    $Form.Controls.Add($InputBox)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,50)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "Please enter the information in the space below:"
    $Form.Controls.Add($objLabel)

    $outputBox = New-Object System.Windows.Forms.TextBox
    $outputBox.Location = New-Object System.Drawing.Size(10,150)
    $outputBox.Size = New-Object System.Drawing.Size(565,200)
    $outputBox.MultiLine = $True
    $outputBox.ScrollBars = "Vertical"
    $Form.Controls.Add($outputBox)

    ############################################## end text fields

    ############################################## Start buttons

    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(400,30)
    $Button.Size = New-Object System.Drawing.Size(75,50)
    $Button.Text = "Install"
    $Button.Add_Click({addprinter})
    $Form.Controls.Add($Button)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(400,80)
    $CancelButton.Size = New-Object System.Drawing.Size(75,50)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$Form.Close()})
    $Form.Controls.Add($CancelButton)

    ############################################## end buttons

    $Form.StartPosition = "CenterScreen"
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedToolWindow
    $Form.Text = "Install Printer by Hostname"
    $Form.Topmost = $True

    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()

    PS. this very much a work in progress so I will be adding in some checks to get rid of errors when port is already present etc.

    #35874
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I'd try to break this down and get out of the script context, because it's possible you're running into threading limitations.

    What happens if you just run Add-Printer interactively from the console to do the same thing? Does it hang then?

    #35877
    Profile photo of ian caine
    ian caine
    Participant

    Thanks for the reply Don, I actually got around to working this out just yesterday.
    The issue seems to be with the way that the default Powershell ISE runs, if I check the script in PowerGUI it works fine and when I saved the script off and ran it via a batch it also runs fine.

    #35878
    Profile photo of Don Jones
    Don Jones
    Keymaster

    And the ISE *does* work differently. I *always* test from the console!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.