2012-03-11 63 views
0
$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 

我不能做$脚本文件内$ computer.name。我必须像$ computer.name那样传递它,并像$ args [0]那样调用它。但后来我失去了所有其他属性(我在$ scriptfile中使用了一堆)。

我不在这里?你会叫什么电脑?你会打电话给$ computer.name?

淑娜:)

+0

为什么我的问题被降低了吗? – Sune 2012-03-12 05:31:28

回答

5

你应该能够的$ args [0]请将.Name得到Name属性。如果您要访问的名称参数,如下所示:$ computer.name,那么你需要在$脚本文件来定义参数:

​​3210

顺便说”你不能这样做:

echo "and then I can do this $computer.name" 

PowerShell仅扩展$ computer的值。把它放在一个子表达式:

echo "and then I can do this $($computer.name)" 
+0

非常感谢!完美的作品! – Sune 2012-03-11 23:08:39

0

正是这样,这是我会怎么写类似的东西:

#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 
相关问题