Powershell开始作业脚本块中的Powershell V3变量未展开
我对powershell脚本有问题。我正在尝试使用start job对计算机列表运行一组测试连接 我已将脚本精简为基本内容就是这样Powershell开始作业脚本块中的Powershell V3变量未展开,powershell,Powershell,我对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 R
$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]未展开
我在服务器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
有人能帮忙吗?我已经找了一个多小时了。谢谢。
获取作业|%{$\u.Command}
显示作业中正在运行的命令。脚本块中的命令是testconnection$args[0]
,因此命令
属性将返回该命令。参数被很好地传递到作业中,如果从作业中检索到输出,您将看到:
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 *
PS C:\>'localhost'\%{Start Job{Test Connection$args[0]}
Id名称PSJobTypeName状态HasMoreData位置命令
-- ---- ------------- ----- ----------- -------- -------
30作业30后台作业正在运行真正的本地主机测试连接$args。。。
PS C:\>获取作业|%{$\.Command}
测试连接$args[0]
PS C:\>获取作业|等待作业|接收作业
源目标IPV4Address IPV6地址字节时间(毫秒)
------ ----------- ----------- ----------- ----- --------
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:\>删除作业*
它不应该展开$args[0]
,因为它是一个脚本块,而不是字符串$args
是由-ArgumentList
参数确定的数组。您可以使用Start Job{Get Process$args[0]}-ArgumentList“*ss”
轻松测试它。如果您在从该命令创建的作业上获得了命令
属性,您仍然会看到它使用了$args[0]
。如果您收到作业的结果,您将看到它正确地应用了预期的筛选器。您是对的,我不敢相信我花了这么多时间固定作业的命令属性,我以为它会被扩展!干杯