Monday, November 11, 2019

Displaying Errors in PowerShell 7 - It Gets Better

In a recent blog post, I described the new approach taken in PowerShell 7 to displaying errors. In that post, I concentrated on the new look and feel of error messages as well as the use of $ErrorView built-in variable. One thing I missed in that post was the new look for run time errors.

To demonstrate the new method of displaying run time errors, consider this script:
# EV1.ps1 - Script to show error view in runtime##  Do something$A = 42$B = $a
#  Now do something tht generates an error (no such file)Get-ChildItem -Path C:\Foo\bar\42.txt##  Now another error1/0# And finish"All Done"
Ok - nothing overly exciting but that's not the point. The script should generate two errors.

In Windows PowerShell (OR if you are running PowerShell 7 with $ErrorView set to "NormalView"), running this script looks like this:


That old familiar Wall of Red Ink! Hard to see the wood for the trees. But you can see the information if you read.

Now - PowerShell 7,. if you are set $ErrorView to "ConciseView" and rerun the script you still get the terrors (or course!), but PowerShell displays them like this:


I hope you'd agree this is a great improvement in displaying runtime errors. And you can always use the new Get-Error command to get all the nitty-gritty if you need it.

As I said in that prior post, I have always loved how PowerShell displays error messaged. I have enough practice to read through that first Wall of Red Ink to see the errors quickly and easily. PowerShell 7 just makes significantly easier. 

Sunday, November 10, 2019

Viewing Errors in PowerShell 7 - Improving on Greatness

As a very long time PowerShell fan, I've always been impressed with the way PowerShell displays errors that occur in scripts or at the command line. PowerShell users are well-acquainted with the "Wall of Red Ink, as I call it:


But once you get over the visual shock and start to actually READ the error message, you begin to appreciate how good these error message actually are. They tell you what the error is and where it happened. I have long evangelised how good these messages are, once you learn to read them. B

But that is not to say things can't be improved. And indeed, in PowerShell 7, you'll find some nice improvements. NB: This blog post is being written using the latest build of the day. By the time PowerShell 7 ships, things may look the same - or different. As an aside, the daily build of PowerShell 7 in incredibly stable - hats off to the engineering team for managing that!

So in PowerShell 7, we have a new built-in variable and a new way of displaying errors. The Variable is $ErrorView which holds an object of the type: System.Management.Automation.ErrorView. This variable can hold one of three values: NormalView, CategoryView, ConciseView. With PowerShell 7, any time an error message is to be displayed, you see different views based on the value of $ErrorView.

A value of  'NormalView' results in the traditional display. But the new default setting, ConciseView, results in just the error message itself, like this:


For day to day use, ConciseView is awesome. BUT, I hear you say, where's all that detail gone. Do you HAVE to use the $Error error record collection?  NO - there's now a cmdlet for that: Get-Error.


With Get-Error you get ALL the error information inside each error record all expanded out nicely. ALL the information without having to expand error records to get to the exception details, etc.

This new feature allows you to limit the Wall of Red Ink to a Line Of Red Ink, and get the error information when you need more details.

What a great new feature!


Tuesday, October 29, 2019

PowerShell 7 Preview 5 Ships

The PowerShell team has shipped another Preview release of PowerShell 7, dubbed Preview 5 (aka P5). P5 both brings great new features and takes us a big step closer to the release of PowerShell 7. Steve Lee posted details of what is in PowerShell V5 in a blog article.

For me there are a couple of notable additions:

The new Pipeline Chain Operators allow conditional execution of commands depending on whether the previous command succeeded for failed:
##  The code:
"Hello" && "There"
# # Produces
Hello
There
## The code
"Hello" || "There"
## Produces
Hello
I suspect we'll see a lot of code starting to use this. But: be careful I suspect a lot of 3AM confusions unless usage is well documented in your scripts.

An interesting feature is the New PowerShell Version notification. When you run PowerShell (P5 or later), during startup PowerShell detects that a newer version is available for download. 


Another cool feature added is adding emphasis to the output of Select-String. Select-String enables you to parse strings using regular expressions. With P5, the matches of the regular expression is highlighted like this:


My favourite new feature is the new condenses error view combined with the Get-Error cmdlet that expands the error report (it is a lot easier than $Error[0] | FL * -Force). Here is an example of what you are going to see:


Why does this matter?
That is one of my favourite questions - after all, it;'s a preview so why should you care? Well for a start, PowerShell 7 is shaping up to be aa huge improvement on the PowerShell experience with Windows PowerShell 5.1. I use it today in my day to day operations and find all the myriad of improvements large and small (I LOVE the CD - feature). If you are using Windows PowerShell today, you should download the Preview (or, if you are brave, the daily build) and start to experiment. 

One significant ad as yet not fully addressed issue with PowerShell 7 is around Cmdlet Coverage. There is no use in a fantastic shell if the cmdlets you use do not work, The change from using the full .NET framework to using .NET Core means that many cmdlets just do not work (and many never will).

The Windows Compatibility module provides partial relief. For example, you can easily use it to load the Server Manager module to allow you to manage Windows Features (i.e. on a Windows Server system). But some modules are not supported this way. For example, the WSUS module is not usable with Windows Compatibility. It's easy to say that the WSUS cmdlet design was poor (it relies on method calls vs using actual cmdlets) - but you are not going to be able to manage WSUS with PowerShell 7 for some time. But this IS a work in progress and I am certain the PowerShell team are taking the feedback on board to enable a better solution to backwards compatibility (although in some cases, the real solution is for the cmdlet designers to re-implement their cmdlets using .NET Core).


Wednesday, October 02, 2019

PowerShell 7's Ternary Operator

The PowerShell 7 team have just released a new PowerShell 7 Preview build. This contains a new, and interesting, new operator, the ternary operator.

This operator is, in effect, a short cut to If/Else. Thus, instead of:

$Message = If ($IsWindows) {"Is Windows"} Else {"Is NOT Windows"}
The Ternary operator allows you to simplify


$Message = $IsWIndows ? "Is Windows" : "Is NOT Windows" 

So a cool new feature coming to PowerShell 7. It is available in the latest Preview but as an Experimental feature. Experimental features in PowerShell 7 are features you turn ON and OFF as you like. By default, experimental features are turned off until you turn them on. To view them and turn them on is simple - just use Get-ExperimentalFeature and Set-ExperimentalFeature like this. 


A relevant question is whether you SHOULD use this operator. For simple things, such as the snippets above, it does enable a more compact script. at the expense of potential confusion by folks not versed in the new syntax. It may not pass the "Three AM test" (ie can you easily and simply work out a bug in this code when woken up at 3 in the morning?). 

Additionally, in my testing, this operator is sometimes a little faster than the old fashioned approach, but there is not a lot in it:

So a new operator, which you have to enable explicitly. It does offer some small performance gains in some cases. Coming soon to a Powershell 7 near you!

Friday, September 20, 2019

Cascadia Code - A New Font On The Block

Microsoft has released a new font, called Cascadia Code. See Thomas Maurer's article for the details.

But so what? Well - this is a font to use in PowerShell or VS Code. Or any Development Environment. It just makes the code easier to read. Here's a look at the font itself:


And here's what it looks like in PowerShell 7's console:


I like it!

To get the font, get it at the Github Repo:https://github.com/microsoft/cascadia-code/blob/master/CascadiaCode-Regular-VTT.ttf

Enjoy

Monday, August 26, 2019

ForEach-Object -Parallel - Comes to PowerShell 7 in P.3

The latest preview edition of PowerShell 7 (itself due for full release at end of Sept or thereabouts) contains a long-awaited feature - the ability to run invocations of a given script block in parallel natively.

An example: Suppose you had a script that used WMI remotely to gather/update information on a number of remote servers. Instead of doing the required operations on one server at a time, running each invocation of the script block in parallel. The ability to run script blocks in parallel has been a huge reason many folks use Workflows (which are not part of PowerShell 7). Foreach -Parallel does away with the need to use workflows just to get parallelism.

Consider the following snippet:

'Non-parallel' $Sb1 = { 1..100 | foreach-object { start-sleep -Milliseconds 100 }} Measure-command { ICM -ScriptBlock $Sb1 } $len = 0

In this sample, you run a script block 100 times. Admittedly the actual script block is not all that exciting but it's an operation that is being done in each iteration of ForEach-Object. The output shows this:

With the third preview of PowerShell 7, the ForEach-Object cmdlet has two new parameters. The first -Parallel which says to run things in parallel. The second, -Throttlelimit tells PowerShell how many separate parallel invocations are allowed. 

Let's rerun that script block but using parallelism and varying the throttle limit:



As you can see, without parallelism, this "script" takes 11.07 seconds. With parallelism, the script takes 4.93 seconds (using throttle limit of 4), 3.70 seconds (throttle limit of 8) and just 2.92 seconds (throttle limit of 12). My system is a dual-processor 6-core system by way of comparison. If you try this, you may get different results depending on your hardware.



Sunday, August 18, 2019

Many Things Go Well with PowerShell 7 - But Not All!

Introduction 

