2012-03-11 62 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 
相關問題