powershell automatic variable for results

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.
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
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

0
Kelly GarciaSenior Systems AdministratorAuthor Commented:
I've just tested the $textbox.text - it don't give me any results.
0
Chris DentPowerShell DeveloperCommented:
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).
0
Webinar: Miercom Evaluates Wi-Fi Security

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom how WatchGuard's Wi-Fi security stacks up against the competition in our upcoming webinar!

Kelly GarciaSenior Systems AdministratorAuthor 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
0
Chris DentPowerShell DeveloperCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:
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

0
Kelly GarciaSenior Systems AdministratorAuthor 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

0
Chris DentPowerShell DeveloperCommented:
Assignment operator.
$OutputBox.Text = $results

Open in new window

0
Kelly GarciaSenior Systems AdministratorAuthor 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
0
Chris DentPowerShell DeveloperCommented:
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.
1
Kelly GarciaSenior Systems AdministratorAuthor Commented:
fantastic!
0
Kelly GarciaSenior Systems AdministratorAuthor 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 =??
0
Chris DentPowerShell DeveloperCommented:
Potentially yes. If you find this technique doesn't work you'll have to use ErrorVariable instead which likely to get a bit frustrating.
0
Kelly GarciaSenior Systems AdministratorAuthor 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
0
Chris DentPowerShell DeveloperCommented:
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

0
Kelly GarciaSenior Systems AdministratorAuthor 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??
0
Chris DentPowerShell DeveloperCommented:
`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.
0
Kelly GarciaSenior Systems AdministratorAuthor 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

0
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Fantastic!!! Thank you!
0
Kelly GarciaSenior Systems AdministratorAuthor 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!!
0
Chris DentPowerShell DeveloperCommented:
&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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.