function Invoke-TopshelfPath { <# .SYNOPSIS Used to call a topshelf path and look for errors. This function is basically just a wrapper for unit-testing purposes. .PARAMETER Path The file path being invoked .PARAMETER Arguments This value just gets splatted as passed in. #> [CmdletBinding()] [OutputType([System.Object])] param( [Parameter(Mandatory=$true)] [string]$Path, [Parameter(Mandatory=$true)] [string[]]$Arguments ) $logLead = Get-LogLeadName try { $output = @(Invoke-CallOperatorWithPathAndParameters $Path @($Arguments)) # This doesn't usually return an exit code for Topshelf $didFailByExitCode = ($LASTEXITCODE -ne 0) $errorMessage = "Service failed by return of exit code other than 0" $didFailByErrorText = $false # Checking the first line of output for the text Error. if ($output[0] -match "Error") { $errorMessage = $output[1] $didFailByErrorText = $true } # look for other exceptions. This won't catch everything, but should get a lot. foreach ($line in $output) { if ($line -match "Win32Exception") { $errorMessage = $line.Split(':', 2) $didFailByErrorText = $true } } Write-Verbose ($output -join '`r`n') if ($didFailByExitCode -or $didFailByErrorText) { Write-Warning "$logLead : [$Path] failed on run. Output printed to verbose stream above." throw "[$Path] failed to execute.`r`n$errorMessage" } } finally { # Keeping a `finally` because you can't have a try without either `catch` or `finally` Write-Verbose "$logLead : done" } }