PowerShell for Windows Admins


January 3, 2017  11:47 AM

foreach confusion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One of the biggest sources of confusion to people learning PowerShell is foreach.

Don’t worry – it is confusing. Having just read the help file about_foreach its doubly confusing. That help file desperately needs a re-write

Foreach is used in two separate ways with two separate meanings

Firstly there is the foreach language statement or foreach loop. This is used to iterate over a collection of items

$disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" 
foreach ($disk in $disks){
  $fp = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($disk.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($disk.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

In this case the Win32_LogicalDisk CIM class is used to create a collection of disk information. The foreach language statement (loop) is used to work through that collection. For each disk in the disks collection we calculate the percentage free space. If that percentage is less than 60 a warnign message is displayed otherwise an information message is displayed – for versions of PowerShell before 5.0 use Write-Host instead of Write-Information.

PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

Alternatively you could do this

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
foreach {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

Get the disk information but this time pass it into foreach on the pipeline

AGAIN PLEASE DON’T COMMENT THAT YOU WOULD DO THIS ANOTHER WAY – SO WOULD I. I’M USING IT TO ILLUSTRATE FOREACH!

This is where the confusion arises – foreach is used in two different ways.

In the second set of code – foreach is actually an alias for Foreach-object so the code could be written

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" |
ForEach-Object -Process {
  $fp = [math]::Round(($psitem.FreeSpace / $psitem.Size) * 100, 2)
  if ($fp -lt 60){
     Write-Warning -Message "$($psitem.DeviceId) has $fp % free space" 
  }
  else {
     Write-Information -MessageData "$($psitem.DeviceId) has $fp % free space" -InformationAction Continue
  }
}

which makes more sense and is less confusing

Couple of tips:

Foreach in the pipeleine is an alias for foreach object and foreach on its own is the language statement

When writing code use Foreach-Object rather than foreach to reduce confusion

January 2, 2017  3:44 AM

New Year book offer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
book, Powershell

The New Year starts very nicely with POwerShell in Action, 3e being Mannings deal of the day for 3 January 2017

Half off my book Windows PowerShell in Action, Third Edition. Use code dotd010317au at http://bit.ly/2iw7MD2

Links should be directed to the DOTD’s page at https://www.manning.com/dotd


December 31, 2016  5:35 PM

PowerShell in 2017

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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…


December 31, 2016  1:36 PM

2016–a PowerShell perspective

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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


December 30, 2016  2:17 PM

Summit 2017 Registration still open

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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


December 30, 2016  2:10 PM

Preserving property order

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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


December 29, 2016  2:52 PM

Linux on the desktop

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Linux, Windows 10

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


December 27, 2016  5:28 AM

Inertia rules

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Opinion, Powershell

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.


December 22, 2016  1:54 PM

Applying updates through WSUS

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, Windows Server 2016, WMI

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


December 20, 2016  2:20 PM

Calculating Standard Deviation – the class

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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)


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: