Real-time WPF GUI Updates in PowerShell Script Not Working

Swahela Mulla 90 Reputation points
2023-08-28T07:58:01.6333333+00:00

Hello Everyone,

I'm facing an issue with my PowerShell script where I'm trying to dynamically update a TextBlock/Label in my WPF GUI to reflect different stages of a process. Unfortunately, I'm only seeing the default value "Not Started" and the final value "Completed!" being displayed in my GUI (All process we able to see in PowerShell Console but not in GUI). I've tried a few different approaches including Dispatcher.Invoke, UpdateLabel.Invoke, and even using runspaces, but nothing seems to work as expected.

I've tested the script in my environment and even tried the suggested solutions from online resources, but still haven't achieved the desired real-time updates. Here's a simplified version of my script:

ClS
$Error.Clear()
Add-Type -AssemblyName PresentationFramework,PresentationCore,System.Windows.Forms

if (Get-Variable "myhash" -Scope Global -ErrorAction SilentlyContinue){Remove-Variable -Name "myHash" -ErrorAction SilentlyContinue;$Error.Clear()}Else{$Error.Clear()}
$Global:myHash = [hashtable]::Synchronized(@{})


$myHash.wpfButton1_Click= {


    $myHash.WPFLabel_2.Content = "Running Script..."
    $myHash.myForm.Focus()
    Write-Host "Starting to Sleep - Running Script"
    Sleep -Seconds 5

    $myHash.WPFLabel_2.Content = "Gatharing Data.."
    Write-Host "Starting to Sleep - Gatharing Data.."
    Sleep -Seconds 5

    $myHash.WPFLabel_2.Content = "Processing Data.."
    Write-Host "Starting to Sleep - Processing Data.."
    Sleep -Seconds 5
    
    $myHash.WPFLabel_2.Content = "Completed!"
    Write-Host "Completed."  
}

[xml]$myHash.myXAML = '<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      
        Title="MainWindow" 
        Height="450" Width="450"
        ResizeMode="NoResize"
             
        WindowStartupLocation="CenterScreen"
        ShowInTaskbar ="True"
        Name="MainForm"

        >
        
         <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <!-- This is the main grid on the form. It divides the form into 2 rows. Top row is contains the header and logo. Bottom row contains the message -->
            <Grid.ColumnDefinitions>
			    <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
			    <RowDefinition Height="*" />
                
		    </Grid.RowDefinitions>


            <StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Orientation ="Horizontal" >
                <Label Name="WPFLabel_1" Margin="2,0,0,0" > Run ME :  </Label>
                <Label Name="WPFLabel_2" Margin="2,0,0,0" > Not Started  </Label>
                <Button Name="wpfButton1" Content="Start Script" Margin="2,0,0,0" Height="25" Width="100" VerticalAlignment="Top"/>
            </StackPanel>
        </Grid>
   
</Window>
'

    $myHash.myForm = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $myHash.myXAML))

    $myHash.myXAML.SelectNodes("//*[@Name]") | ForEach-Object {if (($_.Name).length -gt 1){$myHash.Add($_.Name, $myHash.myForm.FindName($_.Name))} }

    $myHash.wpfButton1.add_Click($myHash.wpfButton1_Click)


    $myHash.myForm.ShowDialog()

I've exhausted my troubleshooting efforts and would appreciate any guidance or suggestions from the community. If anyone has successfully achieved real-time GUI updates in a WPF PowerShell script, could you kindly share your approach and any tips that might help me resolve this issue?

Thank you in advance for your assistance!

Best regards,

Swahela

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,709 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Muhammad Binyameen 395 Reputation points
    2023-08-28T13:40:29.5733333+00:00

    I'd be happy to help you with the issue you're facing. Could you provide me with more information about the specific problem you're encountering with real-time WPF GUI updates in your PowerShell script? The more details you can provide, the better I'll be able to assist you.