## Time for cube calculation

Posted by: | January 29, 2020 Comments Off on Time for cube calculation |

My recent post on spheres got me wondering about the time for cube calculation.

Defining a variable

PS> \$r = 2.37

I can calculate the cube by multiplying \$r by itself 3 times or by using the Power function on the Math class.

Starting with the simple calculation

PS> Measure-Command -Expression {\$r * \$r * \$r}

PowerShell v7 RC2 took 18 milliseconds; PowerShell v6.2.4 took 9 milliseconds and Windows PowerShell 8 milliseconds

Using the math function

PS> Measure-Command -Expression {[math]::Pow(\$r, 3)}

PowerShell v7 RC 2 took 24 milliseconds; PowerShell v6.2.4 took 18 milliseconds and Windows PowerShell took 14 milliseconds.

PowerShell v7 took longer because its a release candidate rather than release code so has extra stuff to do – as technical as I want to get on that one.

PowerShell v6.2.4 and Windows PowerShell are comparable.

The interesting part is that the messy multiplication is faster than using the Math function – something to remember if speed of calculation becomes an issue in future code.

under: PowerShell

## Spheres

Posted by: | January 29, 2020 Comments Off on Spheres |

Continuing my series of functions that can be used for geometric calculations its time for a quick look at spheres.

Remember in all of these functions that PI is set as a constant when the module is loaded:

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

Calculating the volume of a sphere is relatively straight forward. I used the Math Power function to calculate the cube of the radius rather than multiplying it out 3 times as that starts to look messy.

function Get-SphereVolume {
[CmdletBinding()]
param (
)

\$vol = (4/3) * \$pi * [math]::Pow(\$radius, 3)

[math]::Round(\$vol, 3)

}

The surface area of a sphere is a simpler calculation.

function Get-SphereSurfaceArea {
[CmdletBinding()]
param (
)

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

}

under: PowerShell

## Recent releases

Posted by: | January 28, 2020 Comments Off on 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

under: PowerShell

## Circles

Posted by: | January 28, 2020 Comments Off on 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 (
)

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

}

function Get-CircleCircumference {
[CmdletBinding()]
param (
)

\$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.

under: PowerShell

## VScode improvement

Posted by: | December 31, 2019 Comments Off on 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

under: PowerShell

## Clear the trusted host list

Posted by: | December 31, 2019 Comments Off on 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.

under: PowerShell

## Remove a trusted host

Posted by: | December 30, 2019 Comments Off on 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.

under: PowerShell

Posted by: | December 29, 2019 Comments Off on Add a trusted host |

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

[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

under: PowerShell

## Trusted hosts

Posted by: | December 28, 2019 Comments Off on 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\

under: PowerShell

## Keep it Simple

Posted by: | December 28, 2019 Comments Off on Keep it Simple |

One of the principles I’ve always tried to stick with when writing code – in PowerShell or any other language – is Keep it Simple.

Keeping code simple makes it easier to understand, easier to debug and easier to maintain. My definition of simple code may not be the same as yours but by and large simple code is easier to work with. When I was judging Scripting Games entries it was always easier to work with answers that were written in a simple manner. That doesn’t mean they were crude or badly written – it means they were written in a manner that was easy to understand.

Creating a PowerShell pipeline tends to produce a simple solution – the badly, and incorrectly, named “one-liner”.

Complications seem to creep in when solutions start using aspects of the PowerShell language. Loops and simple branching structures (if & switch) should cover most if not all your needs – I’ve seen many complications arise when multiple nested if statements are used or huge conditions used with multiple nested AND/OR statements – try and simplify.

Still not convinced for the new language features such as ternary conditional, null coalescing etc but PowerShell is now open source so if you shout loud enough you’ll get it added to the language.

Whatever your code, coding standards or goals keeping the code as simple as possible will pay off in the end.

under: PowerShell

Older Posts »