locked
PowerShell Form Cancel button RRS feed

  • Question

  • Hi team!

    I am writing a script to install an app in unattend fashion way... I have all the code working, but I wish to put a cancel button meanwhile the script is running and showing the progress bar... like a abort emergency button.
    Plus, the button must be allowed to have a function to kill all sub proccess... such as a msiexec installation, bitstransfer, wmi querys, etc...

    But the button is called as a dialog button... so the script stops and keeps waiting for the button being pressed.

    Here is an example code:

    # Cleaning the environment: Remove-Variable * -ErrorAction SilentlyContinue $scriptversion = 2.5 # # Make Exit-Setupt function: Function Exit-Setup { Set-Variable -Name ExitSetup -Value "Var to exit setup" -Scope Global } # # --- End Showing the Initial Run Question message-- # # Exiting if cancell setup: If ($ExitSetup) {Exit} function CancelButton {[System.Environment]::Exit(0)} # # --- Showing the Installing message ---: # Main Window [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $Form = New-Object System.Windows.Forms.Form #$Form.FormBorderStyle ="None" $Form.ControlBox = $false $form.TopMost = $True $Form.ShowInTaskbar = $false $Form.text = "Setup" $Form.Size = New-Object System.Drawing.Size(590,350) $form.StartPosition = "CenterScreen" $form.BackColor = "#749395" # # Close button $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Size(450,200) $cancelButton.Size = New-Object System.Drawing.Size(75,75) $cancelButton.FlatStyle = "Flat" $cancelButton.BackColor = "Gray" $cancellFont = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold) # Font styles are: Regular, Bold, Italic, Underline, Strikeout $cancelButton.Font = $cancellFont $cancelButton.Text = "Cancel" $cancelButton.tabindex = 0 $cancelButton.Add_Click({$form.Tag = $form.close()}) $form.CancelButton = $CancelButton $form.Controls.Add($CancelButton) # # Body message: $outputBox = New-Object System.Windows.Forms.Label #$outputBox = New-Object System.Windows.Forms.TextBox $outputBox.Location = New-Object System.Drawing.Size(50,200) $outputBox.Size = New-Object System.Drawing.Size(350,75) #$outputBox.MultiLine = $True #$outputBox.ScrollBars = "Vertical" #$outputBox.BackColor = "Red" $Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold) # Font styles are: Regular, Bold, Italic, Underline, Strikeout $outputBox.Font = $Font $outputBox.text=" Please wait, installing some app ERP...`r`n`r`n` To stop the installation press `"Cancel`" button." $Form.Controls.Add($outputBox) # # Progress bar: # Create label $labelprogressbar = New-Object system.Windows.Forms.Label $labelprogressbar.Left=50 $labelprogressbar.Top= 60 $labelprogressbar.Width= 475 #adjusted height to accommodate progress bar $labelprogressbar.Height=100 $Fontprogressbar = New-Object System.Drawing.Font("Calibri",10,[System.Drawing.FontStyle]::Regular) # Font styles are: Regular, Bold, Italic, Underline, Strikeout $labelprogressbar.Font= $Fontprogressbar #optional to show border $labelprogressbar.BorderStyle=2 #add the label to the form $form.controls.add($labelprogressbar) $progressBar1 = New-Object System.Windows.Forms.ProgressBar $progressBar1.Size = New-Object System.Drawing.Size(475,10) $progressBar1.Name = 'progressBar1' #$progressBar1.Value = 0 $progressBar1.Style="Continuous" $progressBar1.Minimum = 0 $progressBar1.Maximum = 100 $progressBar1.Left = 50 $progressBar1.Top = 170 $form.Controls.Add($progressBar1) # # Updateing progress bar status: $labelprogressbar.Text = "Runing at 30%" $progressBar1.Value = 30 #$form.Refresh() #$form.Show() #$Form.Update() Start-Sleep 2 Write-Host "doing something..." # Refresh Window to display message: $Form.Add_Shown({$Form.Activate()}) [void] $Form.ShowDialog() $form.Show() Start-Sleep 2 # Updateing progress bar status: $labelprogressbar.Text = "Runing at 50%" $progressBar1.Value = 50 #$form.Refresh() #$form.Show() #$Form.Update() Start-Sleep 2 Write-Host "doing something else..." Start-Sleep 2

    exit


    Is possible to call the button not as a dialog? I couldn't found any about that...
    And how can I make a function to kill all sub process?

    Many thanks for your time!




    • Edited by Victor M$ Friday, October 11, 2019 8:45 AM
    Friday, October 11, 2019 8:40 AM

Answers

  • To set you on a better path I did a quick job of cleaning up your form and removed all unnecessary code as well as fixing some syntax errors.  It is clear that you have copied much of this from some very old examples that were not very good to begin wit.

    You will need to add the code you want to use to do you install using the article I posted.  The solution you seek is clearly illustrated in the article.

    You will also have to learn PowerShell to a higher level of expertise to design and use forms as well as learn how to work with forms:

    For learning forms start here: https://docs.microsoft.com/en-us/dotnet/framework/winforms/

    There is no PowerShell specific documentation for Forms.  The Sapien site is the best resource that exists so far.

    function Show-Form{
    	Add-Type -AssemblyName System.Windows.Forms
    	
    	$form = New-Object System.Windows.Forms.Form
    	$form.ControlBox = $false
    	$form.TopMost = $True
    	$form.ShowInTaskbar = $false
    	$form.text = 'Setup'
    	$form.Size = '590,350'
    	$form.StartPosition = 'CenterScreen'
    	$form.BackColor = '#749395'
    	$form.Add_Shown({$form.Activate()})
    
    
    	# Close button
    	$cancelButton = New-Object System.Windows.Forms.Button
    	$cancelButton.Location = '450,200'
    	$cancelButton.Size = '75,75'
    	$cancelButton.FlatStyle = 'Flat'
    	$cancelButton.BackColor = 'Gray'
    	$cancellFont = New-Object System.Drawing.Font('Calibri',11,[System.Drawing.FontStyle]::Bold)
    
    	# Font styles are: Regular, Bold, Italic, Underline, Strikeout
    	$cancelButton.Font = $cancellFont
    	$form.Controls.Add($CancelButton)
    	$cancelButton.Text = 'Cancel'
    	$cancelButton.tabindex = 0
    	$cancelButton.Add_Click({$form.Tag = $form.close()})
    	$form.CancelButton = $CancelButton
    
    	# Body message:
    	$outputBox = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($outputBox)
    	$outputBox.Location = '50,200'
    	$outputBox.Size = '350,75'
    	$outputBox.Font = New-Object System.Drawing.Font('Calibri',11,[System.Drawing.FontStyle]::Bold)
    	$outputBox.text = "       Please wait, installing some app ERP...`r`n`r`n`          To stop the installation press `'Cancel`' button."
    
    	# Create label
    	$labelprogressbar = New-Object system.Windows.Forms.Label
    	$form.controls.add($labelprogressbar)
    	$labelprogressbar.Left=50
    	$labelprogressbar.Top= 60
    	$labelprogressbar.Width= 475
    	$labelprogressbar.Height=100 #adjusted height to accommodate progress bar
    	$labelprogressbar.Font= New-Object System.Drawing.Font('Calibri',10,[System.Drawing.FontStyle]::Regular)
    	$labelprogressbar.BorderStyle=2 #optional to show border
    
    	
    	# Progress bar:
    	$progressBar1 = New-Object System.Windows.Forms.ProgressBar
    	$form.Controls.Add($progressBar1)
    	$progressBar1.Size = '475,10'
    	$progressBar1.Name = 'progressBar1'
    	$progressBar1.Style='Continuous'
    	$progressBar1.Minimum = 0
    	$progressBar1.Maximum = 100
    	$progressBar1.Left = 50
    	$progressBar1.Top = 170
    
    	# Updateing progress bar status:
    	$labelprogressbar.Text = 'Runing at 30%'
    	$progressBar1.Value = 30
    
    	Write-Host 'doing something...'
    
    	[void] $form.ShowDialog()
    }
    Show-Form
    
    In PowerShell and all languages a function that declares variables "scopes" the variable.  When the function exits the variables are removed automatically.  This is one reason I recommend that you start by learning PowerShell.

    \_(ツ)_/


    • Edited by jrv Friday, October 11, 2019 4:26 PM
    • Marked as answer by Victor M$ Wednesday, October 30, 2019 4:46 PM
    Friday, October 11, 2019 4:24 PM

