Monthly Archive

Categories

PowerShell

PowerShell version

Depending on the version of Windows you’re running you could be using PowerShell version 1 through version 5.1 (admittedly I suspect there are very few people, if any, still running PowerShell v1). This is complicated by the various versions of Windows Management Framework that are available for download and the large number of alpha and beta versions of PowerShell v6 that have been made available. So how do you know which PowerShell version you have on any given machine?

 

The easiest way is to use the $PSVersiontable automatic variable.  On a Windows 10 machine you’ll see something like this:

PS> $PSVersionTable

Name                           Value 
 ----                           ----- 
PSVersion                      5.1.16299.19 
PSEdition                      Desktop 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...} 
BuildVersion                   10.0.16299.19 
CLRVersion                     4.0.30319.42000 
WSManStackVersion              3.0 
PSRemotingProtocolVersion      2.3 
SerializationVersion           1.1.0.1

 

Notice the PSVersion property. You could just type

PS> $PSVersionTable.PSVersion

Major  Minor  Build  Revision 
 -----  -----  -----  -------- 
 5      1      16299  19

 

On PowerShell v1 $PSVersionTable doesn’t exist so you’ll get nothing returned.

A PowerShell v6 instance on Windows will return

PS C:\Program Files\PowerShell\6.0.0-beta.9> $PSVersionTable

Name                           Value 
 ----                           ----- 
PSVersion                      6.0.0-beta.9 
PSEdition                      Core 
GitCommitId                    v6.0.0-beta.9 
 OS                             Microsoft Windows 10.0.17035 
 Platform                       Win32NT 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...} 
PSRemotingProtocolVersion      2.3 
SerializationVersion           1.1.0.1 
WSManStackVersion              3.0

 

Notice the similarities and differences – especially the PSEdition property

PowerShell v6 on Linux gives something similar

 Name                           Value 
 ----                           ----- 
PSVersion                      6.0.0-beta.9 
PSEdition                      Core 
GitCommitId                    v6.0.0-beta.9 
 OS                             Linux 3.10.0-514.6.1.el7.x86_64 #1 SMP Wed Ja... 
 Platform                       Unix 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...} 
PSRemotingProtocolVersion      2.3 
SerializationVersion           1.1.0.1 
WSManStackVersion              3.0

When looking at PowerShell versions remember that functionality was introduced in this order:

PowerShell v1 – basic core cmdlets

PowerShell v2 – remoting, modules, jobs, WMI cmdlets (apart form Get-WmiObject)

PowerShell v3 – CIM cmldets, CIM sessions, workflows

PowerShell v4 – DSC

PowerShell v5 – PowerShell classes

PowerShell v6 – check the release notes as it changes so much at the moment

Constrained PowerShell or JEA?

PowerShell remoting gives you access to all of the functionality on the box by default. You can created constrained (or restricted) endpoints that limit that functionality to specific cmdlets.

 

Alternatively you can use Just Enough Admin (JEA) to lock down an endpoint through  a Role Based access Control (RBAC) system.

 

JEA is the later option and is more flexible.

 

The PowerShell Team has an interesting (and new to me) take on Constrained Language and JEA.  https://blogs.msdn.microsoft.com/powershell/2017/11/02/powershell-constrained-language-mode/

 

I recommend you read it

Registration for 2018 PowerShell Summit

Registration for the 2018 PowerShell + DevOps Global Summit is open.

These are the important links you'll need:

Summit information

Registration

Agenda

PowerShell Attacks–advice on defending

PowerShell Attacks–advice on defending from Lee Holmes – PowerShell security expert – is available at

https://blogs.msdn.microsoft.com/powershell/2017/10/23/defending-against-powershell-attacks/

Read, learn, inwardly digest and apply

PowerShell + DevOps Summit 2018 schedule

The schedule for the 2018 Summit still needs a little bit of polishing to finish it but it's taking shape. I've started releasing information on sched.com that we're using for all our scheduling needs for the Summit. The one and only truth regarding the sessions and their times can be found at https://powershelldevopsglobalsummit2018.sched.com/

 

I'll be adding sessions over the next few days so keep checking.

 

I'm really excited about the schedule for the 2018 Summit. We'll have 4 rooms for sessions with many of your favourite speakers returning and many new speakers which is really good to see. The Community Lightning Demos return by popular acclaim and we'll be running an Iron Scripter competition as well. The PowerShell Team will be presenting all day Monday and at other sessions through out the Summit.

 

Registration opens 1 November and once you're registered through eventbrite your information will be sync'd to sched.com so that you can access the schedule and use the scheduling app.

PowerShell templating systems

