Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
July 3, 2024

Pester Testing .NET with Copilot

Over the last few weeks, I've been showing how to build PowerShell tools that rely on the .NET Framework in place of calling native PowerShell commands like `Get-CimInstance`. While you can see performance gains with this approach, there are potential drawbacks. First, your code could be a little more difficult to maintain by others. Native commands are at least easy to understand as long as you use full cmdlet and parameter names. Using the .NET Framework can make your code more arcane, especially for someone with less .NET experience than you. Internal documentation is critical if you take the approach I have been demonstrating. Another area where you can encounter challenges is with Pester tests. You can only write tests for functions and cmdlets. And within those tests, you can only mock a command. You can't mock invoking a .NET method. I thought it would be helpful to write a Pester test for one of my recent functions. Then I realized I could also demonstrate how you might use GitHub Copilot in VS Code to help you write such a test. I'll write a test for this function.

Function Get-OSDetail {

    [CmdletBinding()]

    [OutputType('OSDetail')]

    Param(

        [Parameter(

            Position = 0,

            ValueFromPipeline

        )]

        [Alias('CN', 'Server')]

        [ValidateNotNullOrEmpty()]

        [Microsoft.Management.Infrastructure.CimSession[]]$CimSession = $Env:ComputerName

    )

    Begin {

        Write-Verbose "Starting $($MyInvocation.MyCommand)"

        #define the WQL query

        $Query = 'Select CSName,Caption,Version,BuildNumber,InstallDate,OSArchitecture,RegisteredUser,Organization from Win32_OperatingSystem'

        #initialize reference variables

        New-Variable -Name ci

        New-Variable -Name ce

    } #begin

    Process {

        foreach ($cs in $CimSession) {

            #capture connection failures to a variable

            if ($cs.TestConnection([ref]$ci, [ref]$ce)) {

                Write-Verbose "Querying $($cs.ComputerName.ToUpper())"

                $data = $cs.QueryInstances('Root/Cimv2', 'WQL', $query)

                [PSCustomObject]@{

                    PSTypeName             = 'OSDetail'

                    Name                   = $data.Caption

                    Version                = $data.Version

                    Build                  = $data.BuildNumber

                    OSArchitecture         = $data.OSArchitecture

                    RegisteredUser         = $data.RegisteredUser

                    RegisteredOrganization = $data.Organization

                    InstallDate            = $data.InstallDate

                    ComputerName           = $data.CSName

                }

            }

            else {

                Write-Warning "Unable to connect to $($cs.ComputerName.ToUpper()). $($ce.Message)"

            }

        } #foreach

    } #process

    End {

        Write-Verbose "Ending $($MyInvocation.MyCommand)"

    } #end

} #end function Get-OSDetail
I'll be using the chat feature in VS Code to build my Pester tests using GitHub Copilot. You may have other AI services that will work equally well, although you might get slightly different results. If you are using something else, I'd love to compare your responses with mine. Let's dig in.
Get a premium subscription for full article and archive access

Pester Testing .NET with Copilot

Over the last few weeks, I've been showing how to build PowerShell tools that rely on the .NET Framework instead of calling native PowerShell commands like Get-CimInstance. While you can see performance gains with this approach, there are potential drawbacks. First, your code could be a little more difficult to maintain by others. Native commands are at least easy to understand as long as you use full cmdlet and parameter names. Using the .NET Framework can make your code more arcane, especially for someone with less .NET experience than you. Internal documentation is critical if you take the approach I have been demonstrating.

Another area where you can encounter challenges is with Pester tests. You can only write tests for functions and cmdlets. And within those tests, you can only mock a command. You can't mock invoking a .NET method. I thought it would be helpful to write a Pester test for one of my recent functions. Then I realized I could also demonstrate how you might use GitHub Copilot in VS Code to help you write such a test.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
This email brought to you by Buttondown, the easiest way to start and grow your newsletter.