Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- # Inspired by https://pastebin.com/UsftjYPK this is the outline for a status bar window for more complex powershell scripts.
- # Create a custom PowerShell object for the form
- $formObject = [PSCustomObject]@{
- Form = $null
- Label = $null
- ProgressBar = $null
- UpdateProgress = {
- param($progress)
- if ($this.ProgressBar -ne $null) {
- $this.ProgressBar.Value = $progress
- }
- }
- UpdateLabel = {
- param($text)
- if ($this.Label -ne $null) {
- $this.Label.Text = $text
- }
- }
- }
- # Method to initialize the form
- function Initialize-Form {
- $formObject.Form = New-Object System.Windows.Forms.Form
- $formObject.Form.Text = 'Progress Window'
- $formObject.Form.Size = New-Object System.Drawing.Size(300,200)
- $formObject.Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
- $formObject.Form.TopMost = $true
- $formObject.Label = New-Object System.Windows.Forms.Label
- $formObject.Label.Text = 'Starting...'
- $formObject.Label.AutoSize = $true
- $formObject.Label.Location = New-Object System.Drawing.Point(10,20)
- $formObject.Form.Controls.Add($formObject.Label)
- $formObject.ProgressBar = New-Object System.Windows.Forms.ProgressBar
- $formObject.ProgressBar.Style = "Continuous"
- $formObject.ProgressBar.Minimum = 0
- $formObject.ProgressBar.Maximum = 100
- $formObject.ProgressBar.Location = New-Object System.Drawing.Point(10,50)
- $formObject.ProgressBar.Size = New-Object System.Drawing.Size(260,23)
- $formObject.Form.Controls.Add($formObject.ProgressBar)
- $formObject.Form.Show()
- }
- # Method to dispose of the form
- function Dispose-Form {
- if ($formObject.Form -ne $null) {
- $formObject.Form.Close()
- $formObject.Form.Dispose()
- }
- }
- # Initialize the form
- Initialize-Form
- # Example usage: Simulate some work and update the UI
- for ($i = 0; $i -le 100; $i += 10) {
- Start-Sleep -Seconds 1
- # Instead of using Invoke, define helper functions for better scope handling
- $updateLabel = { param($text) $formObject.Label.Text = $text }
- $updateProgress = { param($progress) $formObject.ProgressBar.Value = $progress }
- & $updateLabel -text "Processing: $i%"
- & $updateProgress -progress $i
- [System.Windows.Forms.Application]::DoEvents()
- }
- # Cleanup
- Dispose-Form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement