Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag 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.
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of Kelly Garcia

ASKER

I've just tested the $textbox.text - it don't give me any results.
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).
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
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Assignment operator.
$OutputBox.Text = $results

Open in new window

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
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.
fantastic!
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 =??
Potentially yes. If you find this technique doesn't work you'll have to use ErrorVariable instead which likely to get a bit frustrating.
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
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

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??
`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.
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

Fantastic!!! Thank you!
*>&1

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

what is &1?

Thank you soo much Chris, you have been an absolute star!!
&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.