cancel button does not work in create-printer script

Tim van der Schee 1 Reputation point
2022-11-26T11:47:28.353+00:00

hi,

i've created a powershell script that will create a printer, with ip adress, driver, etc, and creates a AD group for that printer
the GUI askes for that info by some text boxes and pull down menu's, and has an 'ok' and 'cancel' button.
Pressing OK the script will run, and it working perfectly, i'm very happy with that.

but pressing Cancel runs the script also. even when i press the X on the right top the script will run.

how can i get the cancel button stop the script?

printerscript

Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Add new Printer'
$form.Size = New-Object System.Drawing.Size(500,350)
$form.StartPosition = 'CenterScreen'

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(30,30)
$label1.Size = New-Object System.Drawing.Size(130,20)
$label1.Text = 'IP adress:'
$label1.Font = 'Microsoft Sans Serif,10'
$form.Controls.Add($label1)

$textBox1 = New-Object system.Windows.Forms.TextBox
$textBox1.location = New-Object System.Drawing.Point(170,30)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$textBox1.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($textBox1)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(30,60)
$label2.Size = New-Object System.Drawing.Size(130,20)
$label2.Text = 'Printername:'
$label2.Font = 'Microsoft Sans Serif,10'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(170,60)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$textBox2.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($textBox2)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(30,90)
$label3.Size = New-Object System.Drawing.Size(130,20)
$label3.Text = 'Printserver:'
$label3.Font = 'Microsoft Sans Serif,10'
$form.Controls.Add($label3)

$textBox3 = New-Object system.Windows.Forms.ComboBox
$textBox3.Location = New-Object System.Drawing.Point(170,90)
$textBox3.Size = New-Object System.Drawing.Size(260,30)
$textBox3.text = ""
@('PS01','PS02','KOPP-PS01','KOPP-PS03','BARTENDER01','BARTENDER01-T','XA-SILO01') | ForEach-Object {[void] $textBox3.Items.Add($_)}
$textBox3.SelectedIndex = 0
$textBox3.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($textBox3)

$PrinterModelLabel = New-Object system.Windows.Forms.Label
$PrinterModelLabel.location = New-Object System.Drawing.Point(30,120)
$PrinterModelLabel.Size = New-Object System.Drawing.Size(130,20)
$PrinterModelLabel.text = "Model:"
$PrinterModelLabel.Font = 'Microsoft Sans Serif,10,'
$form.Controls.Add($PrinterModelLabel)

$PrinterModelInput = New-Object system.Windows.Forms.ComboBox
$PrinterModelInput.location = New-Object System.Drawing.Point(170,120)
$PrinterModelInput.Size = New-Object System.Drawing.Size(260,20)
$PrinterModelInput.text = ""
@('Ricoh','Zebra') | ForEach-Object {[void] $PrinterModelInput.Items.Add($_)}
$PrinterModelInput.SelectedIndex = 0
$PrinterModelInput.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($PrinterModelInput)

$Driver = New-Object system.Windows.Forms.Label
$Driver.location = New-Object System.Drawing.Point(30,150)
$Driver.Size = New-Object System.Drawing.Size(130,20)
$Driver.text = "Driver:"
$Driver.Font = 'Microsoft Sans Serif,10,'
$form.Controls.Add($Driver)

$DriverInput = New-Object system.Windows.Forms.ComboBox
$DriverInput.location = New-Object System.Drawing.Point(170,150)
$DriverInput.Size = New-Object System.Drawing.Size(260,20)
$DriverInput.text = ""
@('PCL6 Driver for Universal Print','ZDesigner ZD420-300dpi ZPL', 'ZDesigner ZD420-300dpi EPL') | ForEach-Object {[void] $DriverInput.Items.Add($_)}
$DriverInput.SelectedIndex = 0
$DriverInput.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($DriverInput)

$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(30,180)
$label5.Size = New-Object System.Drawing.Size(130,20)
$label5.Text = 'Location:'
$label5.Font = 'Microsoft Sans Serif,10'
$form.Controls.Add($label5)

