Powershell cmd output to Textbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Powershell cmd output to Textbox

Post by soccsupport » Wed Apr 03, 2013 10:48 am

I am doing a System.Text.StringBuilder object to capute PowerShell command outputs for my richtextbox. Two of my commands I can't seem to get proper output or output at all.

Example Command in script.

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server1/PowerShell -Credential $DoorKey

$stringBuilder.AppendLine($Session)

The output for the $Session on shows "$Session1" in the text box. It should show two lines of output.

Another command is $Grr=Search Mailbox blablabla... followed by |Out-String. The output for $Grr produces nothing in my textbox.

If I do $Test1=Get-process|Out-String and do the .AppendLine($Test1) I get all that output in my textbox just fine.

Any ideas? Thanks.

User avatar
davidc
Posts: 5805
Joined: Thu Aug 18, 2011 4:56 am

Re: Powershell cmd output to Textbox

Post by davidc » Wed Apr 03, 2013 10:53 am

You will always need to use Out-String cmdlet in order to convert the objects into text.
PowerShell Code
Double-click the code block to select all.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server1/PowerShell -Credential $DoorKey | Out-String
But it is hard to tell what is happening without the complete code. I recommend uploading a sample form or script.

David
David
SAPIEN Technologies, Inc.

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Wed Apr 03, 2013 11:07 am

Good point about putting the |Out-String at the end of the line of code. Which I forgot I had done that and it works.

BUT... In doing so that kills the $Session variable for the next line of code which is Import-PPSession $Session.

Now if I do the Import-PSSession $Session|Out-String then I all I get in the textbox is "[PSSession]Session1".

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Wed Apr 03, 2013 11:26 am

Here is the sectoin of code that I am trying to get good output to the textbox. Thank you.

PowerShell Code
Double-click the code block to select all.
$exchangeMailboxToolStripMenuItem1_Click={
	#TODO: Place custom script here
	
	$stringBuilder = New-Object System.Text.StringBuilder
	$stringBuilder.AppendLine("(Exchange Mailbox Search Operation)")
	$stringBuilder.AppendLine("")
		
	If ($RemotePS.Checked)
		{#Credentials prompt used to establish Remote Session.
		$DoorKey = Get-Credential
		#This will establish a remote Powershell session with a server.
		$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server1/PowerShell -Credential $DoorKey #"Putting |Out-String here breaks the Import-Session command!!!"
		$stringBuilder.AppendLine($Session)
		#This will import Exchange Powershell modules from remote server into the remote session.
		Import-PSSession $Session|Out-String #"Does Work right-Getting one line of very little text!!!"
		$stringBuilder.AppendLine($Session)
		}
	
	If ($checkedlistbox1.CheckedItems.Contains("Single")) #THIS IS WORKING!!!
	
		{$Grr=Search-Mailbox -Identity $MailboxName.Text -SearchQuery attachment:$SearchString.Text -TargetMailbox User1-TargetFolder Inbox -LogOnly -LogLevel Full |Out-String
		$stringBuilder.Append($Grr)
		
				
		#This will close the session to the Exchange Server.
		Remove-PSSession $Session
		}
		
		
		$stringBuilder.AppendLine("")
		$stringBuilder.AppendLine("End of Line!!!")
		
		
		$richtextbox1.Text = $stringBuilder.ToString()
	
	
}

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Wed Apr 03, 2013 12:09 pm

OK in having a duh moment I fixed one of my issues. For the $Session line I just re-captured that to another variable and then piped that out to text. Works fine. Like so below:
PowerShell Code
Double-click the code block to select all.
$TestSession = $Session|Out-String
$stringBuilder.AppendLine($TestSession)
Now when it comes to my other line of code for the Search-Mailbox, doing the same thing as above yielded no results.

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Wed Apr 03, 2013 1:01 pm

UPDATE - So it appears that the |Out-String is working on my Search-Mailbox line. What is not working is when it throws an error, like when you tell it to search a non existing mailbox or such.

So if the command runs like it should then you do get all the output via the stringbuilder.appendline and shows up in teh textbox.

Just need to figure out how to capture the error when it throws it. Hats off to all real/full time programers!

User avatar
davidc
Posts: 5805
Joined: Thu Aug 18, 2011 4:56 am

Re: Powershell cmd output to Textbox

Post by davidc » Wed Apr 03, 2013 1:26 pm

To display the error message use a try catch block:

PowerShell Code
Double-click the code block to select all.
try{
	$Grr = Search-Mailbox -Identity $MailboxName.Text -SearchQuery attachment:$SearchString.Text -TargetMailbox 
}
catch
{
#get the error message
	 $stringBuilder.Append($_.Exception.Message) 
}
David
David
SAPIEN Technologies, Inc.

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Thu Apr 04, 2013 2:11 pm

So far I am unable to get the Try / Catch to work. No errors wit the code; just not getting the warning text to the Textbox.

User avatar
soccsupport
Posts: 17
Joined: Sun Mar 03, 2013 12:44 pm

Re: Powershell cmd output to Textbox

Post by soccsupport » Thu Apr 04, 2013 2:14 pm

I have an IF statment in there. Seems puting the Try { after the If send the code for a loop.

I get ERROR: Missing statement block after If ( condition ).

User avatar
davidc
Posts: 5805
Joined: Thu Aug 18, 2011 4:56 am

Re: Powershell cmd output to Textbox

Post by davidc » Fri Apr 05, 2013 10:33 am

It’s quite possible the cmdlet doesn't throw an exception and only displays the error message. Unfortunately not all cmdlets are created equal. You will need to ask the module creator.

David
David
SAPIEN Technologies, Inc.

Locked