0

Hi I have a ps script that displays a GUI using WPF with a [System.Windows.Controls.TextBox]. On the GUI I will call some other ps scripts by clicking on various buttons and so I would like to redirect their outputs to the TextBox on the fly.

There are two things I could not get it to work:

First I tried this with cmdlets to update the Text property of the TextBox, which worked, but only after the cmdlet finished. So I need a way to update the Text on the fly.

Next is I tried the same with calling other ps scripts, unlike the cmdlets, I could not capture their outputs, even after they finished. I am guessing because these scripts are using Write-Host instead of Write-Output? Otherwise, I also need a way to capture the outputs of ps scripts to update the Text on the fly.

I am also thinking it would be much easier if there is a PowerShell cmd control that I can add to the GUI, so I don't have to do the above to achieve what I want. This control can execute the ps scripts and print the outputs there, better yet, read the user interactive input. Will pick this as the answer if this is possible.

1 Answer 1

1

Without sample code, this is a little hard to answer.

Usually textblocks are for displaying text and textboxes are for inputting text, I'd probably not use the same box for both.

If you want to update the textblock or box on the fly, wpf has a timer where you can watch for variables and put them in the UI, this is particularly useful if you have your UI and logic in different threads. So you can get the script you have kicked off to update a varible and the timer watches the variable for it to change and will up date the UI.

Add-Type -AssemblyName windowsbase

$updateBlock = {

  $wpftextbox.text = $yourVaribletoWatchGoesHere

}

    $timer = new-object System.Windows.Threading.DispatcherTimer
    # Which will fire 100 times every second        
    $timer.Interval = [TimeSpan]"0:0:0.01"
    # And will invoke the $updateBlock            
    $timer.Add_Tick($updateBlock)
    # Now start the timer running            
    $timer.Start()
    if ($timer.IsEnabled)
    {
        Write-Output 'UI timer started'
    }
    else
    {
        $clock.Close()
        Write-Output 'UI update timer did not start'
    }

You are correct, if they are using write-host their output will be hard to capture. You should edit the scripts so they use write-output, not write-host. This is a good example of why that is best practice.

I'm not sure what you mean by powershell cmd control, but as far as I know there is no shortcuts here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! But I dont have a variable to watch, its the outputs of another script. Do you have an example that $wpftextbox.text = script.ps #output of another script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.