powershell automatic variable for results

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
HI All,

Is there an automatic variable that tells me the results of the last run command on Powershell? for example I run this command to migrate mailboxes:

New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf

I want the results that are outputted on the powershell screen to be stored in a variable that I can then use to output in a textbox I created using wpf.

thank you in advance for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:
No, no automatic variables for output. It's one of the requested features for PS 6, although I haven't been following the status of the RFC.

The "$?" variable does hold a boolean that is supposed to show the result of the last command. However, this is a bit of a poor form of error control and it has little use beyond that.

You'll have to assign:
$textbox.Text = New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
I've just tested the $textbox.text - it don't give me any results.
Chris DentPowerShell Developer
Top Expert 2010

Commented:
It assumes you had a control named that. But you're running with WhatIf, does the command return anything at all when you use WhatIf?

The simplified version is:
$variable = New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf

Open in new window

Given that you're indexing into an array you might want to ensure the variable is positioned to capture output from the whole array (or that you use a collection you can add to). For example:
# Before loop
$results = New-Object System.Collections.Generic.List[Object]

# Inside loop
$results.Add((New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf))

# After loop
$control.Text = $results

Open in new window

And bear in mind that if you do get a result it'll almost certainly be some kind of object. A object with lots of properties won't necessarily display well in a text box (contingent on whatever the ToString method attached to each object does).
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Kelly GarciaSenior Systems Administrator

Author

Commented:
yes that whatif returns an error, that mailbox is already in the targetdatabase, whatever the output I wanted this to be displayed within a textbox
PowerShell Developer
Top Expert 2010
Commented:
An error isn't standard outut, it's error output and cannot be assigned.

The streams are:

1 = StdOut
2 = Error
3 = Warning
4 = Verbose
5 = Debug
6 = Information

It is possible to redirect streams to one another. For example you can redirect every single stream to StdOut.
New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf *>&1

Open in new window

If you only wanted Error:
New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf 2>&1

Open in new window

You can also redirect to a file, this would allow you to keep the streams separate.
New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf 2> error.txt 3> warning.txt

Open in new window

You cannot redirect to a variable; only assign after directing to StdOut.

There are still limitations, not everything is written to a named output stream. Certain output types (such as Write-Host prior to PowerShell 5) cannot be redirected.
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Actually, this technique is probably more appropriate.

Errors are also written automatically to $Error. However, I don't recommend using that one.

You can also add variables for the error stream:
New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf -ErrorVariable MoveError
if ($MoveError) {
     $TextBox.Text = $MoveError.Exception.Message
}

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
I get this error:

At line:1 char:17
+ $OutputBox.Text $results
+                 ~~~~~~~~
Unexpected token '$results' in expression or statement.
At :line:1 char:17
+ $OutputBox.Text $ <<<< results

Open in new window

Chris DentPowerShell Developer
Top Expert 2010

Commented:
Assignment operator.
$OutputBox.Text = $results

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf *>&1

I've just tried this command, what do I do next, is the results assigned to variable??

thank you very much for this information
Chris DentPowerShell Developer
Top Expert 2010

Commented:
All output has been redirected to StdOut, if you assign it to a variable everything (with a few exceptions) will be stored in the variable.
Kelly GarciaSenior Systems Administrator

Author

Commented:
fantastic!
Kelly GarciaSenior Systems Administrator

Author

Commented:
This is the code:


if ($a)
				{
				
				for ($d=0; $d -lt $a.length; $d++)
				{
					if (($AVS - $a[$d]) -gt 200)
					{
		 			 $test = $AVS - $a[$d]
					 [System.Windows.MessageBox]::Show($test)
					 
					try {
					
						$OutputBox.Text = New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf *>&1	
							
						}
					catch {
			
							$OutputBox.Text = “Caught an exception:” 
							$OutputBox.Text = “Exception Type: $($_.Exception.GetType().FullName)” 
							$OutputBox.Text = “Exception Message: $($_.Exception.Message)”
										
										}
						
					}
				}#for...
				}#if ($a)

Open in new window


should I use $OutputBox.Text += New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf *>&1 instead of just =??
Chris DentPowerShell Developer
Top Expert 2010

Commented:
Potentially yes. If you find this technique doesn't work you'll have to use ErrorVariable instead which likely to get a bit frustrating.
Kelly GarciaSenior Systems Administrator

Author

Commented:
this works fantastic, only issue I am having now is that the text is in same line, it don't enter the new results in the next line
Chris DentPowerShell Developer
Top Expert 2010

Commented:
You'll have to manually add a line break. Appending to a text box is a bit of a messy method. This should do it.
$OutputBox.Text += "`r`n"

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
this is fantastic however the first line is missing.

by the way the meaning of `n is new line, what is the meaning of `r??
Chris DentPowerShell Developer
Top Expert 2010

Commented:
`n = Line feed
`r = Carriage return

Type writers...

In Linux, Unix, and (I think) Mac the end of line character is line feed (`n). In Windows it's traditionally carriage return followed by line feed (`r`n). Utilities like notepad don't understand `n alone although some other parts of Windows (including WPF controls by large) do.
Kelly GarciaSenior Systems Administrator

Author

Commented:
the first result is missing from the text using when I try this:

$OutputBox.Text += New-MoveRequest -Identity $m[$d] -TargetDatabase $TargetDatabase -WhatIf *>&1
$OutputBox.Text + "`r`n"

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
Fantastic!!! Thank you!
Kelly GarciaSenior Systems Administrator

Author

Commented:
*>&1

*> - Redirect all streams (output, error, warning, verbose, and debug)

what is &1?

Thank you soo much Chris, you have been an absolute star!!
Chris DentPowerShell Developer
Top Expert 2010

Commented:
&1 is the handle for StdOut. It's required to indicate the thing you're redirecting to is another stream. If you had "1" alone it would make a file named "1" and write there intsead.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial