Posted in Module, Powershell

Checking your installed modules versions

Okay in a bit of a rush and should have posted this one some time ago. In a rush because I  was led along the lines ( thanks Bret Miller ) of using a GitHub Gist to better display (and share) code that I include in posts…

Yes this is very version 1. It was simply a utility I started writing for my own use. Then I found some folks who were running old versions of certain PSGallery installed modules like msonline, ImportExcel or the plethora of Azure Modules. Admittedly I watch Twitter and RSS feeds all day for PowerShell so I was rarely surprised by the results.

The code is a PowerShell function so it can be easily deployed. I recommend placing it in your profile (old school I guess but I still prefer profile.ps1) so you can call it in any PowerShell session.

Recently, well after I  slapped a similar function together for auditing my chocolatey installed packages, I started to look at the next version. After making notes and  beyond  bad code I saw a few things so I am starting a new project to improve it. One quick upgrade will be making the display smart to show the primary module (for example VMware.PowerCLI and not all the sub modules that are part of PowerCLI) only. So now I am looking at creating the project (and learn how to use github projects). My next version will not display to the console with any great expertise like @jaykull but here is a rough version:


Posted in Editor, Powershell, vscode

Snippets – A Lesson Learned

Not the  first and probably won’t be the last but since I was talking about VSCode snippets. I started working on one for creating a [Parameter] statement to make building a Param() section easier. Was cool to provide a dropdown box for a variable (so lesson learned but not the subject of this) and now it is time to test it.

First debug, for that list of choices… at first I had |True,False| but I found using |$True,$False| just looked better, personal choice. Testing continues. Part of the snippet includes [VariableType]$variablename. A possible variable type is “PSObject” but looking at the snippet this should work right?

<div>        // General Parameter Definition</div>
<div>        "Parameter Statement": {</div>
<div>            "prefix": "paramStatement",</div>
<div>            "body": [</div>
<div>                "[parameter(Mandatory=${1|$True,$False|}, ValueFromPipeline=${2|$True,$False|})]\r",</div>
<div>                "\t\tHelpMessage = \"${3:HelpMessage}\")]\r",</div>
<div>                "[${4:VariableType}]$${5:VariableName}"</div>
<div>            ],</div>
<div>            "description": "Scaffold for Parameter creation"</div>
<div>        }</div>

I would type PSObect and the snippet menu would come up as I type pso… Ugggh. Then it dawns on me I had created a snippet months ago for creating Custom PS Objects and the prefix was … psobj. Lesson Learned, even Snippets have reserved words 🙂 !

Posted in Powershell

Sharing – Just my two cents

In my RSS Feed I saw an article , from an author I respect a great deal. This is  just the latest  that just puts me in a quandary. A few months I wrote a script for this function. I struggled with it a few more days fighting with the two versions (Stable and Insiders) to include instances where both are installed. Then I tried to take it one step higher… if snippet is updated locally copy that to github.

Lots of chatter about sharing came out of the PowerShell Summit. Please take no offense but … Most of this comes from the fellow early adopters who could be brutal during “The Scripting Games” and other public forums (see all of the conversation about Write-Host and puppies or kittens whatever it was  when it started). Sure I learned reading the judge’s critiques but they didn’t come off as recommendations or teaching moments. Another person I respected greatly came out with ScriptCop and my insecurity but it was always a kick in the face. Sure PSScriptAnalyzer is basically the same but over the years I get the intent and use it as a guidance.

My own petty insecurity but I don’t share my work publicly, to include blogging. I know they work but might not be perfect. I also have to admit I grind my teeth when I see some posts hit my my RSS Feed. No splatting, backticks, etc. On the same hand I have fought to lead PowerShell training in the places I worked, created  a Slack group for PowerShell but localized to the area I live in. I do love the community and can respect the push for sharing but I think there is talent out there stage shy or maybe even gun shy.

Posted in Editor, Powershell, Snippets, vscode

More Snippets but being Verbose

So this is another 101 level post. Most of my scripting has been for automation and I try to  debug enough that the code is ready to go. I have dabbled with Verbose messaging and normally it didn’t apply so I was easily distracted. I added a personal ToDo, learn to use this handy feature. Quick note, when I have written GUI’s for my scripts. Since they are written for other uses some messaging was in order to save me some helpdesk calls about it. In short I would add a StatusBar with text regions and I would basically add Verbose like messages there. Moving on…

TLDR; so guilty here as I started with If statements and check for Verbose and then change $VerbosePreference, write the message and then set the $VerbosePreference back to where it was. See, this is why I bailed. I also walked on kitten-shells around the write cmdlets with all of the back and forth about write-host and it killed kittens. Focused, let’s do this.

First step: remember the code you should always be adding to your Scripts and Functions

Param ()

Again you should be preferencing your scripts and variables,  these  two lines of code make your code “Advanced”.

How does this help? Being an advanced Script or Function you immediately have access to “Common Parameters” and with that Verbose parameter is inherited. No let’s risk the ridicule and look at that Write-Verbose cmdlet.

Write-Verbose -Message "Okay you are in the Begin block"

That was all it took. No need for If statements or sub-functions… just use that cmdlet.

Since the operator is running in Verbose mode I will be adding this line of code I will be adding this very often. I ran a few test runs and I wanted to include the Lifecycle Block the message is coming from so I started to format the message with [BlockName]Message text. like:

Write-Verbose -Message "[BEGIN]Okay you are in the Begin block"

Admittedly I have seen this on petri and just saw it as a great practice. Lots of manual typing right. How do  we make that easier? 3.2.1 Snippets!!!!

I added the following to my PowerShell.json file:

<div>        // Adds verbose messaging in the BEGIN block</div>
<div>        "Add Verbose in Begin block": {</div>
<div>            "prefix": "verbBEGIN",</div>
<div>            "body": ["Write-Verbose -Message \"[BEGIN]${1:Message}\""],</div>
<div>            "description": "Add verbose messaging for activity in the BEGIN block"</div>
<div>        },</div>
<div>        // Adds verbose messaging in the PROCESS block</div>
<div>        "Add Verbose in Process block": {</div>
<div>            "prefix": "verbPROCESS",</div>
<div>            "body": ["Write-Verbose -Message \"[ROCESS]${1:Message}\""],</div>
<div>            "description": "Add verbose messaging for activity in the PROCESS block"</div>
<div>        },</div>
<div>        // Adds verbose messaging in the END block</div>
<div>        "Add Verbose in END": {</div>
<div>            "prefix": "verbEND",</div>
<div>            "body": ["Write-Verbose -Message \"[END]${1:Message}\""],</div>
<div>            "description": "Add verbose messaging for activity in the END section"</div>
<div>        }</div>

So while in VSCode, working on some code I can type verb and I will see the snippets for each LifeCycle Block. I select the appropriate one, the snippet text is inserted into the code and the message that follows the Block name is highlighted for editing and press tab and I am done.

Something like this:

VSCode - Verbose Message