最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - I'm catching errors, why does output look like it isn't - Stack Overflow

programmeradmin4浏览0评论

Here's my PowerShell 5.1 script to get service status on all machines. I'm giving each server 5 seconds to respond before moving on. However, it still looks like I'm getting the uncaught messages in addition to the friendly message I gave it. Any thoughts?

$results = @()

foreach ($server in $servers) {
    try {
        $job = Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock {
            Get-Service -Name Bits
        } -AsJob

        if (Wait-Job -Job $job -Timeout $timeout) {
            $result = Receive-Job -Job $job
            $results += $result
        } else {
            Write-Output "Timeout: $server did not respond in $timeout seconds."
        }
    } catch {
        Write-Output "Error: $_"
    } finally {
        Remove-Job -Job $job
    }
}

$results

Here's my PowerShell 5.1 script to get service status on all machines. I'm giving each server 5 seconds to respond before moving on. However, it still looks like I'm getting the uncaught messages in addition to the friendly message I gave it. Any thoughts?

$results = @()

foreach ($server in $servers) {
    try {
        $job = Invoke-Command -ComputerName $server -Credential $Credential -ScriptBlock {
            Get-Service -Name Bits
        } -AsJob

        if (Wait-Job -Job $job -Timeout $timeout) {
            $result = Receive-Job -Job $job
            $results += $result
        } else {
            Write-Output "Timeout: $server did not respond in $timeout seconds."
        }
    } catch {
        Write-Output "Error: $_"
    } finally {
        Remove-Job -Job $job
    }
}

$results

Share Improve this question edited Mar 19 at 16:10 mklement0 442k68 gold badges710 silver badges926 bronze badges asked Mar 19 at 15:33 RodRod 15.5k35 gold badges134 silver badges264 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Your un-caught exception is coming from the finally block where you try to remove the job. This happens regardless of the success in your try block.

发布评论

评论列表(0)

  1. 暂无评论