4 Responses

  1. andrewjpalmer
    andrewjpalmer at |

    The issue I am seeing is if the fix also fails the test is reported as Passed.

    Describe “Test” {

    Context ‘System Services’ {

    It “Bits should be Running” {
    $Value = Get-Service bits
    Try {
    $Value.Status | Should Be Running
    }
    Catch {
    Write-Warning “Fixing bits not running”
    Start-Service bits
    }
    }

    }
    }

    If I run the test from a non-elevated PowerShell Window it obviously cannot start the service so fails. The test shows it Passed through.

    Describing Test
    Context System Services
    WARNING: Fixing bits not running
    Start-Service : Service ‘Background Intelligent Transfer Service (bits)’ cannot be started due to the following error:
    Cannot open bits service on computer ‘.’.
    At C:\Users\user\Desktop\Untitled1.ps1:12 char:17
    + Start-Service bits
    + ~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
    ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

    [+] Bits should be Running 948ms
    Tests completed in 948ms
    Passed: 1 Failed: 0 Skipped: 0 Pending: 0 Inconclusive: 0

    Reply
    1. Chris
      Chris at |

      Hmm. Good point. I had brazenly assumed the task would be run with privileges. I’d propose either wrapping the fix segment in another try/catch or iterating through the test one additional time to see if it was remediated.

      Reply
    2. Chris
      Chris at |

      Actually, it looks like the easy solution is to supply an -ErrorAction of Stop. It will fail the test for you.

      Reply
      1. andrewjpalmer
        andrewjpalmer at |

        Yes, that works. Thanks.

Share your point of view!