【问题标题】:Returning output from scriptblock from Get-Job?从 Get-Job 返回脚本块的输出?
【发布时间】:2016-08-09 01:07:59
【问题描述】:

我正在尝试在 PowerShell 3 中测试异步功能。

所以我想我会远程查询一些服务器的正常运行时间,并编写了以下脚本:

function getServerUptimes() {
    # Obtain credentials for logging into 
    # the remote machines...
    $cred = Get-Credential

    $compsHash = @{
       "server1.domain.com" = $null;
       "server2.domain.com" = $null;
    }

    # Define an async block to run...no blocking in this script...
    $cmd = {
        param($computer, $dasCred)

        Write-Host "which: $($computer)"
        $session = new-cimsession $computer -Credential $dasCred

        return (get-CimInstance win32_operatingsystem -CimSession $session | Select PScomputername, LastBootuptime);
    }

    ForEach($comp in $compsHash.Keys) {
        Write-Host "${comp}"

        # Kick off an async process to create a cimSession 
        # on each of these machines...
        Start-Job -ScriptBlock $cmd -ArgumentList $comp, $cred -Name "Get$($comp)"  
    }

    $results = Get-Job | Wait-Job | Receive-Job

    # Retrieve the job, so we can look at the output
    ForEach($comp in $compHash.Keys) {
        $dasJob = Get-Job -Name "Get$($comp)"
        Write-Host $dasJob.output
    }

}

但是,我似乎并没有在生成的$dasJob 对象中恢复任何输出,我在脚本块中返回了值,它要去哪里?

【问题讨论】:

    标签: asynchronous powershell-3.0


    【解决方案1】:

    您已经在变量$results 中有输出(当然不包括Write-Host 输出)。在 PowerShell 中,您可以通过 Receive-Job 检索作业输出。 Output 的属性似乎总是​​空的(不知道为什么)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多