2016-05-13 106 views
0

我已經導入了一個.csv文件,並且在表單中的組合框中列出了第一列。我試圖將組合框中選定的數據與相應的行進行匹配。例PowerShell中的CSV問題

辦公室,服務器 芝加哥,chicago1 紐約,newyork1 洛杉磯,LA1

當他們選擇$辦公室,ID喜歡創造的下一個對象$服務器,並引用它在其他地方。

$Offices = @(Import-CSV "C:\source\PrinterTable.csv") 
$Array = $Offices.office | Sort-Object 

ForEach ($Choice in $Array) { 

    [void] $objListBox.Items.Add($Choice) 

} 

    $handler_Office_Click= 
{ 

    $officeSelected = $objListBox.SelectedItem 
    $row = $officeSelected | where { $_.office -eq $officeSelected } 
    $server = $row.server 
explorer.exe \\$server 


} 

我一直在Google上搜尋幾個小時...請幫助!

+0

爲了得到這個在組合框和作爲選擇項$顯示office = $ objlistbox.selecteditem必須轉到$ handle_office_click函數。現在即時嘗試同樣的事情,但告訴它調用$服務器...仍然需要幫助。 –

回答

0

所以我想通了修復,我不得不把我的CSV回到我的$處理一下,最後的代碼是

$handler_Office_Click= 
{ 

     $officeSelected = $objListBox.SelectedItem 
    $OffServer= ($PrinterTables | where {$_.office -eq $officeSelected}).server 


explorer.exe \\$OffServer 


} 

$PrinterTables = @(Import-CSV "C:\Program Files (x86)\Helpdesk 2.0\PrinterTables.csv") 
$ListedOffices = $PrinterTables.office | Sort-Object 



ForEach ($Choice in $ListedOffices) { 

    [void] $objListBox.Items.Add($Choice) 


} 
0

如果您有選定的辦公室名稱,請在$offices中找到與office匹配的字段。然後從該行中選擇server字段。

$row = $offices | where { $_.office -eq $office } 
$server = $row.server 
+0

也許我這樣做不對......我的列表wasnt如果出現我離開它的$ handler_office_click外面,所以我這樣做: $ handler_Office_Click = { $ officeSelected = $ objListBox.SelectedItem $行= $ officeSelected |其中{$ _。辦公室-eq $ officeSelected} $服務器= $ row.server explorer.exe的\\ $服務器 } –

0

運行一個foreach循環從CSV中讀取每一行,直到你找到$office你想要的。

Foreach ($line in $offices) { 
     If ($line.office -eq $office) { 
       $server = $line.server 
     } 
} 
+0

我不能得到這個工作,並在下拉列表中顯示。 –