Why does this not display the results in the output window

Ask your Windows 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.
User avatar
juneblender
Posts: 93
Joined: Thu Jul 24, 2014 3:26 pm

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

Post by juneblender » Thu Nov 27, 2014 2:11 pm

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: 7096
Joined: Tue May 29, 2007 4:43 pm

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

Post by Alexander Riedel » Thu Nov 27, 2014 2:46 pm

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: 13882
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Nov 27, 2014 4:25 pm

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.

Locked