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.
Powershell cmd output to Textbox
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.
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.
Re: Powershell cmd output to Textbox
You will always need to use Out-String cmdlet in order to convert the objects into text.
David
PowerShell Code
Double-click the code block to select all.$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Server1/PowerShell -Credential $DoorKey | Out-StringBut 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.
SAPIEN Technologies, Inc.
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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".
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".
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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() }
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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.
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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!
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!
Re: Powershell cmd output to Textbox
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.
SAPIEN Technologies, Inc.
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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.
- soccsupport
- Posts: 17
- Last visit: Mon Dec 23, 2019 11:44 am
Re: Powershell cmd output to Textbox
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 ).
I get ERROR: Missing statement block after If ( condition ).
Re: Powershell cmd output to Textbox
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
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.