Windows Updates CIM classes

When Windows 10 and Server 2016 were released they contained a new CIM namespace – ROOT/Microsoft/Windows/WindowsUpdate

This contained a CIM class MSFT_WUOperationsSession that had 2 very useful methods – ScanForUpdates and  ApplyApplicableUpdates.

These methods enabled you to find and install updates from the Windows Update site or a WSUS server if you’d configured the machine to use WSUS.

Best of all the CIM class could be accessed and used remotely which was a huge step forward over the old COM object that couldn’t be used remotely.

Things changed with Windows 10 Fall Creators Update (1709) and Windows Server 1709. MSFT_WUOperationsSession still exists but the methods you need to scan for updates and apply updates are now on the MSFT_WUOperations class. The great thing is that they’re static methods so using them is easier. The bad – no really, really bad – thing is that this class CAN’T BE ACCESSED REMOTELY through a Windows remoting session,  a CIM session or a SSH remoting session.

This takes us back to the bad old days of using COM objects. There doesn’t seem to be any reason or explanation for this decision.

I have managed to use the class remotely through a PowerShell direct remoting session though which means that I can force updates in my lab. Its not really an answer for production though

Advertisements
Posted in PowerShell and CIM, WSUS | 1 Comment

Test-Path -PathType

Test-Path is a useful cmdlet for testing the validity of a path. You get a True/False return depending on if the path exists or not.

There’s also a way to add to the test by determining if the path points to a container (folder) or a leaf (file) using the –PathType parameter.

Here’s some examples that show how it works:

PS> Test-Path -Path C:\Scripts\
True
PS> Test-Path -Path C:\Scripts\ -PathType Any
True
PS> Test-Path -Path C:\Scripts\ -PathType Container
True
PS> Test-Path -Path C:\Scripts\ -PathType Leaf
False
PS> Test-Path -Path C:\Scripts\foo.txt -PathType Container
False
PS> Test-Path -Path C:\Scripts\foo.txt -PathType Leaf
True

C:\Scripts is a folder and foo.txt in a file in that folder. 

Test-Path –PathType is a good way to focus your test to ensure you’re dealing with a file or a folder as appropriate.

Posted in Powershell | Leave a comment

PowerShell v6: #9 Release candidate 2 features

A couple of features of PowerShell v6 release candidate 2 need commenting on.

Firstly, I was surprised when installing RC 2 on a Windows 10 machine (Insider build) that RC1 was removed. In the past you’ve been able to run numerous versions of PowerShell v6 side-by-side. This has consequences if the behaviour continues into the GA version and the 6.1 alpha/beta releases as you’ll have to choose between the stable, production version and the changeable new version.

Secondly, and this one was publicised by the PowerShell team, Pester is no longer installed by default with PowerShell v6. You can download the latest version of Pester from the PowerShell gallery. Not sure on thinking behind this decision but the download is easy so it’s not a deal breaker –you just have to remember to do it. May be best to use Save-Module for the download so you don’t have to perform the download if a new version of PowerShell v6 becomes available and overwrites the current version.

Next job is to install the OpenSSH optional feature and see how that goes. Be very interested to see if and how its updated as new versions of the  Windows Insider builds are released.

Posted in PowerShell v6 | Leave a comment

PowerShell v6: #8 Release candidate 2

Release candidate 2 for PowerShell v6 is available for download from – https://github.com/PowerShell/PowerShell/releases

One worrying point is the the OpenSSH implementation which is required for remoting to and from Linux systems doesn’t appear to be any where near a release candidate – https://github.com/PowerShell/Win32-OpenSSH/releases. The latest release as of this writing is 0.0.24.0. The steps to install OpenSSH are still too complicated and time consuming. There is an installation script but it doesn’t complete all of the manual steps!

at the moment I’d really recommend that you only install OpenSSH where you need it and don’t consider it as a replacement for WinRM based remoting apart from a few narrow scenarios:

– Windows <-> Linux remoting

– Cross domain remoting

– Remoting to or from a non-domain system

Ducks need to be lined up if PowerShell v6 is going to GA in January 2018 

Posted in PowerShell v6 | 2 Comments

Using the Where method

You don’t usually see people using the where method. A recent question on the forums highlighted using the where method.

PowerShell deals in collections and will automatically create a collection of objects if the are multiple objects returned for instance

$procs = Get-Process

Iterating over a process can take time. PowerShell introduced 2 methods on collection to make this easier and much  faster to execute – where and foreach.

As an example