All replies

  • You cannot access forms controls while an event is blocking the form.

    To cancel a process use "Stop-Process".

    Here is an article that will show you some of the issues you will have to code for:

    https://info.sapien.com/index.php/guis/gui-advanced-tips/powershell-studio-creating-responsive-forms


    \_(ツ)_/

    Friday, October 11, 2019 4:00 PM
  • To set you on a better path I did a quick job of cleaning up your form and removed all unnecessary code as well as fixing some syntax errors.  It is clear that you have copied much of this from some very old examples that were not very good to begin wit.

    You will need to add the code you want to use to do you install using the article I posted.  The solution you seek is clearly illustrated in the article.

    You will also have to learn PowerShell to a higher level of expertise to design and use forms as well as learn how to work with forms:

    For learning forms start here: https://docs.microsoft.com/en-us/dotnet/framework/winforms/

    There is no PowerShell specific documentation for Forms.  The Sapien site is the best resource that exists so far.

    function Show-Form{
    	Add-Type -AssemblyName System.Windows.Forms
    	
    	$form = New-Object System.Windows.Forms.Form
    	$form.ControlBox = $false
    	$form.TopMost = $True
    	$form.ShowInTaskbar = $false
    	$form.text = 'Setup'
    	$form.Size = '590,350'
    	$form.StartPosition = 'CenterScreen'
    	$form.BackColor = '#749395'
    	$form.Add_Shown({$form.Activate()})
    
    
    	# Close button
    	$cancelButton = New-Object System.Windows.Forms.Button
    	$cancelButton.Location = '450,200'
    	$cancelButton.Size = '75,75'
    	$cancelButton.FlatStyle = 'Flat'
    	$cancelButton.BackColor = 'Gray'
    	$cancellFont = New-Object System.Drawing.Font('Calibri',11,[System.Drawing.FontStyle]::Bold)
    
    	# Font styles are: Regular, Bold, Italic, Underline, Strikeout
    	$cancelButton.Font = $cancellFont
    	$form.Controls.Add($CancelButton)
    	$cancelButton.Text = 'Cancel'
    	$cancelButton.tabindex = 0
    	$cancelButton.Add_Click({$form.Tag = $form.close()})
    	$form.CancelButton = $CancelButton
    
    	# Body message:
    	$outputBox = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($outputBox)
    	$outputBox.Location = '50,200'
    	$outputBox.Size = '350,75'
    	$outputBox.Font = New-Object System.Drawing.Font('Calibri',11,[System.Drawing.FontStyle]::Bold)
    	$outputBox.text = "       Please wait, installing some app ERP...`r`n`r`n`          To stop the installation press `'Cancel`' button."
    
    	# Create label
    	$labelprogressbar = New-Object system.Windows.Forms.Label
    	$form.controls.add($labelprogressbar)
    	$labelprogressbar.Left=50
    	$labelprogressbar.Top= 60
    	$labelprogressbar.Width= 475
    	$labelprogressbar.Height=100 #adjusted height to accommodate progress bar
    	$labelprogressbar.Font= New-Object System.Drawing.Font('Calibri',10,[System.Drawing.FontStyle]::Regular)
    	$labelprogressbar.BorderStyle=2 #optional to show border
    
    	
    	# Progress bar:
    	$progressBar1 = New-Object System.Windows.Forms.ProgressBar
    	$form.Controls.Add($progressBar1)
    	$progressBar1.Size = '475,10'
    	$progressBar1.Name = 'progressBar1'
    	$progressBar1.Style='Continuous'
    	$progressBar1.Minimum = 0
    	$progressBar1.Maximum = 100
    	$progressBar1.Left = 50
    	$progressBar1.Top = 170
    
    	# Updateing progress bar status:
    	$labelprogressbar.Text = 'Runing at 30%'
    	$progressBar1.Value = 30
    
    	Write-Host 'doing something...'
    
    	[void] $form.ShowDialog()
    }
    Show-Form
    
    In PowerShell and all languages a function that declares variables "scopes" the variable.  When the function exits the variables are removed automatically.  This is one reason I recommend that you start by learning PowerShell.

    \_(ツ)_/


    • Edited by jrv Friday, October 11, 2019 4:26 PM
    • Marked as answer by Victor M$ Wednesday, October 30, 2019 4:46 PM
    Friday, October 11, 2019 4:24 PM
  • Sorry for my delayed reply...

    Yep, the code was a dirty copy/paste from lab... just for you catch the idea... I should have cleaned up before.

    Anyway... after read carefully both links, can't believe that it be so hard to code and it wasn't taked in mind something like I need... I think it isn't so extrange... Bad luck for me..  I will supress the "abort" button.

    Many thanks for your time!
    Wednesday, October 30, 2019 4:46 PM