Monthly Archive

Categories

Monthly Archives: December 2016

PowerShell in 2017

As 2016 winds down what do we have to look forward to from PowerShell?

 

In January WMF 5.1 should become available for down level clients. This brings PowerShell 5.1 to Windows 7 SP1, 8.1, 2008 R2 SP1, 2012, 2012 R2

 

Notice that Windows 8 and 2008 aren’t on that list

 

If you’re using DSC I’d recommend bringing all your DSC servers and target machines up to 5.1

 

April brings the PowerShell Summit in Bellevue Washington USA and the PowerShell conference in Hannover Germany

 

Hopefully, Bruce Payette and I will get PowerShell in Action third edition  finished and published

 

PowerShell 6.0 will continue to be developed and the current alpha builds may turn into betas

 

Beyond that – who knows – watch this space…

2016–a PowerShell perspective

2016 was important as the 10th anniversary of PowerShell being released as a web download – announced November 2006 at TechEd/IT Forum in Barcelona. The PowerShell team organised a 8 hour web cast on Channel 9 that I was privileged to be part of.

 

We also saw PowerShell 5.1 become available on Windows 10 and server 2016 (but no PowerShell for managing containers!!!)

 

April saw the PowerShell Summit which returned to Bellevue. The sell out event went very well with some great sessions and participation from the community and especially from the PowerShell team who went above and beyond. Already looking forward to next years event.

 

I also attended the PowerShell conference in Hannover, Germany and had a great time.

 

May saw the one day WinOps event in London – hope that becomes an annual event

 

The big – OK gigantic –event was the move of PowerShell core to the open source and the release of alpha versions of the code for Windows, Linux and Mac. You can mow remote between Windows and Linux!! How this develops is going to be interesting as it is a potentially breaking event for PowerShell or it could be the greatest thing ever only time will tell.

 

2016 was an interesting year and a fitting 10th anniversary for one of the best things to come out of Microsoft

Summit 2017 Registration still open

Just a quick reminder that the registration for the 2017 PowerShell and DevOps Summit is still open – details from https://powershell.org/summit/

Preserving property order

This is a very common pattern:

 

$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem

$props = @{
OS = $os.Caption
InstallDate = $os.InstallDate
LastBoot = $os.LastBootUpTime
Make = $comp.Manufacturer
Model = $comp.Model
}

New-Object -TypeName PSObject -Property $props

 

Get some data – in this case a couple of WMI classes and create an output object.

Unfortunately, the output looks like this

 

Make        : Microsoft Corporation
Model       : Surface Pro 2
LastBoot    : 30/12/2016 09:41:52
OS          : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04

 

The property order is NOT preserved because you’re working with a hash table. If you absolutely have to preserve the property order use an ordered hash table

 

$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem

$props = [ordered]@{
OS = $os.Caption
InstallDate = $os.InstallDate
LastBoot = $os.LastBootUpTime
Make = $comp.Manufacturer
Model = $comp.Model
}

New-Object -TypeName PSObject -Property $props

 

All you do is add [ordered] in front of the hashtable definition and your output becomes

 

OS          : Microsoft Windows 10 Pro Insider Preview
InstallDate : 08/12/2016 13:20:04
LastBoot    : 30/12/2016 09:41:52
Make        : Microsoft Corporation
Model       : Surface Pro 2

 

exactly what you defined

Linux on the desktop

My sense of humour has some quirky moments – as my friends will tell you. Last night I had a lot of time to sit and think (why is a story for another time and place) and it one point a thought about Linux on the desktop struck me.

 

Back in the 1980s when I worked in seismic exploration and desktop computers were emerging I remember seeing articles at least once every year that “this is the year that Unix takes over the desktop”

 

This then morphed to “this is the year that Linux takes over the desktop”

 

Of course neither of these fantasies were actually likely to happen – and I’m deliberately ignoring the Mac OS which is unix based.

 

However, my thought yesterday was that *nix on the desktop is actually a reality. Windows 10 includes the Bash on Ubuntu on Windows option which brings the bash shell to Windows.

Bet this way of getting *nix on the desktop was ever envisaged. Smile

Inertia rules

In this article https://powershell.org/2016/12/15/the-key-to-understanding-powershell-on-windows-or-linux/ Don Jones explains Windows administrators have difficulty explaining, and “selling” PowerShell to Linux admins.

 

I’ve known Don for quite a long time and He’ll be the first to tel we don’t agree on everything so It’ll be no surprise to learn that I disagree with parts of his argument.

 

I also think he’s missed one of the major factors – INERTIA!

 

Inertias can be defined, among other ways,  as a tendency to do nothing or remain unchanged. This is a property of a great many admins I’ve met usually wrapped around the phrase – “but we’ll always done it this way”

 

Inertia is a major governor in the Windows space with the majority of admins still not embracing PowerShell and automation – I’ve even heard “We’re too busy to automate” as an excuse.

 

