我遇到了PowerShell脚本的问题。我试图使用start-job对计算机列表运行一堆测试连接。
我已经将脚本修剪为基础,就是这样,
$cnamesAll=@("localhost","dc01","ex01","dd01")
$cnamesAll | ForEach-Object { start-job { Test-Connection $args[0]} -ArgumentList "$_"}
Get-Job | % { $_.Command }
Get-Job | Wait-Job
Remove-Job *
这是我跑步时得到的输出:
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2916 Job2916 BackgroundJob Running True localhost Test-Connection $args[0]
2918 Job2918 BackgroundJob Running True localhost Test-Connection $args[0]
2920 Job2920 BackgroundJob Running True localhost Test-Connection $args[0]
2922 Job2922 BackgroundJob Running True localhost Test-Connection $args[0]
Test-Connection $args[0]
Test-Connection $args[0]
Test-Connection $args[0]
Test-Connection $args[0]
2916 Job2916 BackgroundJob Completed True localhost Test-Connection $args[0]
2918 Job2918 BackgroundJob Completed True localhost Test-Connection $args[0]
2920 Job2920 BackgroundJob Completed True localhost Test-Connection $args[0]
2922 Job2922 BackgroundJob Completed True localhost Test-Connection $args[0]
正如您所看到的,启动作业脚本块中的$ args [0]未被扩展。
我在Server 2012上运行它并输出$ PSVersionTable
S C:\Users\administrator\Documents> $PSVersionTable
Name Value
---- -----
PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18408
BuildVersion 6.2.9200.16628
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2
任何人都可以帮助我一直在搜索超过一个小时。感谢。
答案 0 :(得分:3)
Get-Job | % { $_.Command }
显示您在作业中运行的命令。脚本块中的命令是Test-Connection $args[0]
,这就是Command
属性返回的内容。这个参数很好地传递给了这个工作,如果从工作中检索输出,你会看到它:
Get-Job | Wait-Job | Receive-Job
演示:
PS C:\> 'localhost' | % {Start-Job {Test-Connection $args[0]} -ArgumentList $_}
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
30 Job30 BackgroundJob Running True localhost Test-Connection $args...
PS C:\> Get-Job | % { $_.Command }
Test-Connection $args[0]
PS C:\> Get-Job | Wait-Job | Receive-Job
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
VM01 localhost 127.0.0.1 ::1 32 0
VM01 localhost 127.0.0.1 ::1 32 0
VM01 localhost 127.0.0.1 ::1 32 0
VM01 localhost 127.0.0.1 ::1 32 0
PS C:\> Remove-Job *