I am currently working on a PowerShell 7 book for those nice people at Wiley (https://blackwells.co.uk/bookshop/product/PowerShell-7-and-NET-Core-3-0-for-IT-Pros-by-Thomas-Lee-author/9781119644729). I'm meant to finish up by December and have the book out in 2020. For me, one of the key questions around PowerShell 7, in the Window space at least, is how well PowerShell 7 can be used to manage an enterprise. If it can, then IT Pros can move towards it with increasing confidence.

PowerShell 7, at the time this is written is at Preview-3 stage with an expectation of shipping sometime in September. If you find I am wrong on any of the points below please let me know. This is a work in progress as we approach PowerShell 7 RTW.

What Works and What Doesn't

In working on the book, I have found that commands, modules, and .NET classes fall into 3 distinct bands:
  • Stuff that works. 
  • Stuff that can work using WindowsCompatibility module
  • Stuff that does not work and probably won't work 

Stuff that works is, well, stuff that works. Most of the basic day to day commands/modules work fine. Which I think is to be expected. A big win here is that the ActiveDirectory module works great (although sadly, the AD Deployment module does not work natively).

The Windows Compatibility module (WCM) is a solution to some compatibility issues. WCM uses a combination of implicit remoting and PowerShell remoting sessions. For each module you want to use - you use Import-WinModule to load the commands in the current PowerShell session. WCM, under the cover, creates a remoting session into PowerShell 5.x on the local machine and imports the underlying module using implicit remoting. Thus you can import the ADDeployment module using this technique and access the commands natively.

Stuff that doesn't work and never well should be a small subset of overall PowerShell functionality. An example is workflows - these depend on Windows Workflow Foundation and thus far at least, there is no interest in porting the necessary .NET classes to .NET Core.

While I never say never, I think if you want to use anything that doesn't work today - you either stick with PowerShell 5.x for the foreseeable future or evaluation alternatives.

Stuff that Doesn't Work and May Never Work

Here is my list of PowerShell 5.x features that are (probably) not ever going to work in PowerShell 7:
  • Workflows - Workflows in PowerShell depends on the Windows Workflow Foundation and that is not in .NET Core. As far as can be told at the time of writing, this is not going to happen. See the release notes at https://docs.microsoft.com/en-us/powershell/scripting/whats-new/breaking-changes-ps6?view=powershell-6.
  • PowerShell Snap-ins - Snap-ins was an earlier approach to add-ins, but were replaced by Modules in Windows PowerShell 2.  Some Microsoft modules were affected but these are being updated. In many cases, you can convert the Snap-in into a module - but many modules delivered by third parties may not be so easy to modify. If you are still using an old snap-in, consider migrating it to a module. 
  • Desired State Configuration - this too is not going forward. At least not yet. Having said that, there might be some updates on this shortly. We'll see.
  • Windows Update - the issue here is that much functionality of WSUS is delivered via methods, not via cmdlets. The WSUS object methods do not work via Windows Compatibility. It turns out that the module also makes use of SOAP which causes more issues with future support. This is unlikely to be supported in PowerShell 7.
  • Best Practices Module - I would have expected this module to have been fully supported.

Things That Work Using the Windows Compatibility Module

There are a number of Windows PowerShell cmdlets and modules which are not supported natively in PowerShell 7 but can be used by way of the WindowsCompatibility module. You load these modules using the Import-WinModule command.

The modules I've found that do not work natively in PowerShell 7 but do work with Windows Compatibility Module include:

  • ADDSDeployment module 
  • AppLocker module
  • BITSTransfer module
  • ConfigCI module
  • DFSR module
  • DHCP Server module 
  • GroupPolicy module
  • IscsiTarget module
  • MsDtc  module
  • NetworkController module
  • NetworkControllerDiagnostics module
  • NetworkLoadBalancingClusters module
  • ServerManager module
  • Storage Replica module
  • The *-Computer commands in Microsoft.PowerShell.Management specifically Add‑Computer, Checkpoint-Computer, Remove‑Computer, and Restore-Computer commands.
  • The *-Counter commands - these are used to obtain Windows performance counters.
  • The WMI cmdlets. I was kind of surprised at this - but still, the CIM cmdlets are just a better way to leverage WMI.
NB: this is a work in progress. I'll update it as I find out more:
1. (20/8) Removed Print Management from a list of modules that do not work - all seems fine with Preview.2 and beyond
2. (21/8) Updated modules that need remediation. 
3. (21/08) Retested and updated based on Preview 3.
4. (2/09) Added issue with Windows Update, added BITS module to remediated list.









Wednesday, July 31, 2019

The End Of My Era

For me, today is a sad day. At least in some ways. Today is the end of my career as a Microsoft Certified Trainer. My MCT  credentials lapse today and as I stopped taking/passing exams, I am no longer able to renew.  So this is the end of my 26+year road as an MCT and the end of my 51 year career in IT, largely focused on Microsoft technologies.

I first got excited about computers in 1968, when I found that the job of an operator paid something like $.50/hour more than washing dishes – and was way cleaner. After a degree from CMU in Computer Problem Solving (what the hip call AI today). I spent three years working both in tee US and the UK for Comshare. They were a time-sharing company and were, in effect, at the beginnings of the Cloud.

After several years as an OS developer, I took a sabbatical for 7 months. The high point of that trip was seeing Mt Everest over my shoulder from 18192 feet.


After returning, I joined Arthur Andersen and a few weeks later was in Chicago at a training event when I read about the launch of the PC and was blown away by the potential of the PC. Within days of the announcement, I was bugging our partners to invest. Looking back, I became one of the first DOS trainers in the world!  During the 1980s I taught to a variety of audiences on subjects including PCs, DOS, dBase, Word and Windows. I left Andersen in 1988 and launched my own company specialising in a combination of training and direct mail. in 1992 – One cool client was The Savoy Hotel Group which was a lot of fun.

I discovered NT (as an early beta tester) and began doing NT training in 1993 – teaching at SkyTech in London. Happy memories. I also taught for Learning Tree. Their NT and TCP/IP courses were awesome.  I then became an MCT at the very opening of the programme and until today I have remained an active MCT (MCT ID 6851).

Those early days were very different from today. To become an MCT you had to attend an evaluation session (often dubbed Shelia York’s Day Of Hell). I failed my first attempt – I was just told to turn up and was never given the relevant information. Second time around I nailed it and became both one of the first MCTs and one of the first MCSEs in the world.  You also had to attend a trainer prep course for any course you wanted to re-deliver – I taught a lot of these in my time which was a real honour to do this training.

I was also active first on MSN, then in the newsgroups, when there were newsgroups. I loved being able to help other MCTs. Like many MCTs, I  spent time working for Microsoft both in Redmond and Europe. As an MCT I served on a couple of advisory boards too. Perhaps the most meaningful for me was the Certified Learning Consultant initiative – requiring learning partners to have suitably qualified learning consultants on staff.

As many of the early MCTs will know, I have a passion for quality. Even when I all too often fall short myself. Such is life. When Lutz Ziob joined Microsoft Learning, he quickly outsourced much of the work that had previously been done internally (with LOTS of contractors – some of them awesome). The outsourcing made sense, unfortunately, the quality of what was produced was incredibly bad. I started a discussion about quality which I am glad to say had a major effect. One thing the discussion surfaced was that students often rated the courseware far better than the MCTs did. If nothing else, this proved the value of the MCT.

I have many happy memories: meeting Bill Gates and getting him to sign my Windows 95 Gold CD.


I also met Steve Ballmer on a couple of occasions. Here’s one:


In my travels, I have had many adventures, lost luggage, horrible rooms, cancelled/rerouted flights. I even spent an evening in jail in Turkey during a military revolution. I have also had the good times – flying Concorde, staying in the Savoy, and eating at the Tuna House. A precious memory was being in the room when Jeffrey Snover launched PowerShell and waving a $20 at him saying I’ll buy it.

So now it's over. I enter full retirement with a mixture of relief(I made it!) and sadness.







What a long strange trip it's been. Thanks for all the fish

Monday, July 22, 2019

Windows Forms - working in PowerShell 7 Preview2

One long-anticipated feature of what is soon to be PowerShell 7 is the use of Windows Forms. Technically, it is the .NET Framework that provides for form handling, but with the latest Preview.2 of PowerShell 7, this is now possible!

Here is a Windows Forms-based script that now works just fine in Powershell 7 Preview 2.

First, here is the code:

# Get-Shares Windows Forms Script
#

# Load System.Windows.Forms
Add-Type -Assembly System.Windows.Forms

# Get PowerShell version details
$Maj = $PSVersionTable.PSVersion.Major
$Min = $PSVersionTable.PSVersion.Minor
$Ver = "$Maj.$Min"

# Create form
$Form = New-Object Windows.Forms.Form 
$Form.Width = 750
$Form.Height = 650
$Form.Text = "My First Windows Forms Application - PowerShell Version $ver"

# Create a "computer name" label control and add it to the form
# Set label location, text, size, etc
$Label1 = New-Object Windows.Forms.Label
$Label1.Location = New-Object Drawing.Point 50, 50
$Label1.Text     = "Computer Name:"
$Label1.Visible  = $true
$Form.Controls.Add($Label1)

# Create a text box to get computer name and add to form
$Text1 = new-object windows.forms.textbox
$Text1.Location = New-Object system.drawing.point 150, 50
$Text1.Text     = "Localhost"
$Text1.Visible  = $true
$Form.Controls.add($text1)

# Create a label to output stuff
$Label2 = New-Object Windows.Forms.Label
$Label2.Location = New-Object Drawing.Point 50, 100
$Label2.Width    = 750
$Label2.Height   = 360
$Label2.Text     = ""
$Label2.Visible  = $true
$Form.Controls.Add($Label2)

# Create a button to get the shares
$Button1          = New-Object Windows.Forms.Button
$Button1.text     = "Push To Get Shares"
$Button1.width    = 150
$Button1.location = new-object drawing.point 350, 50

# Define Getting Shares Button Click handler
$Button1_OnClick = {
  $Label1.Text = "Getting Shares"
  $Shares = Get-CimInstance WIn32_Share -ComputerName $Text1.Text
  $Label2.Font = [System.Drawing.Font]::new('Courier New', 10)

  $Label2.Text = "Shares on $($Text1.Text):`n"
  Foreach ($Share in $Shares) {
    $Name = $Share.Name
    If ($Name.Length -gt 17) {
      $name = $($Name.substring(0,17)+'...').padright(16)
      }

    $Path = $share.Path
    $Label2.Text += "{0,-20}  {1}`n" -f $Name, $Path
  }
  $Label1.Text = "Computer Name:"
}
    
# Add the script block handler
$Button1.Add_Click($Button1_Onclick)    
$Form.Controls.Add($button1)

# Now create a button to close window
$Button2          = New-Object Windows.Forms.Button
$Button2.Text     = "Push To Close Form"
$Button2.Width    = 150
$Button2.Location = New-Object drawing.point 160, 550

# Define Button Click handler
$Button2_OnClick = {
  $Form.Close() 
}

# Add the script block handler
$Button2.Add_Click($Button2_Onclick)    
$Form.Controls.add($Button2)

# finally, show the form as dialog box.
$Form.ShowDialog()


And here you can see the results. First against first a work station:


And here a DC



Success. A working Windows.Forms script that runs great on Windows PowerShell now runs fine in PowerShell 7 Preview.2 (and beyond into infinity....)



Monday, July 01, 2019

Windows Terminal Is Here (in Preview) and it Rocks!

In Windows, PowerShell and CMD.Exe both run inside a console. That console is where programs interact with a command line application. The console inside Windows has not had a lot of love, although has improved of late. But that has just changed big time with the release of the Windows Terminal. If you are running Windows 10 - just go the Windows Store and get it - it's free.

The installation does a great job of detecting the console applications including CMD.EXE, PowerShell.Exe, PWSH.EXE, and the versions of WSL you are running, By default, the terminal installer does not find PowerShell 7.

Configuration of the WIndows terminal is, today, done via hand editing the Profiles.JSON file. On My workstation, this file is: C:\Users\tfl\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState\profiles.json - You may need to adjust it based on your userid.

To enable PowerShell 7,. I just added a new section at the top of the profiles section inside the profiles.json file. I added the following:

 {
   "acrylicOpacity": 0.75,
   "closeOnExit": true,
   "colorScheme": "Solarized Light",
   "commandline": "C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe",
   "cursorColor": "#FFFFFF",
   "cursorShape": "bar",
   "fontFace": "Consolas",
   "fontSize": 12,
   "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff4442}",
   "historySize": 9001,
   "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
   "name": "Windows PowerShell 7.0.0 Core Preview",
   "padding": "5, 5, 5, 5",
   "snapOnInput": true,
   "startingDirectory": "%USERPROFILE%",
  "useAcrylic": false
  },

If you wish to add PowerShell 7, as I do, you need to do is to ensure the guid value inside this profile block is unique. You can just munge an existing GUID, as I did, or use New-Guid cmdllet create one know to be unique.

Since I plan to use Pwsh 7 mainly, I also wanted the default shell to default to using PowerShell 7. That also involves updating the Profiles.JSON file. At the top of the file, in the Globals section, change the value of the defaultProfile to the GUID of Pwsh 7. Mine looks like this


{
 "globals" : 
  {
   "alwaysShowTabs" : true,
   "defaultProfile" : "{61c54bbd-c2c6-5271-96e7-009a87ff4442}",
   "initialCols" : 120,
   "initialRows" : 30,
 ... etc


Once I have set these values in the Profile.JSON file, my terminal looks like this:


And away we go. There is far more you can do with the JSON file and I sure hope that by release later this year, there is a GUI to help configure the settings. I don't mind in the least having to deal with the JSON file and can live with this for now. 

Friday, June 14, 2019

What PowerShell Commands Do You Use Most Often?

I saw this question in a twitter thread. And a good one it is too. My immediate reaction was: Get-Module, Get-Command, Get-Help, and Get-Member. But those are my most  valuable. So what were my most USED? Turns out there’s a script for that:

PS [C:\foo> ]> Get-MostUsedCommands
Count Name
----- ----
1442 git
832 cd
578 ls
353 f
243 cc

I have uploaded this script to the Spiceworks script library at https://community.spiceworks.com/scripts/show/4644-get-the-most-used-powershell-commands - Feel free to download that code and play with it!

But if you are impatient and want the code, here it is:

Function Get-MostUsedCommands {
   <#
.Synopsis
    Gets most used PowerShell commands
.DESCRIPTION
    Uses the parser and command history to construct a list of the
    most used commands and returns a sorted list. The -TOP parameter
    tells how many results to return.
    Based on a tweet by @stevenjudd
.EXAMPLE
    PS [C:\foo> ]> Get-MostUsedCommands

  Count Name
   ----- ----
    1442 git
     832 cd
     578 ls
     353 f
     243 cc
.EXAMPLE
    Another example of how to use this cmdlet
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
.NOTES
    Works in Windows PowerShell 5.1, PowerShell 7 Preview 1.
#>
   # Define the parameters
   [CmdletBinding()]
   Param (
     [Alias("GMUC")]
     $Top = 5 # top number of results to return 
   )

  $ERR = $null
   [System.Management.Automation.PSParser]::Tokenize((Get-Content (Get-PSReadLineOption).HistorySavePath), [ref]$ERR) |
     Where-Object { $_.Type -eq 'command' } |
       Select-Object -Property Content |
         Group-Object -Property Content |
           Sort-Object -Property Count, Name -Descending |
             Select-Object -Property Count, Name -first $Top
}         

Saturday, June 08, 2019

Using PSEdit from the PowerShell Console - There's a script for that!

The PowerShell ISE has a neat built-in function, PSEDIT. It brings one or more files up into exit windows. I used to use this a lot (in the days before I discovered VS Code!). The PSEDIT function used the $PSISE object to add the files into edit windows. But this does not work from withing the PowerShell console.

But as ever, there's a script for that!  Just add the following function definition into your PowerShell 5.x console:

Function PSEDIT {
[CmdletBInding()]
param([Parameter(Mandatory=$true)]$filenames)
foreach ($filename in $filenames) {
dir $filename | 
  where {!$_.PSIsContainer} |
     %{
        start-process $Pshome\powershell_Ise.exe $filename
      }
   }
}

Once you restart PowerShell, you now have that command avaialble in the console.

Of course, in PowerShell 7, this function does not work since there is no PowerShell ISE V7. In PowerShell 7, you should be using VS Code. Once you install VS Code, typing Code $FileName just works as you would expect - and either start a new instance of VS Code with that file or add the file to an open instance. Very civilised - and another reason I love VS Code.

Tuesday, June 04, 2019

First Steps with With PowerShell 7

In my last blog post, PowerShell 7 Is Here - Getting Started, I showed how you can install PWSH 7 and associated tools. I have had some good feedback on that post so it all seems to work (today!).

Having run the Install-PWSH7 script, which you can find at: https://community.spiceworks.com/scripts/show/4638-install-powershell7, you should now have PowerShell 7 Preview 1 on your system.

The first thing to notice is that the executable is now pwsh.exe. Also, the home directory, aka $PSHOME is now: C:\Program Files\PowerShell\7-preview. What this provides is a mechanism to run multiple versions of PowerShell side by side. On my system, I have PowerShell 5.1, 6.2 and 7.0. If you use the MSI to install PowerShell 7, the installer updates your environment path so you can easily find PWSH.EXE.

The next thing to notice is that there are a lot fewer modules immediately available. Since PowerShell 7 is installed in a different folder, modules you may have with PowerShell 5.1 are not visible. One thing I had to do was copy over my personal modules.

The next thing I found is that some modules do not load naturally. So for example, you can not just use Import-Module to import the ServerManager module. The solution to this is to use the Windowscompatibility module you can find on the PowerShell Gallery. Amongst other things, this module uses implicit remoting to access Windows PowerShell cmdlets from PowerShell7, for those modules that are not compatible directly. 

An example of using this is the Get-EventLog cmdlet. By default, running this cmdlet results in errors. But by using the Import-WinModule Microsoft.PowerShell. Management command you can access the windows event log, like this:


For more information on this module, see https://github.com/powershell/windowscompatibility.

So far, the following modules need to be loaded using Import-WinModule:
  • ServerManager
  • ADDSDeployment (and even then some cmdlets do not work properly).
  • DHCPServer
And finally, there are some things that are either not going to be supported or, arguably, are broken. So far, Windows.Forms, DSC and Workflows are not supported. Support for forms should come in later releases, prior to RTM.

With respect to Active Directory, I've worked out how to promote a server to be the first DC in a new forest but so far, I can not add a second DC to that initial domain. Still playing with that.

Another thing I found that is, arguably broken, is that the System.IO.DirectoryInfo object, returned from Get-ChildItem/GetItem cmdlets has changed the way it returns the parent directory. In PowerShell 5.1, the Parent property is just a relative path,, whereas in later versions this property returns the full path.  This will break some scripts. 



Sunday, June 02, 2019

PowerShell 7 Is Here - Getting Started

Like many IT Pros, I’ve been waiting on the first builds of PowerShell 7. Late last week Steve Lee on the PowerShell team inside Microsoft published a road map to PowerShell 7. And on Friday I finally got access to the code! YEAH. It took some playing around with the code, but I now have a basic development environment up and working. And of course, there’s a script for that.
The environment for PowerShell 7, at least for me, includes downloading the latest build and installing Visual Studio Code as my main IDE. Here’s my getting started script:
# 1. Fix Execution polity then install latest versions of Nuget and PowershellGet
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
Install-PackageProvider Nuget -Force |
  Out-Null
Install-Module -Name PowerShellGet -Force –AllowClobber 


# 2. Setup C:\Foo to hold stuff. ignore the error if it already exists,

#    then set location to C:\Foo
New-Item -Path C:\Foo -ItemType Directory –EA 0
Set-Location -Path C:\Foo

# 3. Install PowerShell 7

#    For now, load the preview version.
#    Based on: https://www.thomasmaurer.ch/2019/03/how-to-install-and-update-powershell-6/
$URI = "https://aka.ms/install-powershell.ps1"
Invoke-RestMethod -Uri $URI |
  Out-File -FilePath C:\Foo\Install-PWSH7.ps1
C:\Foo\Install-PWSH7.ps1 -UseMSI -Preview -Quiet


# 4. Download and save Install-Vscode script from PSGallery

Save-Script -Name Install-VSCode -Path C:\Foo

# 5. Now run it and add in some popular VSCode Extensions

$Extensions = "Streetsidesoftware.code-spell-checker",
              "yzhang.markdown-all-in-one",
              "davidanson.vscode-markdownlint"
$InstallHT = @{
   BuildEdition = 'Stable'
   AdditionalExtensions = $Extensions
   LaunchWhenDone = $true}
.\Install-VSCode.ps1 @InstallHT 

# 6. Install WindowsCompatibility module

# the ServerManager module
Install-Module -Name WindowsCompatibility -Force

In step 1, I update the execution policy, and install the latest versions of the NuGet package provider and get the latest PowerShellGet module. Windows 10 ships with older versions of these tools so updating them is a good thing to do. With step 2, I create a  local folder, C:\Foo, which is where I put stuff.  Feel free to change either or both of these steps to suit your needs. IN step 3, I download and run a script to install PowerShell 7 on the local system.  The code here is a slightly more long-winded (but possibly more easily understandable) version of a one-liner posted by @Steve_MSFT. 
In step 4, I download a script that installs VSCode and in step 5, I run the script. This installs the latest stable version of VSCode along with three useful extensions. Adjust this list as appropriate.                 
Lastly, in step 6, I install the WIndowsCompatibility module – which is going to be useful in order to use PowerShell 7 to support a wider set of modules/cmdlets.  I plan to highlight this in coming blog articles.
Now that you have PowerShell 7 land VSCode, you need to set VSCode to a) use your colour scheme of choice then set the version of PowerShell you want VS Code. On my workstation, here’s how that looks:
So now that you have both PowerShell 7 and VS Code installed, it’s time to start getting used to the new version. My next post is going to be around installing AD, and the issues arising (along with solutions where possible).














Monday, April 08, 2019

PowerShell 7 - Coming Soon!

The news is now out and to me clear - the next release of PowerShell, to be known as PowerShell 7 is going to be a huge deal. Steve Lee released the news in a blog post about the next release. There are several big points in Steve's blog post:

First, the next release is going to be called PowerShell 7. They are dropping 'core'  from the product name, and the team are taking the opportunity to up the major build number to 7.0. I think many folks confused the term "core", so this is a great time to tune the product name. The change in version number was also surprising but most welcome - by calling it PowerShell 7, it signals to Windows IT Pros that what you now know as PowerShell Core 6.2 is pretty good on windows (all my Grateful Dead curation scrips work - so that's good!). But it's not 'there' enough: no WPF, no WinForms, and a bunch of cmdlets that are not supported.

Since PowerShell is based on .NET, its hard to do things that are not in the version of .NET in use. So since there is no WinForms support in the .NET version integrated with PowerShell Core 6.2, that version of PowerShell core cannot run Winforms programs. All you need to do is to add the necessary classes and hey presto.

.NET Core 3.0 is the big game changer here. It is intended to have all the underlying APIs that should allow upwards of 90% compatibility with Windows PowerShell 5.1. In my discussions with Steve Lee, I came away with the distinct view that .NET Core would both solve a lot of problems many Windows IT Pros had in adopting the newer versions and be an awesome tool in the Linux world. Imagine being able to write mini-GUIs that work in Linux. Utterly cool in my view and potentially very very useful for the Linux users. When we chatted, the view was to call it PowerShell Core 6.3, but I really like the name PowerShell 7. It not only makes the name easier but it calls out for adoption.

I am looking forward to updating my PowerShell books for PowerShell 7. Can't wait to get stuck in! I'll certainly be blogging more about this new release!

Monday, March 18, 2019

Moving from PowerShell Journeyman to PowerShell Master

I’ve just finished writing another book on PowerShell. The book looked at a number of core Windows Features and components, from AD, DHCP/DNS, SMB file sharing and FSRM, Hyper, V and more. Having gone through the process of witting over 125 scripts using a dozen Windows Server 2018 features  has given me a perspective on both PowerShell in use today, and in what it takes, what you really need to know in order to progress from a Google-Engineering assisted journey man into a PowerShell  master.  There is absolutely NOTHING wrong with using Google to get the job done. And for many their career path possibly precludes acquiring deep skills. But if you are the folks who aspire, please read on!

So what do you need to know, to know how to do? The following list is not in any order:

  • Understand and be able to use the PowerShell language - the starting point, for me, is that you know the PowerShell language and can use it. You should understand the core concepts of cmdlets, objects, and the pipeline. You should be familiar the the core approach to discovery (Get-Module, Get-Command, Get-Help, and Get-Member). You need to know how each language feature works and be able to leverage it in your scripts. Knowing the internal architecture of PowerShell is also almost expected.
  • Understand the .NET Framework. Powershell is built on top of .NET – cmdlets work by using .NET. Get-Process, for example, just calls [System.Diagnostics.Process]::GetProcesses(). Also known as a static method (GetProcesses()) on a .NET Class (Systems.Diagnostics.Process). You should understand the architecture of .NET (CLR, BCL, IL and JIT Compilation, .NET Security, and more).  The .NET Framework can often provide functionality for which there are no cmdlets. For example, there a number of .NET classes useful for localisation. Time zones, clock types, DST/ST, etc are all a method call away and therefore easy to use if you know how.
  • Understand how to read C# and be able to convert C# to PowerShell. There is a feast of wonderful examples of more obscure tasks often written in C#. You should be able to read the C# sufficiently to see how the code is doing things, and be able to concert simple snippets into working PowerShell Code. Knowing enough VB.NET to convert it into PowerShell is also a useful skill.
  • Understand COM and COM objects. There are a number of features that make use of COM. The Microsoft Office products can each be automated using PowerShell’s COM interop features. The Performance Logging and Alerting subsystem makes use of COM. You use PowerShell's New-Object cmdlet to instantiate a COM object to specify PLA data collector sets.
  • Know how to use XML. Some features such as the Task Scheduler make use of XML. You should know how to use the XML emitted by windows as well as knowing how to manage XML documents and the DOM. XPath is also a useful skill. The FSRM, for example, produces reports. The report format is fixed and cannot be modified. But the FSRM also produces XML files containing the report’s raw data for you to format to your own needs. PowerShell also makes use of XML for default object formatting. which you can customise to change how PowerShell formats objects.
  • Know how PowerShell modules work. Cmdlets are delivered in modules and you can write your own. Both DSC and JEA leverage modules. You should know where modules are stores, how PowerShell finds them and how it builds the Module cache, what a manifest is.
  • JEA – Also known as Just Enough Administration. It’s a neat feature that enables you to provide delegated permissions to do just those things necessary for a person’s job and nothing more. This is a very useful security feature of Powershell that appeals to large and distributed organisations.
  • Understand how to implement DSC. DSC is a great way to configure hosts and to ensure they stay configured. You should know about DSC resources, setting up DSC pull servers (SMB and Web), and DSC reporting and error logging. You should also know how DSC resources work as well as how to write your own DSC resources)
  • Master Remoting – This is a rich topic area. You should understand the PowerShell Remoting stack (including PSRM, SOAP, WinRM), how end points work and how to create a constrained end point. 
  • Understand at Depth Core Windows Features. I suppose it’s obvious, but to be a PowerShell master you have to be able to apply your skills  to Windows. You should really understand AD (and GPO), SMB (SMB3 SOFS, Clustering and Hyper-Converged S2D), Containers and Docker, Hyper-V (and maybe VMware too!), TCP/IP Networking, Disk/FIle Storage, PLA, Task Scheduler, and probably more.
  • Leverage Azure – Organisations are increasingly moving to the cloud and knowing Azure (or AWS) is also an important skill. With Azure, you should be able to build IAAS objects in cloud including web sites, VMs, and Virtual networks. You should also be able to manage Azure Storage and Content distribution.
  • Be competent at Windows Troubleshooting – There are a variety of good PowerShell tools that assist in Troubleshooting, particularly network troubleshooting. These tools really are second nature to a PowerShell master. And to be a good trouble-shooter you really need to understand what you are troubleshooting. Knowing how to leverage the information in the event logs is also critical. You should become very familiar with docs.Microsoft.com.  And if you ever work out how to fully automate the Windows trouble-shooters – let me know.
  • Use PowerShell Core and VSCode – PowerShell Core 6 is almost a re-invention of PowerShell. Cross-platform, open source, based on .NET Core which is also open source along with a totally new development tool (VS Code). Arguably,6.1 and 6.2 are not quite ready for hard core usage across all features. But it’s close – I am now using the developing 6.2 and VS Code in preference to the ISE and Windows PowerShell. My Grateful Dead scripts even work in PS Core! There are a number of features that do not work with PowerShell Core. Today, for example, DSC and Windows Forms are not supported (although 6.3 should support Windows.Forms!).

So a baker’s dozen of things you really need to know, and know how to do with PowerShell.

PowerShell Core and Experimental Features

In testing any new feature, one technique for getting users to use (and test) the feature is known as feature flags.  Essentially these are settings (flags) that signal you should gain access to those experimental features. The allows a user to opt-in to testing the new features. Thus is a big application, such as PowerShell, most users just use the published feature set. But if you know how, you can turn on some interesting new features. And of course if you do not like a given experimental feature, you can turn it off.  Let’s look at hot oaccess these features. In this blog post, I am using PowerShell Core 6.2.0-rc.1. If you install different versions, your mileage is going to vary!

Finding Experimental features

Finding experimental features is pretty easy. Hey – this is PowerShell and you should know what to do). Like this:

PS [C:\foo> ]> Get-ExperimentalFeature
Name                        Enabled Source   Description
----                        ------- ------   -----------
PSCommandNotFoundSuggestion   False PSEngine Recommend potential commands based on fuzzy search on a CommandNotFoundException
PSImplicitRemotingBatching    False PSEngine Batch implicit remoting proxy commands to improve performance
PSTempDrive                   False PSEngine Create TEMP: PS Drive mapped to user's temporary directory path
PSUseAbbreviationExpansion    False PSEngine Allow tab completion of cmdlets and functions by abbreviation

So these four experimental feature present on 6.2.0-rc.1 all look pretty cool to me.  The usefulness of these may vary. For me tablcompletion of cmdlet names using abbreviations could interesting. Potentialy really useful so worth looking at even though as I think about it – if the alias is any good, it’s wired into my fingers thus it may be a feature that I never use. Creating the TEMP: folder is something I would take advantage of. The PSCommandNotFoundSuggestion just helps IT Admins to find what they need. And the batching of implicit remoting commands could be very useful if you are, for example, managing Exchange On-Line  using local PowerShell and importing the session.

Enabling Experimental Features

Again – this is PowerShell so: Simples:

PS [C:\foo> ]> Get-ExperimentalFeature | Enable-ExperimentalFeature
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.
WARNING: Enabling and disabling experimental features do not take effect until next start of PowerShell.

So very simple to add in. Due to how these features are implemented you nee to restart Pwsh before you can access the fetures:


Using Experimental Features:

The Command not found suggestion feature is nice. After enabling, PowerShell does a better job of handling typos, like this:

PS [C:\foo> ]> get-chliditem
get-chliditem : The term 'get-chliditem' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-chliditem
+ ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (get-chliditem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


Suggestion [4,General]: The most similar commands are: Get-ChildItem, Get-ChildItem2.

Nice.  It could be really useful where you have long cmdlet names


Disabling Experimental Features

Needless to say, disabling them is simple too: Use the DisableExperimentalFeature cmdlet to disable the commands.


Summary

Experimental features are PowerShell Core features that may or may not be added to future versions of PowerShell Core. They are easy to enable, consume, and disable as you wish but use at your own risk.