Skip to content

Instantly share code, notes, and snippets.

View DigitalAXPP's full-sized avatar

Alex DigitalAXPP

  • NNIT A/S
  • Czech Republic, Prague
View GitHub Profile
@DigitalAXPP
DigitalAXPP / PowerShell testing in C#.md
Last active November 10, 2023 06:30
The gist lists both options to test PowerShell cmdlets in C#.

PowerShell testing in C#

PowerShell cmdlets can be written in C# by inheriting either form the Cmdlet class or the PSCmdlet class. In short, the difference between both classes are the level of dependence to the PowerShell runspace. The Cmdlet-class is less tighly coupled to the PowerShell runspace, than the PSCmdlet class. A more in-depth comparison between both classes can be found here.

One important difference between both classes are the requirements for automated testing. The Cmdlet class can be easily invoked in C#, while the PSCmdlet class requires to be run in a PowerShell runspace. A great description about the two testing variations can be found in the course by Nathan Honeycutt "PowerShell Cmdlet Development in C# - The Ins and Outs".

Testing for the Cmdlet class

In general, I use for automated testing the XUnit framework.

@DigitalAXPP
DigitalAXPP / PwSh_Recursion.md
Last active July 28, 2020 21:35
PowerShell - Recursive function

PowerShell and recursive functions

What are we talking about here? A recursive function calls itself until, ideally, a base condition is met and the loop exits. The classic example of a recursive function is a factorial calculation. In this calculation, the number multiplies with all its decrementing numbers.

5! = 5 * 4 * 3 * 2 * 1 = 120

The below code displays the recursive function to calculate the factorial of the entered digit.

function factorial ([int]$Number) 

{