Capture all Consule Output from Form and Out-File

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 7 years and 1 month 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
cweaver
Posts: 8
Last visit: Wed May 09, 2018 7:53 am

Capture all Consule Output from Form and Out-File

Post by cweaver »

I'm trying to capture all of the console output from a packaged psf, which is now an .exe. Using try catch blocks I am able to capture errors.

What I am not able to do is capture what the script outputs to the console. This is particularly critical when using it to create Exchange and Skype accounts. I want to see all of that information b/c it is evidence of admin actions.

This is particularly painful for Exchange b/c admin actions are not captured by the event log. In this manner, someone doesn't have to query Exchange's admin log.

For regular .ps1 files, I would use the start-transcript... stop-transcript method; however, I have read that this is not usable by Sapien PowerShell studio. When I tested it just using a regular .ps1 file, it worked phenomenally. I started a transcript and robocopy moved all of the data to a text file when I put 2>&1 | Write-Output after each executed command (with an output)
User avatar
cweaver
Posts: 8
Last visit: Wed May 09, 2018 7:53 am

ABOUT POWERSHELL Windows PowerShell (POSH) is a command-line shell and associated scripting language created by M

Post by cweaver »

I'm trying to capture all of the console output from a packaged psf, which is now an .exe. Using try catch blocks I am able to capture errors.

What I am not able to do is capture what the script outputs to the console. This is particularly critical when using it to create Exchange and Skype accounts. I want to see all of that information b/c it is evidence of admin actions.

This is particularly painful for Exchange b/c admin actions are not captured by the event log. In this manner, someone doesn't have to query Exchange's admin log.

For regular .ps1 files, I would use the start-transcript... stop-transcript method; however, I have read that this is not usable by Sapien PowerShell studio. When I tested it just using a regular .ps1 file, it worked phenomenally. I started a transcript and robocopy moved all of the data to a text file when I put 2>&1 | Write-Output after each executed command (with an output)
User avatar
cweaver
Posts: 8
Last visit: Wed May 09, 2018 7:53 am

Capture all Consule Output from Form and Out-File

Post by cweaver »

I'm trying to capture all of the console output from a packaged psf, which is now an .exe. Using try catch blocks I am able to capture errors.

What I am not able to do is capture what the script outputs to the console. This is particularly critical when using it to create Exchange and Skype accounts. I want to see all of that information b/c it is evidence of admin actions.

This is particularly painful for Exchange b/c admin actions are not captured by the event log. In this manner, someone doesn't have to query Exchange's admin log.

For regular .ps1 files, I would use the start-transcript... stop-transcript method; however, I have read that this is not usable by Sapien PowerShell studio. When I tested it just using a regular .ps1 file, it worked phenomenally. I started a transcript and robocopy moved all of the data to a text file when I put 2>&1 | Write-Output after each executed command (with an output)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture all Consule Output from Form and Out-File

Post by jvierra »

What you are asking is not clear. Why do you think that Start-Transcript won't work?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture all Consule Output from Form and Out-File

Post by jvierra »

I merged your duplicate posts. Please try not to post the same topic in multiple forums.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture all Consule Output from Form and Out-File

Post by jvierra »

You cannot output to the console from a form. Output to a console is suppressed in Windows Forms. This has nothing to do with PSS or PowerShell it is just how Windows forms work.

You can use Write-Host to the console but that cannot be captured; it can only be displayed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture all Consule Output from Form and Out-File

Post by jvierra »

I should also note that you can return objects to the console but not from an EXE.
User avatar
cweaver
Posts: 8
Last visit: Wed May 09, 2018 7:53 am

Re: Capture all Consule Output from Form and Out-File

Post by cweaver »

I will include some code to clarify what I am trying to do and capture. I highlighted the lines in particular that I am trying to capture output that would go to the console if it were not suppressed.

Try
{
if ($ExchangeServer -eq "STLEXCserver")
{
Enable-Mailbox -identity "ourdomain\$sam" -Alias $alias 2>&1 |Write-Output | Out-file -FilePath $sLogFile -Append
}
else
{
Enable-Mailbox -identity "ourdomain\$sam" -Alias $alias -Database $mdb 2>&1 |Write-Output | Out-file -FilePath $sLogFile -Append
}
Set-Mailbox $alias -EmailAddresses @{ Add = "$sam@ourdomain.com" } 2>&1 |Write-Output | Out-file -FilePath $sLogFile -Append
Log-Write -LogPath $sLogFile -LineValue 'Success: able to create mailbox.'
[System.Windows.Forms.MessageBox]::Show('Success: able to create mailbox.')
}
Catch
{
[System.Windows.Forms.MessageBox]::Show('Error: Unable to create mailbox.')
Log-Write -LogPath $sLogFile -LineValue 'Error: Unable to create mailbox.'
Log-Write -LogPath $sLogFile -LineValue "$Error"
}

My organization is still using Exchange 2010. When creating a user, there is output that would get pushed to the console when using it as a ps1. However, as a Winform that output is suppressed. Any recommendation on how I can capture that output?
User avatar
cweaver
Posts: 8
Last visit: Wed May 09, 2018 7:53 am

Re: Capture all Consule Output from Form and Out-File

Post by cweaver »

After updating my tool to Powershell Studio 2017 and compiling my code, I believe that I may have figured it out. I'll keep you posted.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Capture all Consule Output from Form and Out-File

Post by jvierra »

Do not do this:
Enable-Mailbox -identity "ourdomain\$sam" -Alias $alias 2>&1 |Write-Output | Out-file -FilePath $sLogFile -Append

Do this:
Enable-Mailbox -identity "ourdomain\$sam" -Alias $alias 2>&1 | Out-file -FilePath $sLogFile -Append

The Write-OutPut is not needed and may create issues.
This topic is 7 years and 1 month 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