简体   繁体   English

如何基于列表框选择将CSV数据保存到变量?

[英]How do I save CSV data to variables based on a ListBox selection?

I have a drop down list in my script that gets populated by the "name" column in a CSV file. 我的脚本中有一个下拉列表,由CSV文件中的“名称”列填充。 I'd like to save the information in the other columns that go with that name into variables to be used later. 我想将信息与该名称一起保存在其他列中,以供以后使用。

For example (bullets are there so they show underneath each other, showed as one line otherwise): 例如(子弹在那里,所以它们彼此下方显示,否则显示为一行):

  • Name,Address,PhoneNumber,Email 姓名,地址,******中国,电子邮件
  • Bob,1234 Bob Rd,123-4567,bob@email.com Bob,1234 Bob Rd,123-4567,bob @ email.com
  • Bill,5678 Bill Ave,246-8024,bill@email.com Bill,5678 Bill Ave,246-8024,bill @ email.com

The drop down list would show Bob and Bill. 下拉列表将显示Bob和Bill。 Based on what I pick I'd like the rest of their info to be saved into 3 different variables, $Address, $PhoneNumber, and $Email. 根据我的选择,我希望将其其余信息保存到3个不同的变量中,分别为$ Address,$ PhoneNumber和$ Email。

I've got as far as importing the CSV into the array and sorting them alphabetically. 我已经将CSV导入数组并按字母顺序对其进行了排序。

$Customers = @(Import-CSV "$dir\Apps\Customers.csv")
$Array = $Customers.name | Sort-Object

Here's part of the drop down box code: 这是下拉框代码的一部分:

ForEach ($Choice in $Array) {

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

}

After that I can't figure out how to grab the correct information based on what's been chosen when I click a button. 之后,我无法根据单击按钮时选择的内容来弄清楚如何获取正确的信息。

Here's the button code: 这是按钮代码:

$handler_gobutton_Click = { $company = $companybox.SelectedItem
                            ....
                          }

I have a few If statements in there right now for an earlier solution that doesn't use CSV's, but I'd like to move to a CSV file if possible. 我现在有一些If语句,用于不使用CSV的较早解决方案,但如果可能的话,我想移至CSV文件。

Thanks! 谢谢!

EDIT: I'm thinking what I need to do is somehow find the array count number based on the name column. 编辑:我在想我需要做的是以某种方式找到基于名称列的数组计数数字。 After that I should be able to save $array[0].address into a variable, for example. 之后,我应该能够将$ array [0] .address保存到一个变量中。 Finding how to compare the variable with the name in it to the name in the array and from that getting the count number is coming up empty so far... Ideas? 寻找如何将具有名称的变量与数组中的名称进行比较,到目前为止,获得计数值是空的...想法?

Got it to work! 得到它的工作!

$handler_gobutton_Click = {
$company = $companybox.SelectedItem
$index = [array]::IndexOf($customers.name,$company)
If ($customers[$index].uninstall -eq "True") { $uninstallbox.checked = $True }
... and so on
}

Source: https://www.reddit.com/r/PowerShell/comments/t1928/finding_array_index_number/ 资料来源: https : //www.reddit.com/r/PowerShell/comments/t1928/finding_array_index_number/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM