Avatar of Kelly Garcia
Kelly Garcia
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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.
PowershellScripting Languages

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
Chris Dent

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 Garcia

ASKER
I've just tested the $textbox.text - it don't give me any results.
Chris Dent

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).
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Kelly Garcia

ASKER
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
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

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 Garcia

ASKER
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

Assignment operator.
$OutputBox.Text = $results

Open in new window

Kelly Garcia

ASKER
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 Dent

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Kelly Garcia

ASKER
fantastic!
Kelly Garcia

ASKER
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 Dent

Potentially yes. If you find this technique doesn't work you'll have to use ErrorVariable instead which likely to get a bit frustrating.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kelly Garcia

ASKER
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 Dent

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 Garcia

ASKER
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??
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Chris Dent

`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 Garcia

ASKER
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 Garcia

ASKER
Fantastic!!! Thank you!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kelly Garcia

ASKER
*>&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 Dent

&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.