Richtextboxlogging for PS Studio, script for normal script use

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
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Richtextboxlogging for PS Studio, script for normal script use

Post by stevens »

Product, version and build:
(*** Please do not write "latest" as a version, specify a version number ***)
32 or 64 bit version of product: 64
Operating system: W2K12R2
32 or 64 bit OS: 64
PowerShell Version:

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
Hi,

I'd like to use a powershell script within PS Studio as well as outside. I'd also like to write the output in the normal script to write-host (or verbose), in powershell studio to $richtextboxlogging.text).
Therefore I use $LoggingPSStudio. It works for write-host but not for $richtextboxlogging.
Below the code, please advise howto correct.

  1. $output = if ($LoggingPSStudio)
  2.     {
  3.       '$richtextboxLogging.Text ='
  4.     }
  5.     else  
  6.     {
  7.       'Write-Host'
  8.     }
  9.      
  10.     #region executing computer checks  
  11.     if ($ComputerPerformance -or $All)
  12.     {
  13.       & $output 'Getting Uptime'
  14.       $Uptime = $(Get-Uptime -ComputerName $ComputerName)
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by DevinL »

If I'm not mistaken, you want the output to go to $richtextboxlogging if $LoggingPSStudio is true, correct? If this is the case, I'd write a small function like so and just call it when you need to send output:
  1. function Write-Message {
  2.   param
  3.   (
  4.     [Parameter(Mandatory = $true,
  5.            Position = 0)]
  6.     [String]$Message,
  7.     [Parameter(Position = 1)]
  8.     [boolean]$LoggingPSStudio
  9.   )
  10.    
  11.   if ($LoggingPSStudio) {
  12.     $richtextbox1.Text = $Message
  13.   } else {     
  14.     Write-Host $Message
  15.   }
  16.    
  17.    
  18.   #TODO: Place script here
  19. }
  20.  
  21. if ($ComputerPerformance -or $All) {
  22.   Write-Message -Message 'Testing, testing' -LoggingPSStudio $false
  23.   $Uptime = $(Get-Uptime -ComputerName $ComputerName)
  24. }
DevinL
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by stevens »

Thanks but I don't want to change every command in my script to write-mesasge -message 'mymessag" -loggingpsstudio $false or $true
I'd like to have my script get-comptuterreport to be with options -writehost or -loggingpsstudio.

The comments in the script then just would be write-message 'Mymessage', I'd then only have to do a find replace of write-host -text by write-message -message
Sounds more flexible to me. Could you advise on that?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by jvierra »

In you form place a simple function that is a wrapper for write-host at the script scope.
  1. function Write-Host{
  2.     Param($message)
  3.         $richtextbox1.AppendText($message)
  4. }
When not in the form Write-Host or Write-Verbose will behave as usual. In a form the wrapper function will intercept the function call and output as you like.

Attached is a simple demo of how to do this.
Attachments
Test-AltWrite.psf
(16.78 KiB) Downloaded 123 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by jvierra »

We can actually write all parameters to both locations at the same time and account for extra parameters not used in the GUI.
  1. function Write-Host {
  2.     param (
  3.         [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromRemainingArguments = $true)]
  4.         [System.Object]${Object},
  5.         [switch]${NoNewline},
  6.         [System.Object]${Separator},
  7.         [System.ConsoleColor]$ForegroundColor,
  8.         [System.ConsoleColor]${BackgroundColor}
  9.     )
  10.     $textbox1.Lines += $Object
  11.     Microsoft.PowerShell.Utility\Write-Host @PsBoundParameters
  12. }
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by stevens »

Thanks but both result in an error when used outside PS Studio (PS ISE):

You cannot call a method on a null-valued expression.
At c:\MyScripts\Powershell\HealthCheck\HealthCheck.ps1 char:9
+ $richtextbox1.AppendText($message)


Write-Host : The property 'Lines' cannot be found on this object. Verify that the property exists and can be set.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by jvierra »

Did you run the example I posted above?
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by stevens »

I did, but honestly, I don't get it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextboxlogging for PS Studio, script for normal script use

Post by jvierra »

stevens wrote:I did, but honestly, I don't get it.
What is it that you don't get?
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