gpt4 book ai didi

powershell - Powershell启 Action 业脚本 block 中的Powershell V3变量未扩展

转载 作者:行者123 更新时间:2023-12-03 01:18:27 26 4
gpt4 key购买 nike

我在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]

如您所见,start-job脚本块中的$ 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

有人可以帮我搜索一个多小时吗?谢谢。

最佳答案

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 *

关于powershell - Powershell启 Action 业脚本 block 中的Powershell V3变量未扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com