We help IT Professionals succeed at work.

powershell automatic variable for results

194 Views
Last Modified: 2017-04-18
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

Chris DentPowerShell Developer
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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).
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
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Chris DentPowerShell Developer
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions