Monthly Archive

Categories

Monthly Archives: January 2016

PowerShell conferences 2016

April is going to be a very busy month. It starts with the PowerShell plus DevOps Global Summit in Bellevue, Washington - https://eventloom.com/event/login/PSNA16

 

The PowerShell Conference EU 2016 follows later in the month - http://www.psconf.eu/

 

I’ll be speaking and running pre-conference workshops at both events. Hope to see you at one or the other.

European PowerShell conference 2016

PowerShell Conference EU combines the former “Deutsche PowerShell Konferenz” and “PowerShell Summit EU” into one great big 3-day PowerShell event for Admins and DevOps in Europe and takes place April 20-22 in Hannover/Germany. With more than 40 international speakers including PowerShell inventor Jeffrey Snover, and more than 60 sessions, you are cordially invited to join this massive European PowerShell event. The agenda is up on www.psconf.eu, and registration is open. Seats and hotel capacity are limited so don’t wait and register!

 

Psconf.eu is partnering with powershell.org and psconf.asia, and together we want to ensure that you have a comprehensive local PowerShell conference in your region of the

Testing against an arrays contents

You may need to test if a value is a member of an array. PowerShell provides 2 operators for testing array membership  - -   -in and –contains.

Simple usage is like this

 

PS> $colours = 'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'

 

PS> 'blue' -in $colours
True

PS> $colours -contains 'blue'
True

 

Note the order of value and array changes between the operators.

 

You  can also use these operators in Where-Object

 

PS> $testcolours = 'blue', 'pink', 'yellow'

 

PS> $testcolours | where {$_ -in $colours}
blue
yellow

PS> $testcolours | where {$colours -contains $_}
blue
yellow

 

Often the value we want is a property of an object

 

PS> $to = New-Object -TypeName PSObject -Property @{Colour = 'green'}
PS> $to1 = New-Object -TypeName PSObject -Property @{Colour = 'pink'}

 

PS> $to, $to1 | where Colour -in $colours

Colour
------
green

PS> $to, $to1 | where {$colours -contains $_.Colour}

Colour
------
green

 

The –in operator can be used in the simplified Where-Object syntax (it was introduced for that purpose) but –contains has to use the full, original syntax

 

For testing non-membership you also get –notin and –notcontains

 

PS> $to, $to1 | where Colour -notin $colours

Colour
------
pink

PS> $to, $to1 | where {$colours -notcontains $_.Colour}

Colour
------
pink

WMI discovery

I’ve been working with System Center Configuration Manager this week. SCCM uses a lot of WMI. However, WMI discovery is still a bit of an art.

 

I needed to find the SMS_SCI_Component class.

 

If you know the namespace you can easily find the classes in that namespace but namespaces are hierarchical so you need to work down the tree.

The root namespace is at the top of the namespace tree so

C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace root

Name
----
subscription
DEFAULT
SCCMDP
CIMV2
msdtc
Cli
nap
MicrosoftIISv2
SECURITY
CCMVDI
NetworkModel
RSOP
SMS
ccm
StandardCimv2
WMI
AccessLogging
directory
Policy
InventoryLogging
Interop
Hardware
ServiceModel
Microsoft
aspnet

 

After a bit of digging I found I needed the sms namespace

PS C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace 'root\sms'

Name 
----    
site_PS1

 

which leads to:

Get-CimInstance -ClassName SMS_SCI_Component -Namespace 'root\sms\site_PS1'

 

What if you only know the class?

There’s a little trick using Get-WmiObject

Get-WmiObject -Namespace root -Recurse -List -Class SMS_SCI_Component

   NameSpace: ROOT\SMS\site_PS1

Name  
----      
SMS_SCI_Component      

 

The trick is to start with the root namespace and use the –Recurse (search through all child namespaces) and –List (just give class details) parameters.  You’ll see a listing by namespace for each instance of the class discovered.

Starting to use PowerShell

A common question goes along the lines of  “I’ve leant PowerShell (from class, book etc) but what do I do next?”

 

