Better way to Reboot a computer

I saw a post of how to use the restart-computer cmdlet. They used wait to pause the process until the computer was back online. This post is just how to take it one step further. If I want my script to wait for the reboot to occur it is normally because I want to perform more actions against it. In this post I am using the restart-computer cmdlet but I am extending it to wait but I am also specifying what it will wait For.  In this case I am waiting for WinRM to come online so that I can connect to CIM.

Yes, I splatted it because I love splatting parameters for readability and flexibility.

#Requires -Version 5
<# .SYNOPSIS     Better way to reboot computers .DESCRIPTION     Really it is just an demonstration of the flexibility of restart-computer. Being able to tell the cmdlet to     wait for a specific element to be online before continuing can help with workflows that require a reboot and     then further action. .PARAMETER ServerName     Name of the computer to be restarted. .EXAMPLE     C:\PS>.\better-rebootcomputer.ps1 -servername testmachine
    Example of how to use this cmdlet
.Link
    https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/restart-computer
.NOTES
    ===========================================================================
    Created with: 	Visual Studio Code
    Created on:   	05.01.2017
    Created by:   	John Kavanagh
    Organization: 	TekSystems
    Filename:     	better-rebootcomputer.ps1
    ===========================================================================
    date initials: Enter first comment here
#>

[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
Param(
     [Parameter(Mandatory = $true,
     ValueFromPipeline = $true,
     Position = 1,
     HelpMessage = "Enter the computer name to be rebooted")]
     [ValidateScript({test-connection -Computername $_ -Count 1 -Quiet })]
     [System.String]$servername
)
$args = @{
    ComputerName = $servername
    Wait         = $True
    For          = "WinRM"
    Force        = $True
}
restart-computer @args
# Now let's test - yes I am using CIM so For has to be WinRM, WMI is up first but CIM
# requires WinRM
$sess = New-CimSession -computer $servername
Get-CimInstance -classname win32_operatingsystem -CimSession $sess<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>

 

Advertisements

PoSH–vscode analyzer and parameters

The more I work with Visual Studio code the more I like it, especially for PowerShell. The Integrated Terminal just gets better and better. It just makes working with scripts much easier and makes me better at crossing the t’s and dotting the i’s.

One thing I came across recently was how the ScriptAnalyzer (a tool not just for vscode ) and Visual Studio Code work together. I was working with some colleagues on taking some code for a project and assembling it into a script. Of course that meant adding advanced functionality.  I wanted to provide parameters for the input that is needed. I started adding a password parameter. While it isn’t a formal credential but rather a simple string applied to a step within the script I used the word password in the parameter name. Then I noticed green squiggly lines for the complete parameter statement. Understanding that this was just a recommendation I messed around with it a little. As soon as I changed the parameter name to something other than “password” the warning/suggestion indicator disappeared. Since I want the operator to understand what the parameter is I kept the name and will live with the green squiggly lines but I just thought that was a nice implementation.

param-detection

Powershell, Visual Studio Code and Extensions

While getting the feel for, and getting even more comfortable with, Visual Studio code I have been playing with a good number of Extensions. While fun, this can be overwhelming as there are a great number of extensions available depending on what you plan on using Visual Studio Code for. The ones I am favoring so far:

I am leaving out themes as I still have not found the Theme for me but there are some interesting choices.

Just some quick code:

Begin {
    $extPath = "C:\Program Files (x86)\Microsoft VS Code Insiders\resources\app\extensions"
}
Process {
    $codeExtensions = get-childitem -Path $extPath -Directory | sort-object -Property Name
    ForEach ($ext in $codeExtensions) {
        $ext.Name
    }
}

I am using Open Live Writer to write blog posts, still hoping a Code Syntax plugin becomes available because in my opinion this a a great blogging tool.

Visual Studio Code and Powershell Snippets the easy way

I spent more time working with Visual Studio Code and Snippets. For my Comment Based Help I did find a way to add the file name but sadly there seems to be no way to include a Date. I have been spoiled by some of the stuff Sapien does and wanted to see if I could reproduce that. Maybe more on that later. If you are looking to quickly create snippets, and this is not just Powershell, but there is an extension that makes it a very simple process. Check out https://marketplace.visualstudio.com/items?itemName=tariky.easy-snippet-maker . There is another one I am testing but as I started to work through these I found something that had me thinking there was a problem but I looked a little further and nope no problem if you know where to look.

What am I talking about? So this extension allows you to select some code, right-click create snippet… enter the Name, prefix and Description and wala you have a snippet. Then I tried it in the Insiders version of Visual Studio Code… hmmm doesn’t seem to work. Uggh. Try again and it says a Snippet with that name already exists. I open the powershell.json file and I can’t find it. What the heck…. then I compared \Roaming\Code\User\Snippets to \Roaming\Code – Insiders\User\Snippets the extension is putting the Snippet in the non Insiders version of the Snippets folder. Just an FYI.

Visual Studio Code Snippets

I am trying to get comfortable with Visual Studio Code for working with PowerShell scripts. Admittedly I still revert back to the ISE to debug the code. Maybe Code will provide that integrated interface one day but we will see. Moving on, one thing I use a great deal of are snippets. Admittedly I was completely lost on how to implement snippets in this IDE, then I found a great article from Keith Hill. This post provides information as well as a great starter file.

Now I want to add my own Snippets. The process is easy enough and the JSON editing in Visual Studio Code is pretty slick. I then found myself struggling with how to create a snippet and using PowerShell syntax create a variable. It starts with a dollar sign, easy enough right? Not really. The dollar sign is part of the snippet language as well. Yes I read this doc but didn’t put one and one together I guess. After some trial and error I found that $$ would then create the PowerShell syntax I was looking to achieve.

https://gist.github.com/jkavanagh58/1e06ac75860340cb1c1399b7676f7ebb.js

image

So in a PowerShell script I start typing the prefix and Intellisense detects possible matches, one of them being my Snippet:

image

Then I click on the User Snippet reference and the result is…

image

Now I have to start converting my library of snippets and then play with the dollar sign operator of Visual Studio Code snippets.