Code reuse is a big plus when you’re developing lots of scripts. The ability to create templates for your code can also save you lots of time – for instance when advanced functions first appeared I create myself a template the included all of the validation checks, the code to switch functionality based on parameter sets and code to handle –whatif and –confirm.  Things have moved on since then and there are a number of PowerShell templating systems.

 

Plaster – https://github.com/PowerShell/Plaster

 

PSScaffold – https://github.com/RedeployAB/PSScaffold

 

PowerShell template - https://github.com/kayasax/PowershellTemplate

 

Check them out – one or more maybe the thing you need or they may inspire you to create something better.

PowerShell Trim

PowerShell Trim – no its not a new slimming fad. Trimming is the act of removing characters – usually white space – from the beginning or end of a string.

You have three methods on the String class you can use

Trim
TrimEnd
TrimStart

 

PS> $str = "   abcdefghijk   "
PS> $str.TrimStart()
abcdefghijk
PS> $str.TrimEnd()
abcdefghijk
PS> $str.Trim()
abcdefghijk

 

You can also trim specific characters from the beginning or end of a string

PS> $str = ".,:abcdefghijk:,."

PS> $tc = [char]'.', [char]',', [char]':'

 

PS> $str.TrimStart($tc)
abcdefghijk:,.
PS> $str.TrimEnd($tc)
.,:abcdefghijk
PS> $str.Trim($tc)
abcdefghijk

 

You should now be able to handling any string trimming required in your code

PowerShell editors

PowerShell works great when you use it interactive;y but at some point you’re likely to want to write substantial pieces of code – scripts or functions – for which you’ll need an editor. This is my take on PowerShell editors.

 

There are PowerShell add-ins for Visual Studio – one of the best is PowerShell Tools by Adam Driscoll: https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsforVisualStudio2017-18561

 

If you’re working with Visual Studio already and need to add PowerShell to your project then this is a good option. if you’re only creating PowerShell code then Visual Studio has too much overhead.

 

PowerShell ISE (Integrated Scripting Environment) was introduced with PowerShell v2. Its gone through a number of updates with subsequent versions of PowerShell. It’s the tool I normally use. It supplies access to the debugger and happily runs across a number of PowerShell runspaces. You can use it to run and debug code on remote machines.

 

ISEs drawback is that its Windows only and it isn’t part of PowerShell v6.

 

As far as I can tell all of the editor related development work is going into VSC (Visual Studio Code) https://code.visualstudio.com/. VSC is a lightweight editor like ISE but can also work with a large number of other languages. You can tailor VSC by only downloading those extensions you need. I have extensions for PowerShell, JSON, XML MSSQL, markdown and Docker among others.

 

VSC has good debugging tools. I do miss the ability to run selected lines of code like ISE. I’ll be sticking with ISE for demos for now.

 

VSC has the advantage that its available for Windows and Linux (and mac) so you can use the same editor cross platforms if need be.

 

There are other products you can consider. ISE Steroids (from the PowerShell studio) is a great add-in for ISE. Sapien have tools such as PowerShell Studio and Primal Script. Idera have PowerShell Plus. These and other tools have to be paid for.

 

So what editor should you use.

 

ISE is great for Windows only environments. If you need cross-platform or multi-language consider VSC.

 

I use ISE but I’m increasingly turning to VSC as that seems to be getting the development effort and evolving very quickly with updates most months.

Call for topics closing 1 October

The call for topics is closing 1 October at 23:59 GMT. We’ve had a fantastic set of submissions. Creating an agenda for the 2018 Summit is going to be very difficult because we’ve had so many fantastic sessions submitted and I don’t have enough slots to  take them all.

 

The call for topics is hosted by papercall.io – highly recommended – and the cut off is automatic.

 

I WILL NOT ACCEPT ANY SESSIONS SUBMITTED AFTER THE CUT OFF DATE.

DSC–the future?

I was incredibly excited when I first saw DSC – it was in April 2013 as a special MVP only preview at the first PowerShell Summit – as a work in progress. Since that time my excitement has waned to the point that I now ask DSC – the future?

 

Looking at the PowerShell Team announcement about the introduction of DSC core - https://blogs.msdn.microsoft.com/powershell/2017/09/12/dsc-future-direction-update/

I have to question if a complete new version of DSC is worth my time investigating especially when backward compatibility isn’t guaranteed.

 

Don Jones has question where DSC is going - https://powershell.org/2017/09/13/the-future-of-powershells-desired-state-configuration/

 

Overall, I’d say that DSC has had a mixed success since its introduction due to changes and the difficulty around preforming some activities. The integration with Azure hasn’t been smooth.

Is it time to look at another configuration tool set. Microsoft’s  current rush to embrace Linux may make Chef or Puppet a better option for you. Read the comments on the PowerShell Team announcements to see what other people think about DSC.