Articles published in 2019

I’ve had the following articles published in 2019

https://searchwindowsserver.techtarget.com/tutorial/Set-up-users-with-key-PowerShell-Active-Directory-commands

https://searchwindowsserver.techtarget.com/feature/Editing-content-with-Windows-PowerShell

https://searchwindowsserver.techtarget.com/tip/Windows-Compatibility-module-expands-PowerShell-Core-reach

Enjoy

Advertisements
Posted in Powershell | Leave a comment

Stable sort

In Windows PowerShell if you do something like this:

PS> (1..20 | Sort-Object -Property {$_ % 3}) -join ‘ ‘
9 6 12 15 3 18 19 16 13 10 4 1 7 20 17 2 8 11 5 14

The results come back in an unexpected order. This is not a stable sort as the results are sorted by their modulus result but lose the order within each group.

If you want a stable sort – where the results come back from a calculation like this in the order in they were received you need to add the –Stable switch. BUT that was added to PowerShell v6.1 and later 

PS> (1..20 | Sort-Object -Property {$_ % 3} -Stable ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20

The results are sorted by modulus result and correctly within each group of modulus results

You could also use the Top or Bottom parameters:

PS> (1..20 | Sort-Object -Property {$_ % 3} -Top 20 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20
PS> (1..20 | Sort-Object -Property {$_ % 3} -Bottom 20 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10 13 16 19 2 5 8 11 14 17 20

If you alter the values presented to Top and Bottom you’ll get a subset of the results

PS> (1..20 | Sort-Object -Property {$_ % 3} -Top 10 ) -join ‘ ‘
3 6 9 12 15 18 1 4 7 10
PS> (1..20 | Sort-Object -Property {$_ % 3} -Bottom 10 ) -join ‘ ‘
13 16 19 2 5 8 11 14 17 20

Sort-Object in PowerShell Core has an interesting set of additions compared to Windows PowerShell.

Posted in PowerShell v6 | Leave a comment

OpenSSH installation

OpenSSH installation has got a lot simpler in Windows 10 1809; Windows Server 2019 and Windows Server 1809.

OpenSSH is available as an optional feature. The client is preinstalled when you install the operating system. You just need to install the server:

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Installing the optional feature creates the required firewall rule which is good.

You still need to make changes to the sshd_config file to enable password and pubkey authentication. The subsystem isn’t configured for PowerShell.

There’s a bug in OpenSSH so that subsystem paths with spaces aren’t parsed so you need to create a symbolic link for the PowerShell v6 folder.

if you want to use key-pair authentication the OpenSSHUtils module on the PowerShell gallery has bugs so you need to manually set the permissions on the authorized_keys file.

SSH remoting has a lot to offer but incorrect documentation, the work needed to install and configure it  and buggy software will stop people using it. On the plus side OpenSSH is getting easier to install and configure but its still a long way from good.

Posted in PowerShell v6 | 1 Comment

Create a symbolic link

I recently had to create a symbolic link to overcome a bug in OpenSSH whereby OPENSSH won’t work with the path C:\Program Files\PowerShell\6\pwsh.exe because it has a space.

The answer is to create a symbolic link which is a file that contains a reference to another file (or directory):

New-Item -ItemType SymbolicLink -Path C:\pwsh -Target ‘C:\Program Files\PowerShell\6’

C:\pwsh is the symbolic link and the Target parameter has the original path. You can now use C:\pwsh in place of  C:\Program Files\PowerShell\6

New-Item can also create a Hardlink or a Junction.

HardLink is a directory entry that associates a name with a file on disk

Junction is an NTFS artefact that is a  pointer to a directory on the local volume

Posted in Powershell | Leave a comment

PowerShell Core v6.2.1

PowerShell Core v6.2.1 has been released – https://github.com/PowerShell/PowerShell/releases 

as has v6.1.4

The new versions are to primarily fix the Security Vulnerability CVE-2019-0733 – https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2019-0733

v6.2.1 also enables tab completion for functions

Posted in PowerShell v6 | 2 Comments

Windows Server 2019 updates with CIM

Windows Server 2019 updates with CIM remain the same as all server versions post Windows Server 2016. This code will check for and install any updates. Micorosft Update or WSUS will be used depending on how your system is configured

$au = Invoke-CimMethod -Namespace root/microsoft/windows/windowsupdate  -ClassName MSFT_WUOperations -MethodName  ScanForUpdates -Arguments @{SearchCriteria=”IsInstalled=0″}
$au.Updates

if ($au.Updates.Length -gt 0) {
   Invoke-CimMethod -Namespace root/microsoft/windows/windowsupdate  -ClassName MSFT_WUOperations -MethodName  InstallUpdates -Arguments @{Updates = $au.Updates}
}
else {
   Write-Warning “No updates available”
}

This code should work on Server 1709, 1803, 1809 and Windows Server 2019.

It won’t work on Windows Server 2016 as the CIM classes were changed post Windows Server 2016

Posted in PowerShell and CIM, Windows Server 2019 | Leave a comment

WSL improvements

Windows Subsystem fro Linux – WSL improvements have been recently announced – https://devblogs.microsoft.com/commandline/

WSL 2 is on the way which will allow more Linux apps in WSL including Docker

A Linux kernel will ship with Windows especially tuned for WSL 2

WSL 2 will be much faster and have full system call compatibility.

A new console – now called a Terminal (more Linux terminology) will also become available for WSL, Windows Command prompt and most importantly PowerShell. It’ll feature multiple tabs

Windows Terminal will be shipped via the Windows Store  – do I really want to access an online Store for server software?

Posted in Windows 10 | Leave a comment