start-job 无法获得结果


PowerShell交流中心分类: Powershell基础start-job 无法获得结果
0
howcare asked 2年 ago

各位大神好,本人小白,刚接触powershell不久,最近遇到start-job无法获得运行结果的问题迷茫不已,麻烦各位大神帮忙解答,谢谢~~
版本信息: $PSVersionTable
Name Value
—- —–
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
脚本内容:

start-job -name job1 -ScriptBlock {if (Test-Connection -ComputerName http://www.baidu.com -Quiet){ echo “Website is ok”;echo “Website is ok”>>.\record.txt} else {echo “Website is down”;echo “Website is down” >>.\record.txt}}

 
单独使用if判断可以正确显示和记录结果,但放到start-job里就无法获得和记录结果.实在不晓得哪里犯了错.麻烦各位大神指教~

×用微信扫描并分享
1 Answers
1
Best Answer
Mooser Lee 管理员 answered 2年 ago
PS> 1..5 | foreach {Start-Job -ScriptBlock { Test-Connection -ComputerName www.pstips.net } }

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Job1            BackgroundJob   Completed     True            localhost             Test-Connection -Comp...
3      Job3            BackgroundJob   Completed     True            localhost             Test-Connection -Comp...
5      Job5            BackgroundJob   Completed     True            localhost             Test-Connection -Comp...
7      Job7            BackgroundJob   Completed     True            localhost             Test-Connection -Comp...
9      Job9            BackgroundJob   Completed     True            localhost             Test-Connection -Comp...


PS> Get-Job | Wait-Job | Receive-Job

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       125
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       125
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       123
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
My-Host www.pstips.net  116.62.63.90                                              32       124
howcare replied 2年 ago

感谢大神指点~