Posted in Editor, Powershell, vscode

Let you IDE dress it up

I am re-posting something I posted in a small Slack group.

I started with showing how easy it can be to let your editor (in this case VSCode which personally I can’t see why you would be using anything else but …) come in and make your code look right. Let’s face it, we have an idea for what we are coding, and we want to stay focused on that. Between interruptions, writing, testing, writing again just focus on the task at hand is ideal. One thing that can help with that is the code formatting capabilities of your Editor/IDE.

So in the first video I have a simple scratchpad script where I am testing an If\ElseIf statement versus a Switch statement. There is nothing special in the code, matter of fact it just might be flawed, hence why it is a “scratchpad” script. The point is I was working through and then used Format Document to dress up the code (I believe there is also the use of “Trim Trailing Whitespaces” involved as well).

So as you see the format did dress up my sloppy code but I was not thrilled on how that destroyed my concise switch statement elements. Yes it formatted according to the settings in my settings.json file but…

I kept thinking about that scenario and then I remembered the setting that would probably work through this. In the following video I show the same code, format and the destruction, I mean expansion of my switch elements, Ctrl-Z, change the setting and the Format Document works to my liking…

So I stay in OTSB but because they are simple elements my Switch statement stays just a few lines.

This applies to other languages in Visual Studio Code but this is a PowerShell blog so… If you are using Visual Studio Code for PowerShell work, go into your Settings file (Use the ⚙ icon and select settings and then search for “PowerShell Code Format” .  When you get to the Code Formatting: Preset check here for a great reference when you select the formatting style.

Again, I welcome any questions or feedback you might have. Now back to work.

 

 

Posted in Editor, Powershell, vscode

Warning: Venting over VSCode Install

I spent a good portion of time coming up with a scripted process to help some colleagues to make the move from the ISE to VSCode. It was hard enough to get some to see the value of PowerShell and now to get them off the PowerShell ISE…

Give them a nice clean install of VSCode with extensions, modules, snippet file, settings…

The install-vscode from PSGallery was a great way to automate it with Build Type, Architecture and include extensions….

Now VSCode prompts you to use a different (User versus System) install. Easy enough to download the file or use from a shared location but I have looked at both and I just don’t see a need for this variation or even the prompt…

Starting over again…

Posted in Editor, Powershell, vscode

VSCode – let it help

So I was working on some documentation to help colleagues move off the ISE. ISE I love you but it’s time to go…

This is quick … Splatting has been my favorite technique, not sure it is a technique but.., and then a simple Format Select to align the key pairs. There is a great module for splatting in ISE but we are moving to VSCode

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>
<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>
</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 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

[CmdletBinding()]
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.

BEGIN {
...
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:

BEGIN {
...
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>
<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>
</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

 

Posted in Editor, Powershell, Snippets, vscode

Easy Snippet – Small Tweaks

[These are pretty much 101 level posts]

Still distracted obviously, while on a conference call I was grazing through my VSCode PowerShell snippet file. I came across probably the most basic snippet and noticed it was not only lacking but missing some snippet magic:

        “Function”: {
            “prefix”: “func”,
            “body”: [
                “function $1() {“,
                “\t$0”,
                “}”
            ],
            “description”: “Function”
        },

Look at that mess. What is wrong?:

  1. When used it pastes the function shell but missing the closing curly bracket
  2. The $1() does nothing useful and we know the name of the function should be there.
  3. The $0 was right but again with no closing curly bracket why

So ashamed of myself, okay not really but a chance to improve with what I have learned

The revised version:

“Function”: {
            “prefix”: “func”,
            “body”: [
                “function $FunctionName {“,
                “\t$0# Code goes here\r”,
                “\\}”
            ],
            “description”: “Function”
        },
So what I have now performs the following when I call the Snippet now:
  1. Creates a basic function shell.
  2. The FunctionName term is highlighted first, so you enter the function name before beginning which just seems right.
  3. Once we give the function a name (Verb-Noun of course) we press Tab and we can start replacing “# Code goes here” with the contents of the function.
  4. The closing bracket is there and inline.

This is what it looks like in use.

Code_2018-04-26_16-29-48

 

 

 

Now to go back and fix up my other half-baked snippets and watch to see if I can discover new tricks as the community posts their snippets.