传递变量与Artheast to ArgumentList,丢失属性
-
11-12-2019 - |
题
$Computers = Get-QADComputer -sizelimit 5
.
返回五台计算机的列表。我用循环
foreach($computer in $computers) {
echo "and then I can do this $computer.name"
.
只能从$计算机中获取ComputerName。 但是当我尝试将它传递到开始工作时:
Start-Job -FilePath $ScriptFile -Name $Computer.Name -ArgumentList $Computer
.
我无法在$ scriptfile中做一个$ computer.name。我必须像$ computer.name一样传递它并称之为$ args [0]。但是,我松开了所有其他属性(我正在使用$ scriptfile中的一堆。)
我不是在这里到达的?你会称之为$电脑?你会称之为$ computer .name?
SUE :)
解决方案
您应该能够使用$ args [0] .name获取名称属性.Name。如果要访问如此:$ computer.name等名称参数,则需要在$ scriptfile中定义一个参数:
param(
$Computer
)
$Computer.name
.
顺便说一下'你不能这样做:
echo "and then I can do this $computer.name"
.
PowerShell仅扩展值$计算机。将其放入子表达式:
echo "and then I can do this $($computer.name)"
. 其他提示
所以,这就是我如何写类似的东西:
#Get Services
$Services = Get-Service
#Limit results to the first 5 entries
$Services = $Services[0..4]
#Loop through services
foreach ($Service in $Services)
{
#For each Service run a basic echo to host to prove that the Service was passed in successfully
Start-Job -ScriptBlock { param ($Service) Write-Host "Service Name is $($Service.Name)" } -ArgumentList $Service
}
.
然后您可以检索这样的作业:
#Retrieve Jobs
Get-Job | Receive-Job
. 不隶属于 StackOverflow