This recent change was kicked off by one of our customers, who wanted to see ‘Write-verbose’ messages when debugging or testing, but not in production. We assumed that the same is probably true for ‘Write-Debug’ messages. It makes perfect sense to suppress these messages once your script or application has moved into production.
If you develop with PrimalScript or PowerShell Studio and run your production scripts simply from a prompt, then this is very simple:
You check for the presence of a SAPIENHost object and if it is not there, then you are not in your developing environment.
If you deploy your scripts with our script packager, then this is not working of course. All PowerShell packaging engines with the exception of the Windows Service engine have a SAPIENHost object.
So we added a function called ‘IsDebugging’ to the object to determine the current state:
To further distinguish the current environment your script is running in, we also added the functions ‘IsTesting’ and ‘IsPackaged’.
‘IsTesting’ returns true when you run your script in PrimalScript or PowerShell Studio with a profiler enabled.
‘IsPackaged’, as the name implies, returns true if the script runs in a packaged executable.
These functions are available with PrimalScript 2021 (7.7.147), PowerShell Studio 2021 (5.8.186), PowerShell for Windows PowerShell 5.1 and PowerShell Core 7.1.