Powershell 多作业脚本。在作业完成时打开文件,而不是等到所有作业完成
Powershell Multijob script. Make file open when job completes instead of waiting until all jobs complete
我正在编写一个脚本来 ping 多个站点,然后打开文件以显示结果。我希望结果在完成时打开。相反,它会等到所有作业完成后再打开文件。我也遇到过它只会打开部分文件的问题。任何帮助将不胜感激
$count = 500
$sites = "www.google.com","8.8.8.8","127.0.0.1"
foreach ($site in $sites)
{
Remove-Item "C:\WFSupport\Self Service Tool$site.txt"
start-job -Name $site -ScriptBlock { param ($count,$site) ping -n $count $site } -ArgumentList $count, $site
}
While ((Get-Job).State -match 'Running')
{
foreach ($Job in Get-Job | where {$_.HasMoreData})
{
$Jobname = $Job.Name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
}
Start-Sleep -Seconds 10
}
While ((Get-Job).State -match 'Completed')
{
foreach ($Job in Get-Job | where {$_.HasMoreData})
{
$Jobname = $Job.Name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
Invoke-Item "C:\WFSupport\Self Service Tool$Jobname.txt"
}
Get-Job | Remove-Job
}
这是因为 'Running' 的 While 循环检查直到所有作业都停止 运行ning 才会停止。 None 下面的代码将 运行 直到 while 循环结束。
while ((Get-Job).State -match 'Running') {
foreach ($job in Get-Job | where {$_.HasMoreData}) {
$jobname = $job.name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
if ($job.State -like 'Completed'){
Invoke-Item "C:\WFSupport\Self Service Tool$Jobname.txt"
$job | remove-job
}
}
start-sleep -seconds 10
}
我正在编写一个脚本来 ping 多个站点,然后打开文件以显示结果。我希望结果在完成时打开。相反,它会等到所有作业完成后再打开文件。我也遇到过它只会打开部分文件的问题。任何帮助将不胜感激
$count = 500
$sites = "www.google.com","8.8.8.8","127.0.0.1"
foreach ($site in $sites)
{
Remove-Item "C:\WFSupport\Self Service Tool$site.txt"
start-job -Name $site -ScriptBlock { param ($count,$site) ping -n $count $site } -ArgumentList $count, $site
}
While ((Get-Job).State -match 'Running')
{
foreach ($Job in Get-Job | where {$_.HasMoreData})
{
$Jobname = $Job.Name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
}
Start-Sleep -Seconds 10
}
While ((Get-Job).State -match 'Completed')
{
foreach ($Job in Get-Job | where {$_.HasMoreData})
{
$Jobname = $Job.Name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
Invoke-Item "C:\WFSupport\Self Service Tool$Jobname.txt"
}
Get-Job | Remove-Job
}
这是因为 'Running' 的 While 循环检查直到所有作业都停止 运行ning 才会停止。 None 下面的代码将 运行 直到 while 循环结束。
while ((Get-Job).State -match 'Running') {
foreach ($job in Get-Job | where {$_.HasMoreData}) {
$jobname = $job.name
Receive-Job $Job | Out-File -Encoding ascii -Append "C:\WFSupport\Self Service Tool$Jobname.txt"
if ($job.State -like 'Completed'){
Invoke-Item "C:\WFSupport\Self Service Tool$Jobname.txt"
$job | remove-job
}
}
start-sleep -seconds 10
}