Windows 从启动作业调用获取输出

Windows 从启动作业调用获取输出,windows,multithreading,powershell,start-job,Windows,Multithreading,Powershell,Start Job,我有以下代码,它可以创建多个作业,并在阵列中的所有计算机上运行脚本块中的内容($SMSMembers)。问题是它没有返回任何有意义的输出,我可以用它来确定代码是否成功运行。我用谷歌搜索了大约100种不同的方法,但没有一种方法对我有效。根据我在StackOverflow上看到的其他帖子,我认为这是我试图运行的代码 $SMSMembers = @("computer1","computer2","computer3") $output = @() foreach ($compName in $SMS

我有以下代码,它可以创建多个作业,并在阵列中的所有计算机上运行脚本块中的内容(
$SMSMembers
)。问题是它没有返回任何有意义的输出,我可以用它来确定代码是否成功运行。我用谷歌搜索了大约100种不同的方法,但没有一种方法对我有效。根据我在StackOverflow上看到的其他帖子,我认为这是我试图运行的代码

$SMSMembers = @("computer1","computer2","computer3")
$output = @()
foreach ($compName in $SMSMembers) {
    $scriptblock = {
        $file = {"test"}
        $filepath = "\\$using:compName\c$\scripts\NEWFILE.txt"
        $file | Out-File -FilePath $filepath
        Start-Sleep -Seconds 5
        Remove-Item $filepath
    }
    $output += Start-Job -ScriptBlock $scriptblock | Get-Job | Receive-Job
}
Get-Job | Wait-Job
foreach ($item in $output) {
    Write-Host $item
}
这个脚本除了将文件复制到远程计算机,然后将其删除之外,没有什么作用。我只想得到输出,如果工作成功与否。就像我说的,这段代码工作正常,我只是没有得到反馈

我的最终目标是能够向一系列计算机发送命令(
$SMSMembers
),并使用此代码请求当前用户,并返回用户名输入:

$user = gwmi Win32_ComputerSystem -Comp $compName |
        select Username -ExpandProperty Username

请看下面的代码,我认为您应该能够理解这一点

>  Get-job | Receive-Job 2>&1 >> c:\output.log

2>&1>
具有从
Get Job | Receive Job
收集所有输出的功能,对于
start Job

它应该同样有效。请看下面的代码,我想您应该能够解决这个问题

>  Get-job | Receive-Job 2>&1 >> c:\output.log

2>&1>
具有从
Get Job | Receive Job
收集所有输出功能的
start Job
在创建作业、获取作业信息,然后在作业完成之前背靠背地接收作业时,它应该同样适用于
start Job
。相反,收集作业信息,然后在循环外等待作业完成,并在作业完成时接收输出

$SMSMembers = @("computer1","computer2","computer3")
$scriptblock = {
    $file = {"test"}
    $filepath = "\\$using:compName\c$\scripts\NEWFILE.txt"
    $file | out-file -FilePath $filepath
    Start-Sleep -Seconds 5
    remove-item $filepath
}
$Jobs = foreach($compName in $SMSMembers){
    Start-Job -ScriptBlock $scriptblock
}
Wait-Job $Jobs
$Output = Receive-Job $Jobs
foreach ($item in $output){
    write-host $item
}
编辑:稍微修改了脚本,这样我就不会随意复制文件,但它的功能应该还是一样的。然后用预期结果对其进行测试:

$SMSMembers = @("computer1","computer2","computer3")
$scriptblock = {
    $RndDly=Get-Random -Minimum 10 -Maximum 45
    start-sleep -Seconds $RndDly
    "Slept $RndDly, then completed for $using:compname"
    }
$Jobs = foreach($compName in $SMSMembers){
    Start-Job -ScriptBlock $scriptblock
}
Wait-Job $Jobs
$Output = Receive-Job $Jobs
foreach ($item in $output){
    write-host $item
}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      Job1            BackgroundJob   Completed     True            localhost            ...                      
3      Job3            BackgroundJob   Completed     True            localhost            ...                      
5      Job5            BackgroundJob   Completed     True            localhost            ...                      
Slept 30, then completed for computer1
Slept 27, then completed for computer2
Slept 11, then completed for computer3

创建作业,获取作业信息,然后在作业完成之前背靠背接收作业。相反,收集作业信息,然后在循环外等待作业完成,并在作业完成时接收输出

$SMSMembers = @("computer1","computer2","computer3")
$scriptblock = {
    $file = {"test"}
    $filepath = "\\$using:compName\c$\scripts\NEWFILE.txt"
    $file | out-file -FilePath $filepath
    Start-Sleep -Seconds 5
    remove-item $filepath
}
$Jobs = foreach($compName in $SMSMembers){
    Start-Job -ScriptBlock $scriptblock
}
Wait-Job $Jobs
$Output = Receive-Job $Jobs
foreach ($item in $output){
    write-host $item
}
编辑:稍微修改了脚本,这样我就不会随意复制文件,但它的功能应该还是一样的。然后用预期结果对其进行测试:

$SMSMembers = @("computer1","computer2","computer3")
$scriptblock = {
    $RndDly=Get-Random -Minimum 10 -Maximum 45
    start-sleep -Seconds $RndDly
    "Slept $RndDly, then completed for $using:compname"
    }
$Jobs = foreach($compName in $SMSMembers){
    Start-Job -ScriptBlock $scriptblock
}
Wait-Job $Jobs
$Output = Receive-Job $Jobs
foreach ($item in $output){
    write-host $item
}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
1      Job1            BackgroundJob   Completed     True            localhost            ...                      
3      Job3            BackgroundJob   Completed     True            localhost            ...                      
5      Job5            BackgroundJob   Completed     True            localhost            ...                      
Slept 30, then completed for computer1
Slept 27, then completed for computer2
Slept 11, then completed for computer3

我试着运行这段代码,我更喜欢这个解决方案,在开始作业时附加一个变量。唯一的问题是,当输出数组在foreach中运行时,它没有给我任何输出。据我所知,数组是空的。我可以用你的解决方案使它工作。我发现,当您完成scriptblock时,必须将作业的结果作为字符串传回。我试着用一个变量结束作业scriptblock,就像一些建议一样,但它不起作用,它特别需要一个字符串,就像你的一样。我试着运行这段代码,我更喜欢这个解决方案,在开始作业时附加一个变量。唯一的问题是,当输出数组在foreach中运行时,它没有给我任何输出。据我所知,数组是空的。我可以用你的解决方案使它工作。我发现,当您完成scriptblock时,必须将作业的结果作为字符串传回。我试着用一个变量来结束作业scriptblock,比如一些建议,但它不起作用,它特别需要一个字符串作为结尾,就像你的一样。