write-verbose in deployed form?

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
joeypiccola
Posts: 9
Last visit: Mon Jun 18, 2018 7:44 am

write-verbose in deployed form?

Post by joeypiccola »

Hello,

I have built a GUI in PowerShell Studio as a Project (e.g. multi file \ form). I have deployed it via the Export to File option (which seems to have merged and joined all my files into a single ps1 file). I've noticed in the generated \ deployed .ps1 file there is no [CmdletBinding()] at the top. As such, my write-verbose commands are not working \ visible in the console used to launch the .ps1 file. Other than manually editing the generated \ deployed .ps1 file and adding the snippet below, how would I go about getting CmdletBinding enabled on my the generated \ deployed .ps1 file?

Thanks!
  1. [CmdletBinding()]
  2. Param()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: write-verbose in deployed form?

Post by jvierra »

Well - you really can't because a project is wrapped in a "Main" (in startup.pss) and it has not binding. You can set the global verbose preferences in your profile to see if it will propagate.

Verbose acts in the local scope and all child scopes. Global scope sets the default for everything.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: write-verbose in deployed form?

Post by jvierra »

This would probably also work:

In Startup.pss at the top:
  1. #Define a Param block to use custom parameters in the project
  2. [CmdletBinding()]
  3. Param ($CustomParameter)
  4. $global:VerbosePreference = 'Continue'
  5. function Main {
  6. <#
User avatar
joeypiccola
Posts: 9
Last visit: Mon Jun 18, 2018 7:44 am

Re: write-verbose in deployed form?

Post by joeypiccola »

Excellent! Thank you very much. That also helps me understand the Startup.pss! Thanks @jvierra.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: write-verbose in deployed form?

Post by jvierra »

Great. Yes. Startup.pss is the root of all roots for a project script.

You should probably save and replace $VerbosePreference in the startupm or ... this ...
  1. #Define a Param block to use custom parameters in the project
  2. #[CmdletBinding()]
  3. #Param ($CustomParameter)
  4.  
  5. function Main {
  6.     Param ([String]$Commandline)
  7.     $savedVerbosePreference = $global:VerbosePreference
  8.     $global:VerbosePreference = 'Continue'
  9.    
  10.     if((Show-MainForm_psf) -eq 'OK'){
  11.        
  12.     }
  13.     # put things back where they were
  14.     $global:VerbosePreference = $savedVerbosePreference
  15.     $global:ExitCode = 0 #Set the exit code for the Packager
  16. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: write-verbose in deployed form?

Post by jvierra »

I suspected that this was the case so I tested it; THe only thing needed is on line in startup.pss

Just do this:
  1. #Define a Param block to use custom parameters in the project
  2. [CmdletBinding()]
  3. Param ($CustomParameter)
Once that is added you can call the file with "-Verbose" or you can run the EXE with "-verbose" and the verbose is set globally.

A "Param" statement is required.
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