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 - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.