$textBox5 = New-Object System.Windows.Forms.TextBox
$textBox5.Location = New-Object System.Drawing.Point(170,180)
$textBox5.Size = New-Object System.Drawing.Size(260,30)
$textBox5.Font = 'Microsoft Sans Serif,12'
$form.Controls.Add($textBox5)

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(170,210)
$okButton.Size = New-Object System.Drawing.Size(130,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

THIS IS THAT CANCEL BUTTON###

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(300,210)
$cancelButton.Size = New-Object System.Drawing.Size(130,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

THIS IS THAT CANCEL BUTTON###

$form.Topmost = $true

$form.Add_Shown({$textBox1.Select()})
$result = $form.ShowDialog()

$PrinterAddress = $textBox1.Text  
$PrinterName = $textBox2.Text  
$Printserver = $textBox3.Text  
$Driver = $DriverInput.Text  
$Location = $textBox5.Text  
$Model = $PrinterModelInput.Text  

$Title = " Settings"
$Body = " op printserver $Printserver
is printermodel $Model
met $Driver
met printernaam $PrinterName
met ipadres $PrinterAddress
op locatie $Location

    aangemaakt. is dit correct?   

"

$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Disclaimer = $Shell.popup("$Body",15,"$Title",0)
$Disclaimer | Out-Null

$Title = " Creating printer"
$Body = " Creating $PrinterName Printerqueue"
$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Disclaimer = $Shell.popup("$Body",5,"$Title",0)
$Disclaimer | Out-Null

Add-PrinterPort -ComputerName $PrintServer -Name $PrinterAddress -PrinterHostAddress $PrinterAddress
Add-Printer -ComputerName $PrintServer -Name $PrinterName -DriverName $Driver -port $PrinterAddress -Shared -ShareName $PrinterName -RenderingMode SSR -Location $Location -Comment $PrinterModel

$Title = " Finished creating printerqueue"
$Body = " Finished creating Printer $PrinterName Printerqueue
on server $PrintServer"
$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Disclaimer = $Shell.popup("$Body",5,"$Title",0)

cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prncnfg.vbs -t -p $PrinterName -r $PrinterAddress -s $PrintServer +shared -direct -enablebidi

$Title = " Creating printgroup"
$Body = " Creating AD Group PRT - $PrinterName in Printer OU"
$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Disclaimer = $Shell.popup("$Body",5,"$Title",0)

New-ADGroup -Name "PRT - $PrinterName" -GroupCategory Security -GroupScope Global -DisplayName "PRT - $PrinterName" -Path "OU=Printer Groups,OU=Groups,OU=Koppert,DC=Koppert,DC=int" -Description "$LocationPrinterModel"

$Title = " Finished creating printgroup"
$Body = " Finished creating AD Group PRT - $PrinterName in Printer OU"
$Shell = new-object -comobject wscript.shell -ErrorAction Stop
$Disclaimer = $Shell.popup("$Body",5,"$Title",0)

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 47,886 Reputation points
    2022-11-27T15:46:37.327+00:00

    Where do you check the result of the "ShowDialog"? You store the result in a variable "$result" but you don't seem to check if the value is 'Cancel' or "OK". If it's 'Cancel', don't run "the script".

    0 comments No comments

  2. Tim van der Schee 1 Reputation point
    2022-11-28T13:39:08.227+00:00
    $Title = " Settings"  
    $Body = " op printserver $Printserver  
    is printermodel $Model  
    met $Driver  
    met printernaam $PrinterName  
    met ipadres $PrinterAddress  
    op locatie $Location  
      
         aangemaakt. is dit correct?   
    "  
      
    $Shell = new-object -comobject wscript.shell -ErrorAction Stop  
    $Disclaimer = $Shell.popup("$Body",15,"$Title",0)  
    $Disclaimer | Out-Null  
    

    I'd rather have an 'NO' in this window.... because in this window there is an 'OK' button (where does that come from????) and i would like an 'cancel' too...

    because the question is 'is this correct'... and if the answer is no, ths script should stop...

    can anyone tell me how to do that?

    0 comments No comments

  3. Rich Matheisen 47,886 Reputation points
    2022-11-28T15:49:06.357+00:00

    Use the appropriate value instead of "0" in the intType parameter. See here: wshshell-popup.html

    0 comments No comments

  4. Tim van der Schee 1 Reputation point
    2022-11-28T16:08:18.073+00:00

    yes, almost there, and choosing 'no' the script should restart (so the textboxes can be altered/filled in again) in stead of continue...


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.