Monthly Archive

Categories

Monthly Archives: June 2018

Windows server 2019 preview build 17962

The availability of Windows server 2019 preview build 17962 has been announced.

 

Also available is:

 

A preview of Hyper-V Server 2019

 

System Insights - a new feature bringing local predictive analytical capabilities to Windows Server. Systems Insights is managed through Windows Admin Center or PowerShell

 

Server Core App Compatibility on Demand – includes ability to install system components that need some of the GUI capabilities not present on server core including MMC, perfmon, sysinternals tools and PowerShell ISE

 

A new preview of Windows Admin Center (WAC was formerly known as project Honolulu)

 

I’m not a big fan of WAC – its perpetuating the GUI based focus of admin tools. One good thing in this preview is that the underlying PowerShell scripts are know visible. This may just make it useful as a learning tool in the same way that the AD Adninistrative center is a way to learn how to manage AD with PowerShell

Avoid Add-Member

There’s a huge amount of code still being created that looks like this:

$os = Get-WmiObject -Class Win32_OperatingSystem
$comp = Get-WmiObject -Class Win32_ComputerSystem

$op = New-Object -TypeName PSObject |
Add-Member -MemberType NoteProperty -Name OS -Value $os.Caption -PassThru |
Add-Member -MemberType NoteProperty -Name Version -Value $os.Version -PassThru |
Add-Member -MemberType NoteProperty -Name Name -Value $comp.Caption -PassThru |
Add-Member -MemberType NoteProperty -Name Manufacturer -Value $comp.Manufacturer -PassThru |
Add-Member -MemberType NoteProperty -Name Model -Value $comp.Model -PassThru

$op

 

Some data is acquired and a custom object is created for the output. In this case an empty object is created and Add-Member is used to create and populate the object’s properties.

 

The best thing that can be said about this method is that it works. This method is a hang over from the early days of PowerShell. It’s about time that people moved on and learned how to do this properly.

 

NOTE: Get-CimInstance should be used these days instead of Get-WmiObject as the WMI cmdlets aren’t available in PowerShell v6.0 and are effectively deprecated.

 

Instead of using Add-Member use a hash table to create and populate the properties:

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

$props = @{
OS = $os.Caption
Version = $os.Version
Name = $comp.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
}

$op = New-Object -TypeName PSObject -Property $props

$op

 

This is much cleaner, involves less typing (therefore reducing errors) and is easier to understand.

 

Many people (including those that should know better) whine about the fact that using New-Object means that the order of the properties when displayed isn’t the same as input and so prefer the Add-Member method.

 

If you absolutely have to force the order of properties then use an ordered hash table:

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

$props = [ordered]@{
OS = $os.Caption
Version = $os.Version
Name = $comp.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
}

$op = New-Object -TypeName PSObject -Property $props

$op

 

So, should Add-Member be totally avoided. Not quite. It does have a use – when you want to add a very small number of properties to an existing object. Otherwise don’t use it.