Project maintained by timhaintz Hosted on GitHub Pages — Theme by mattgraham

The Power of Community

Introduction

I started blogging in April 2018 to document things I was interested in and to share knowledge. I have been using PowerShell since 2009 and started to give back to the community via Stack Exchange & Microsoft TechNet in late 2016. Helping via the forums led me to writing this blog.

For the past 10 months, I have been writing and publishing posts, not realeasing them publicly. On the 6th of February this year, I decided to use my newly created Twitter account to announce my new blog post. What a great experience that was! Within an hour, I had feedback not just from the PowerShell community, but from the exact people that have unknowingly helped me out over the years from their own blogs, websites and articles. SQLDBAwithbeard & Mike F Robbins both provided valuable feedback to my original Pester script.

The below scripts show the value of community and sharing. My original script is shown first. The SQLDBAwithbeard and Mike F Robbins suggestions are shown after.

Script

Original Post - Pester Code Block

# Services we want to test for
$services = 'bthserv','WinRM'
# Loop through the services
Describe "Testing critical services on ca1" {
    $services.ForEach{
        Context "Testing $($_)" {
            It "The $($_) service should be running" {
                (Invoke-Command -ComputerName ca1 -ScriptBlock {param($_) Get-Service -ServiceName $_} -ArgumentList $_ ).status | Should be 'Running'
            }
        }
    }
}

Original Results

When a service is off, the below failure is displayed

Pester Multiple Services - Remote Fail

When the services are all running, the below success is displayed

Pester Multiple Services - Remote Pass

SQLDBAwithbeard suggestion - Pester Code Block

Please note, the below syntax Should -Be requires Pester v4.

# Services we want to test for
$services = Get-Service 'bthserv','WinRM' -Computername ca1
# Loop through the services
Describe "Testing critical services on ca1" {
    $services.ForEach{
    It " $($PsItem.Name) should be running" {
        $Psitem.Status | Should -Be 'Running' -Because 'these are critical services on this server.'
        }
    }
}

SQLDBAwithBeard Results

When a service is off, the below failure is displayed

Pester Multiple Services - SQLDBAwithBeard - Remote Fail

When the services are all running, the below success is displayed

Pester Multiple Services - SQLDBAwithBeard - Remote Pass

Mike F Robbins suggestion - Pester Code Block

Describe 'Testing critical services on ca1' {
    $services = @{service = 'bthserv'}, @{service = 'WinRM'}
    It "The <service> Service on ca1 Should Be Running" -TestCases $services {
        param($service)
        (Get-Service -ComputerName ca1 -Name $service).Status |
        Should Be 'Running'
    }
}

Mike F Robbins Results

When a service is off, the below failure is displayed

Pester Multiple Services - Mike F Robbins - Remote Fail

When the services are all running, the below success is displayed

Pester Multiple Services - Mike F Robbins - Remote Pass

Conclusion

As you can see from the results of the tests, my original method of testing was very inefficient. SQLDBAwithBeard’s method shows to be the most efficient with Mike F Robbins’ method of using -TestCases being more Pester-Purist in the words of Matt Hitchcock.

What has releasing my blog to the community shown me? The PowerShell and Pester community are awesome and very helpful. There are also multiple ways to get to the same outcome and it is great to learn new methods.

Thank you to everyone in the PowerShell community. I hope to keep sharing and any feedback I receive, I will try and pass on.

Keep learning and practicing Kaizen.

Hope you’re having a great day and this is of use.

Thanks, Tim.