Why does this not display the results in the output window

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
juneblender
Posts: 93
Last visit: Thu Mar 30, 2017 8:54 am

Re: Why does this not display the results in the output wind

Post by juneblender »

Out-String sends a string to the host process, so the crucial question here is how PowerShell Studio (or the form) responds when it gets that string, if at all.

Right now, the code selects the greatest date value, generates the string, and then resets the focus on the $results. Seems reasonable.

As an aside, Sort-Object sorts [DateTime] objects by date value by default, so you can use:

$newestLogon=$logons| Sort-Object

instead of:

$newestLogon=($logons| Measure-Object -Max).Maximum
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Why does this not display the results in the output wind

Post by Alexander Riedel »

I just read through this entire thread. I may have missed a thing or two in here, so my apologies if I repeat something or missed a point.

The discussion mostly seems to revolve around the use of 'out-string'.
Out-string sends a textual representation of an object to stdout in a powershell console host.
A Windows application, as opposed to a console application, simply has no stdout. So the output basically goes to lala land.

If you run the GUI script in a PowerShell console then it is different, you have a console that runs a set of dialogs, so the underlying application is a console application.

To make a long story short, for GUI apps out-string and the like simply DO NOT WORK as they do in a console, because stdout, stdin and stderr are not there.

If the object itself, like DateTime, has an overridden .ToString() method, you can use a simple assignment, i.e.
$Result.Text = Now.ToString()

For other, more complex objects, you may need to use a stringbuilder object and its formatting abilities and the individual object members to construct a meaningful textual representation of an object.

In some cases an assignment operation will also help:

$out = Get-Process
generates a system.array of process objects. You cannot assign that to a text field obviously.

However,
$out = Get-Process | Out-String
pipes the output through PowerShell's default formatter and assigns the textual output to $out. So in this case $out is a string and can be used to be assigned to a text field on a form.
$out.GetType().Name should be "String" if you want to create a test before assigning values to text fields.

I hope this clears this up a little bit better.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Why does this not display the results in the output wind

Post by jvierra »

Short answer: YOu must assign to teh control directly.

Example for a textbox:

$textbox1.Text = ($logons| Measure-Object -Max).Maximum

Or,as Alex suggests:

$textbox1.Text = $logons | sort-object -desc | select -first 1


It works but it is cumbersome.
This topic is 9 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked