Why i need a second click on "OK" Button?

Tim Kristof 1 Reputation point
2022-05-27T08:44:47.923+00:00

I wrote a script, wich works fine. But i have to klick twice on d´the OK Button and dont know why.
Here is the Code:
$CancelText = 'Cancel'
$OKButtonText = 'OK'
$SystemWindowsFormsTextBox = 'System.Windows.Forms.TextBox'
$SystemWindowsFormsLabel = 'System.Windows.Forms.Label'
$SystemWindowsFormsButton = 'System.Windows.Forms.Button'
$SystemDrawingSize = 'System.Drawing.Size'
$0Field = '{0}'

function start-printercreation
{
  <#
      .SYNOPSIS
      EIn Drucker wird auf dem Prinserver erstellt

      .DESCRIPTION
      Aus der Abfrage des Formulars mit den Parametern 
      Druckername
      Druckerport 
      Treibername
      wird ein Drucker installiert.
      Die Druckertreiber werden zuvor aus den vorhandenen verfügbaren Druckertreibern ausgelesen.

      .EXAMPLE
      start-printercreation


      .NOTES
      Dieses Skript ist nur für die direkte Ausführung auf dem Printserver gedacht. Da auf den anderen Servern der Spoolerdienst deaktiviert ist.


  #>



  Add-PrinterPort -Name ($0Field -f $Printerport) -PrinterHostAddress ($0Field -f $Printerport) -PortNumber 9100
  Add-Printer -Name ($0Field -f $Printername) -DriverName ($0Field -f $Drivername) -PortName ($0Field -f $printerport) -Shared -ShareName ($0Field -f $Printername)


}



Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

$objForm = New-Object -TypeName System.Windows.Forms.Form
$objForm.Text = 'Dateneingabe'
$objForm.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (400,300)
$objForm.StartPosition = 'CenterScreen'
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq 'Enter') { $objForm.DialogResult='OK'
$objForm.Close()} })
$objForm.Add_KeyDown({if ($_.KeyCode -eq 'Escape') { $objForm.DialogResult='Cancel'
$objForm.Close()} })

$OKButton = New-Object -TypeName $SystemWindowsFormsButton
$OKButton.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,180)
$OKButton.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,23)
$OKButton.Text = $OKButtonText

$OKButton.DialogResult = $OKButtonText
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object -TypeName $SystemWindowsFormsButton
$CancelButton.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (150,180)
$CancelButton.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (75,23)
$CancelButton.Text = $CancelText
$CancelButton.DialogResult = $CancelText
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object -TypeName $SystemWindowsFormsLabel
$objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,20)
$objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20)
$objLabel.Text = 'Name des Druckers:'
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object -TypeName $SystemWindowsFormsTextBox
$objTextBox.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,40)
$objTextBox.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20)
$objTextBox.Text = 'Druckername'
$objForm.Controls.Add($objTextBox)

$objLabel = New-Object -TypeName $SystemWindowsFormsLabel
$objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,70)
$objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20)
$objLabel.Text = 'IP-Adresse des Druckers:'
$objForm.Controls.Add($objLabel)
$objTextBox2 = New-Object -TypeName $SystemWindowsFormsTextBox
$objTextBox2.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,90)
$objTextBox2.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20)
$objTextBox2.Text = '1.2.3.4'
$objForm.Controls.Add($objTextBox2)

$objLabel = New-Object -TypeName $SystemWindowsFormsLabel
$objLabel.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,120) 
$objLabel.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (280,20) 
$objLabel.Text = 'Treffen Sie bitte eine Auswahl:'
$objForm.Controls.Add($objLabel) 
$objCombobox = New-Object -TypeName System.Windows.Forms.Combobox 
$objCombobox.Location = New-Object -TypeName $SystemDrawingSize -ArgumentList (10,140) 
$objCombobox.Size = New-Object -TypeName $SystemDrawingSize -ArgumentList (260,20) 

$Printerdrivers = (Get-PrinterDriver).Name
foreach ($printerdriver in $printerdrivers)
{ 
  $null =  $objCombobox.Items.Add(($0Field -f $Printerdriver))
}
$objCombobox.Height = 70
$objForm.Controls.Add($objCombobox) 
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
$null =  $objForm.ShowDialog()
$Drivername = $objCombobox.SelectedItem
$Printerport = $objTextBox2.Text
$Printername = $objTextBox.Text





$null =  $objForm.ShowDialog()

If ($objForm.DialogResult -like $OKButton) {start-Printercreation} else {'Abbruch geklickt'}

Could you help me?

Greets form Hamburg

Tim

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,354 questions
{count} votes