Pester in practice

The following function sets a computer description by modifying values in the registry:

function Set-ComputerDescription { 
    [CmdletBinding()] 
    param ( 
        [Parameter(Mandatory = $true)] 
        [AllowEmptyString()] 
        [String]$Description 
    ) 
 
    $erroractionpreference = 'Stop' 
 
    try { 
        $path = 'HKLM:\System\CurrentControlSet\Services\LanmanServer\Parameters' 
 
        if ((Get-Item $path).GetValue('srvcomment') -ne $Description) { 
            if ($Description) { 
                Set-ItemProperty $path -Name 'srvcomment' -Value $Description 
            } else { 
                Remove-ItemProperty $path -Name 'srvcomment' 
            } 
        } 
    } catch { 
        throw 
    } 
} 

When the function interacts with the registry, it does so using the following commands:

  • Get-Item
  • Set-ItemProperty
  • Remove-ItemProperty

Testing the actions undertaken by ...

Get Mastering Windows PowerShell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.