$Paths = @(
@{NodeName = ‘VMNAme1’; Instance = ‘Internal’ ; Path = ‘C:\Temp\’},
@{NodeName = ‘AnotherName’ ; Instance = ‘External’ ; Path = ‘C:\Temp2’},
@{NodeName = ‘VMNAME2’; Instance = ‘Internal’ ; Path = ‘C:\Temp1\’}
)

Get-ChildItem -Path $paths.where({$_.Nodename -eq $env:COMPUTERNAME}).Path

Depending on the machine on which you’re running you’ll get the appropriate folder.

The important bit is the $_.Nodename . You need to use the old style Where syntax when using the where method.

Posted in PowerShell v6 | 2 Comments

Avoid the truncation in the display

A question came up regarding my recent post about using the file system COM object to get folder sizes. The question was about the folder path and how you could see the whole path. In other words how to avoid the truncation in the display and the three dots.

When PowerShell displays objects it will automatically use a table format if the object has four properties or less. It uses a list if there are more properties. This behaviour can be overridden by PowerShell’s formatting system. This behaviour can lead to truncation of one or more fields.

This is the function to get folder sizes

function Get-FolderSize {
 [CmdletBinding()]
 param (
   [string]$path = 'C:\MyData'
 )

if (-not (Test-Path -Path $path)){
   Throw "$path - Path not found"
 }

$fso = New-Object -ComObject "Scripting.FileSystemObject"

Get-ChildItem -Path $path -Directory -Recurse |
 foreach {
   
   $size = ($fso.GetFolder("$($PSItem.FullName)")).Size
   
   $props = [ordered]@{
     Name = $PSItem.Name
     Created = $PSItem.CreationTime
     FilePath = $PSItem.FullName
     SizeMB = [math]::Round( ($size / 1mb), 2)
   }

  New-Object -TypeName PSObject -Property $props

 }
}

If you just use the function as is

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts

You can get truncated output – and you don’t see the size!

Office-Excel                         10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-...
Office-OneNote                       10/02/2017 19:16:26 C:\MyData\OneDrive\Data\Scripts\Office-...
Office-Outlook                       10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-...
Office-PowerPoint                    10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-...
Office-Visio                         10/02/2017 19:18:00 C:\MyData\OneDrive\Data\Scripts\Office-...
Office-Word                          10/02/2017 19:16:23 C:\MyData\OneDrive\Data\Scripts\Office-...

 

You could use Format-List

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts | Format-List

which means you get lots of displays like this:

Name     : Event Filters
Created  : 10/02/2017 19:16:31
FilePath : C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvents\PowerEvents\Samples\Event 
            Filters
SizeMB   : 0.02

Using Format-Table with the –wrap parameter will avoid the truncation

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts | Format-Table –AutoSize –Wrap

Samples                                10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent
                                                            s\PowerEvents\Samples                                 
Event Consumers                        10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent
                                                            s\PowerEvents\Samples\Event Consumers                 
Event Filters                          10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent
                                                            s\PowerEvents\Samples\Event Filters                   
ConfigMgr                              10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent
                                                            s\PowerEvents\Samples\Event Consumers\ConfigMgr

Notice that the Size is again cut off.  You need to ensure that your output pane in ISE or the PowerShell console is wide enough to display all the fields or PowerShell will truncate the display – without telling you

Posted in Powershell | Leave a comment

PowerShell v6: #7 Module paths

There is a very significant gap between the functionality available in PowerShell v6 as opposed to PowerShell v5.1. In part this is due to the underlying version of .NET but mainly to the defined module paths in the two versions.

In PowerShell v5.1 I have:

PS>  $env:PSModulePath -split ‘;’
C:\Scripts\Modules
C:\Users\Richard.MANTICORE\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

In PowerShell v6 I have

PS C:\scripts> $env:PSModulePath -split ‘;’
C:\Users\Richard.MANTICORE\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\6.0.0-rc\Modules

The vast majority of the module supplied with PowerShell v5.1 reside in C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules which PowerShell v6 can’t see.

Unless you add it in yourself

$env:PSModulePath = $env:PSModulePath + ‘;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules’

I’ve appended the PowerShell v5.1 modules to the PowerShell v6 module path.

There’s still no guarantee that the modules will work – it depends on the module code and if it accesses .NET classes that aren’t available in .NET core.

You’ll have to use trial and error to determine what works. For instance:

Get-NetAdapter
Get-Volume
Get-Partition

all work. BUT they’re from CDXML modules based on CIM classes not binary modules. As a rule thumb I’d expect the CIM based modules to just work. Binary module will definitely be trial and error.

Posted in PowerShell v6 | 2 Comments