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
Why does this not display the results in the output window
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.
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.
- juneblender
- Posts: 93
- Last visit: Thu Mar 30, 2017 8:54 am
- Alexander Riedel
- Posts: 8479
- Last visit: Thu Mar 28, 2024 9:29 am
- Been upvoted: 37 times
Re: Why does this not display the results in the output wind
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.
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.
SAPIEN Technologies, Inc.
Re: Why does this not display the results in the output wind
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.
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.