Recent releases

Recent releases of interest include:

PowerShell v7 Release Candidate 2. No significant changes from RC 1 – see

https://github.com/PowerShell/PowerShell/releases/tag/v7.0.0-rc.2

Windows Terminal v0.8.10261. Bug fixes – see https://github.com/microsoft/terminal/releases/tag/v0.8.10261.0

PowerShell v6.2.4. No significant changes – see https://github.com/PowerShell/PowerShell/releases/tag/v6.2.4

With GA of PowerShell v7 close not sure its worth investing too much energy in v6.2.4

Posted in Powershell | Leave a comment

Circles

One of my all time favourite films is “The Thomas Crown Affair” – the original NOT the remake. The films theme tune – Windmillls of your Mind – with its focus on circles got me thinking about geometrical calculations in general and circles in particular. I decided I’d create a library of such calculations – eventually it’ll be available on github – starting with circles.

Two calculations come to mind immediately for circles – area of a circle and its circumference:

New-Variable -Name pi -Value ([math]::PI) -Option Constant

#region CIRCLES
function Get-CircleArea {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $area = $pi * $radius * $radius

  [math]::Round($area, 3)

}

function Get-CircleCircumference  {
   [CmdletBinding()]
   param (
     [double]$radius
   )

  $ccmfrnce = 2 * $pi * $radius

  [math]::Round($ccmfrnce, 3)
}

#endregion CIRCLES

I started by creating a constant for the value of PI. As its a constant you can’t change or remove it. Defining it once saves time and effort.

Both functions take a single parameter – the radius of the circle. The area or circumference is calculated.

Area = pi * radius squared

Circumference = 2 * pi * radius

The output is  rounded to three decimal places.

You’ll notice that the function only contains the calculation. I’ve not added any pipeline ability, help, comments, error handling or any other “production level code”. This is deliberate as I want to concentrate on the calculations and most of these functions will be fairly simple. If you want the bells and whistles think of it as an opportunity <GRIN>

Next up will be spheres.

Posted in Powershell | Leave a comment

VScode improvement

VScode is the recommended editor for PowerShell Core as its multi-platform. Personally, I’ve preferred the Windows PowerShell ISE as its more closely aligned with the code I create and the work I do. The latest VScode improvement means I’m beginning to question that stance.

The latest version of VScode has buttons to run the whole code in the editor window or run selected code. In, and of, itself that’s not a huge improvement but it saves me remembering the specific keystrokes or digging through the menus.

So, thankyou for an improvement that makes things easier for me and means I’m more likely to use VScode

Posted in Powershell | Leave a comment

Clear the trusted host list

The final option in administering the trusted host list is to clear the entire list. The following function will clear the trusted host list

function clear-trustedhost {
[CmdletBinding()]
param (
  [string]$computername = $env:COMPUTERNAME
)

if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
   Set-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername -ValueSet @{TrustedHosts = “”}
}
else {
   Write-Warning -Message “$computername is unreachable”
}

}

Use Set-WSMANinstance to set the value of trusted hosts to being an empty string.

Posted in Powershell | Leave a comment

Remove a trusted host

Continuing our collection of routines to manage the trusted hosts this time we’ll look at how to remove a trusted host

function remove-trustedhost {
[CmdletBinding()]
param (
  [string]$trustedhost,
  [string]$computername = $env:COMPUTERNAME
)

if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
   $th = Get-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername |
   Select-Object -ExpandProperty TrustedHosts

  if ($th) {
     $ths = $th -split “, |,”, 0, “Regex”

    $newth = ($ths -ne $trustedhost) -join “, “
    
     Set-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername -ValueSet @{TrustedHosts = $newth}

  }
   else {
     Write-Warning -Message “No trusted hosts to remove”
   }
  
}
else {
   Write-Warning -Message “$computername is unreachable”
}

}

The trick here is to get the current trusted hosts list and split on the commas to create an array. Create a new trusted hosts array that doesn’t contain the one you want to remove then use the join operator to recreate the string. Write that string back to the system.

Again this needs to be run with elevated privileges otherwise you’ll get an Access Denied error.

Posted in Powershell | Leave a comment

Add a trusted host

Last time I showed how to read the trusted host list  – this is how you add a trusted host

function add-trustedhost {
[CmdletBinding()]
param (
  [string]$trustedhost,
  [string]$computername = $env:COMPUTERNAME
)

if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
   $th = Get-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername |
   Select-Object -ExpandProperty TrustedHosts

  if ($th) {
     $newth = $th + “, $trustedhost”
   }
   else {
     $newth = $trustedhost
   }

  Set-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername -ValueSet @{TrustedHosts = $newth}
}
else {
   Write-Warning -Message “$computername is unreachable”
}

}

Get the current trusted host list and append the new trusted host name. Use Set-WSManInstance to write back the new trusted host list.

The function currently only takes a single new trusted host but you could modify the code to accept an array of computer names and iterate through the array to create the new trusted host list.

You need to be running PowerShell with elevated privileges (as administrator) to be able to write the trusted hosts list back to the system.

This function and last times get-trustedhost both have a computername parameter (defaults to local computer) so you can use these functions (and the upcoming functions to clear and remove trusted hosts) on the local or remote machines

Posted in Powershell | Leave a comment

Trusted hosts

You can use trusted hosts in WSMAN based PowerShell remoting to authenticate computers where Kerberos isn’t available.

The WSMAN configuration is available through the WSMAN PowerShell drive or you can use the WSMANInstance cmdlets – available in Windows PowerShell v5.1, PowerShell 6.2 (Seem to have come back in at PowerShell v6.1) or PowerShell v7

function get-trustedhost {
[CmdletBinding()]
param (
  [string]$computername = $env:COMPUTERNAME
)

if (Test-Connection -ComputerName $computername -Quiet -Count 1) {
   Get-WSManInstance -ResourceURI winrm/config/client -ComputerName $computername |
   Select-Object -ExpandProperty TrustedHosts
}
else {
   Write-Warning -Message “$computername is unreachable”
}

}

The only non-obvious piece of code is the resource URI which you can work out from the examples in the help file. By contrast using the WSMAN drive you need to access

Get-ChildItem -Path WSMan:\localhost\Client\

Posted in Powershell | Leave a comment