SAPIEN Information Center
ImplementingAssembly: Find PowerShell Classes in Modules
Beginning in PowerShell 5.0, you can create your own .NET classes and include them in scripts and modules. I've been having fun working on and teaching A Class of Wine. If you're a PowerShell scripter, but not a developer, creating your own classes really helps you to understand the concepts in .NET classes.
But, for the past year, many of us been complaining about how hard it is to detect PowerShell classes that are defined in modules. We were missing the ImplementingAssembly property! Thanks to PowerShell MVP Bartek Bielawski (@bielawb) for finding it.
The MessageBox Control
MessageBox Control [System.Windows.Forms.MessageBox]
Displays a modal window with a message for the user and at least one button. The MessageBox closes only when the user clicks a MessageBox button. A MessageBox can contain text, buttons, and symbols that show information to the user and require their response.
MSDN Page: System.Windows.Forms.MessageBox
How to find an installation directory
One of the important best-practice rules in scripting (and programming) is to avoid using constant values for things that can vary. Using constants or "hard-coding" variable values is prone to error and requires continual maintenance. So, wherever you can, use variables and wildcards for variable values and use constants for the few things in life that don't change, e.g. New-Variable pi -Value [Math]::Pi -Option Constant
This issue arose in a SAPIEN forum post (thanks for posting!) in which someone hard-coded the path to program that a PowerShell Studio installer file (.msi) installs. That doesn't seem like a particularly risky thing, but paths change and, in this case, we changed a path to conform to an updated Windows standard. So, instead of hard-coding a file system path, the best way to find an installation directory is to use the same technique that Windows uses. It doesn't change often and it is very unlikely to change without a version change that you can check in your script.
If you need to use a file path or URL in a script, here are a few guidelines:
- Get the path at run time from a reliable location
- Place the path variable at the top of a script (in script scope), so it's very obvious to anyone who is maintaining the script.
Collapse This! Using Regions to Organize Your Scripts
Whether you're writing a script or reading one, you want to keep your environment as simple and uncluttered as possible. You want to display only what you need to see and eliminate everything else. The Expand and Collapse features in PowerShell Studio let you do just that -- with simple hotkeys and configurable settings.
In this blog, I show how to configure and use the Expand and Collapse features of PowerShell Studio and how to use regions to organize the code in your scripts.