Page 4 of 4

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

Posted: Thu Nov 27, 2014 1:11 pm
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

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

Posted: Thu Nov 27, 2014 1:46 pm
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.

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

Posted: Thu Nov 27, 2014 3:25 pm
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.