People resist change. PowerShell represents change so its resisted. Inertia is being overcome in the Windows space and I suspect PowerShell will expand into the Linux space but not quickly. Linux admins have spent a long time learning to use their toolset and that inertia  isn’t going to be overcome quickly.

 

A few iterations of PowerShell through the open source and you might begin to see some traction. Until then inertia rules.

Applying updates through WSUS

I like to keep the virtual machines in my test lab up to date so have a WSUS server to download and manage updates. The difficulty is applying the updates. With Windows 2012 R2 I used a module that would contact the WSUS server and apply the updates – the was especially useful on server core installations.

I found with Windows 2016 that this COM based module wasn’t reliable so after a bit of investigation discovered that there are some CIM classes that you can use to discover and apply available updates and see what updates have been applied.

 

All I need is a simple set of code so wrote a bare bones module that offers three functions:

#Scan for available updates
function Get-AvailableUpdate {
[CmdletBinding()]
param()
$ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
$result = $ci | Invoke-CimMethod -MethodName ScanForUpdates -Arguments @{SearchCriteria="IsInstalled=0";OnlineScan=$true}
$result.Updates
}

#Install all available updates
function Install-AvailableUpdate {
[CmdletBinding()]
param()
$ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
Invoke-CimMethod -InputObject $ci -MethodName ApplyApplicableUpdates
}

#list installed updates
function Get-InstalledUpdate {
[CmdletBinding()]
param()
$ci = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
$result = $ci | Invoke-CimMethod -MethodName ScanForUpdates -Arguments @{SearchCriteria="IsInstalled=1";OnlineScan=$true}
$result.Updates
}

 

Testing so far seems to be good. As this is just for me I’m bothering with adding error testing or other production ready stuff. This works and I’ll fix problems as they occur

Calculating Standard Deviation – the class

You’ve seen how to calculate standard deviation and how to turn that calculation into a PowerShell function. This time we’ll use the calculation to create a class:

class stats {

static [double] StandardDeviation ([double[]]$numbers) {

$mean = $numbers | Measure-Object -Average | select -ExpandProperty Average
$sqdiffs = $numbers | foreach {[math]::Pow(($psitem - $mean), 2)}

$sigma = [math]::Sqrt( ($sqdiffs | Measure-Object -Average | select -ExpandProperty Average) )

return [math]::Round($sigma, 3)

}
}

 

Name the class stats. We’ll create a static method on the class that will calculate the Standard Deviation. The method takes an array of doubles as input – integers are converted automatically to double.

 

The calculation is the same as before. Just add the return keyword to ensure you actually get the output. Using return is mandatory in PowerShell classes.

 

Because its a static class you don’t need to create an instance of the class – just use it directly:

$numbers = 1..10
[stats]::StandardDeviation($numbers)
[stats]::StandardDeviation(1..10)

Calculating Standard Deviation – the function

Last time I showed how to calculate the standard deviation of a set of numbers and said the code could easily be turned into a function

function Measure-StandardDeviation {
[CmdletBinding()]
param (
[double[]]$numbers
)
$mean = $numbers | Measure-Object -Average | select -ExpandProperty Average
$sqdiffs = $numbers | foreach {[math]::Pow(($psitem - $mean), 2)}


$sigma = [math]::Sqrt( ($sqdiffs | Measure-Object -Average | select -ExpandProperty Average) )
[math]::Round($sigma, 3)
}

 

I’ve called the function Measure-StandardDeviation based on Measure-Object producing the mean.

The parameter is set to accept an array of doubles – floating point numbers – integers will be automatically converted to doubles.

To use the function

$numbers = 1..10
Measure-StandardDeviation -numbers $numbers

 

or create the input array directly

Measure-StandardDeviation -numbers (1..10)

Calculating Standard Deviations–the calculation

A while ago I saw something asking about calculating standard deviations for a set of numbers in PowerShell. You can calculate the the mean (average) of a set of numbers using Measure-Object

 

$numbers = 1..10


$mean = $numbers | Measure-Object -Average | select -ExpandProperty Average
$mean
5.5

 

To calculate the standard deviation you:

Subtract the mean of the set from each number in the set and square the difference. Then calculate the mean of those squared differences and finally take the square root of the result.

We already have the mean

 

so lets calculate the differences and square them

$sqdiffs = $numbers | foreach {[math]::Pow(($psitem - $mean), 2)}

 

The Pow static method of the math class is used to create the squares. The standard deviation (normally referred to as sigma) is calculated:

$sigma = [math]::Sqrt( ($sqdiffs | Measure-Object -Average | select -ExpandProperty Average) )
[math]::Round($sigma, 3)

 

You can calculate the mean of the squares of the differences using measure-object again. I used the Round static method of the math class to give my answer 3 decimal places.

 

Now that we can calculate the standard deviation we could turn this into a method, or a PowerShell class or even create a proxy function for Measure-Object that enables you to calculate standard deviation.