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/

Posted in Powershell, Summit | Leave a comment

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

Posted in Powershell Basics | 3 Comments

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

Posted in Linux, Windows 10 | Leave a comment

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.

Posted in Opinion, Powershell | 3 Comments

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

Posted in PowerShell and CIM, PowerShell and WMI, Windows Server 2016, WSUS | Leave a comment

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)
Posted in Powershell Basics | Leave a comment

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)
Posted in Powershell Basics | Leave a comment