The usual answer is to pick a problem in your organisation and solve it.

 

I’ve provided an example in this UK TechNet blog post

http://blogs.technet.com/b/uktechnet/archive/2016/01/04/starting-your-powershell-journey.aspx

 

Its the second one of a series on PowerShell

2015 December Scripting Games Puzzle

The December 2015 puzzle was based on the 12 Days of Christmas song.  Starting with this here-string.

$list = @"
1 Partridge in a pear tree
2 Turtle Doves
3 French Hens
4 Calling Birds
5 Golden Rings
6 Geese a laying
7 Swans a swimming
8 Maids a milking
9 Ladies dancing
10 Lords a leaping
11 Pipers piping
12 Drummers drumming
"@

 

A here-string is a multiline string. It is NOT an array – its a single string. The first task is to  split into an array of individual lines:

 

## split into individual lines
$s = $list -split '\n'

Each line of the multiline here string ends with a new line – so that becomes the split point.

 

The array can be sorted by length of line. Ascending or descending wasn’t specified so here’s both options:

'Sort by length ascending'
$s | Sort-Object -Property Length

 

"`n "
'sort by length descending'
$s | Sort-Object -Property Length -Descending

 

Removing the numbers to give justthe text and sorting by length of text. I trmmed the strings as some empty spaces had appeared in the arrays. I think because I copied the here-string

"`n "
'remove numbers sort by length ascending'
$s.Trim() -replace '\d{1,2}\s', '' | Sort-Object -Property Length #| group length | ft -a -wrap

 

"`n "
'remove numbers sort by length descending'
$s.Trim() -replace '\d{1,2}\s', '' | Sort-Object -Property Length -Descending

 

Create objects. Split on white space and restrict output to 2 elements – number and text in this case. Create object using New-object

"`n "
#'create objects'
$items = @()
$s.Trim() | foreach {
    $item =  $psitem -split '\s',2
    $items += New-Object -TypeName PSObject -Property @{
        Count = $item[0] -as [int]
        Item = $item[1]
    }
}

 

Count the number of birds

"`n "
'count of birds'
$birdcount = ($items -match '(Partridge|Doves|Hens|Birds|Geese|Swans)' | Measure-Object -Property Count -Sum).Sum
$birdcount

 

Count all items

"`n "
'count of items'
$itemcount = ($items | Measure-Object -Property Count -Sum).Sum
$itemcount

 

If you treat the song as stating the gifts are cumulative then how many gifts are given in total.  Each item is given (13 – the day on which its given) times i.e. 12 to 1 times respectively.  The total number of items can be calculated like this

"`n "
'cumulative count of items'
$total = 0
$items | foreach {$total += $psitem.Count * (13-$psitem.Count) }
$total

 

As a bonus here’s how you calculate the cumulative number of each type of item.

"`n "
'cumulative number of each item'
$totalitems =@()
$items | foreach {
    $totalitems += New-Object -TypeName PSObject -Property @{
        Count = $psitem.Count * (13-$psitem.Count)
        Item = $psitem.Item
    }
}
$totalitems

Happy New Year 2016

A new year and a number of things to which we can look forward.

The re-release of WMF 5.0. The WMF 5.0 download was pulled because a bug was over writing the module path. Look fot the download being available again soon.

Windows Server 2016 – Containers and Nano server give lots of new options for deployment and lots of new PowerShell to play with. There may even be other PowerShell changes – who knows given the new release processes.

In early April we have the PowerShell + DevOps Summit in Seattle. Late April sees the European PowerShell Conference in Hanover (tracks in German and English). Registration for both events is open.

The UK PowerShell group is reviving. After meetings in London and Manchester planning is underway for the next meetings – look for more info soon.

PowerShell in Action, third edition will appear.  Its already in early release and will complete this year.

A few other projects are in the pipeline which may or may not come to fruitition.

One prediction I can confidently make is that 2016 will NOT be the year that Unix/Linux conquers the desktop Smile