2012-04-25 23 views
1

我目前正在編寫一個使用get-service的腳本|選擇顯示名稱以顯示正在運行的內容。Powershell數組和刷新

但是,我遇到了麻煩。

我使用這個:

function Getcurrent-AD 
{ 
    $Script:current_active_d = get-service | select Displayname 
    $array = New-Object System.Collections.ArrayList 
    $array.AddRange($current_active_d) 
    $current_output.DataSource = $array 
    $objForm.refresh() 
} 

$moveright.Add_Click({ 

$selectedRow = $current_output.CurrentRowIndex 
    if (($procid=$Script:current_active_d["$selectedRow"].displayname)) { 
       write-host $procid 
     } 


} 
) 

創建一個數組,並把它放在一個數據網格的形式,然後寫入到主機在那裏我一下,但我可以只有讓它工作,才能在控制檯中顯示所選的第一個項目。

請幫忙!

回答

1

試試這個:

$current_output.Add_Click({ 
    $value = $current_output.Rows[$current_output.CurrentRow.Index].Cells['DisplayName'].Value 
    Write-Host $value 
}) 
+0

感謝,但我怕我得到這個錯誤 - 無法索引空數組。在C:\ Temp \ ADXv0.300.ps1:170 char:30 + $ value = $ moveright.Rows [<<<< $ moveright.CurrentRow.Index] .Cells ['Displa yName']。Value + CategoryInfo:InvalidOperation :(:) [],RuntimeException + FullyQualifiedErrorId:NullArray – obious 2012-04-25 10:36:42

+0

$ moveright是datagridview控件? – 2012-04-25 11:28:32

+0

這裏是我的表單:$ current_output = New-Object System.Windows.Forms.DataGridView - $ moveright = New-Object System.Windows.Forms.Button - 右移是我正在使用的按鈕的名稱 – obious 2012-04-25 11:40:07