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.
This topic is 11 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
soccsupport
Posts: 17
Last visit: Mon Dec 23, 2019 11:44 am

Powershell cmd output to Textbox

Post by soccsupport »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell cmd output to Textbox

Post by davidc »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell cmd output to Textbox

Post by davidc »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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
Last visit: Mon Dec 23, 2019 11:44 am

Re: Powershell cmd output to Textbox

Post by soccsupport »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell cmd output to Textbox

Post by davidc »

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.
This topic is 11 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked