从 CSV 加载两个数组
Loading two arrays from a CSV
我有一个包含两列 Employee 和 Manager 的 CSV 文件,我想将它们导入两个变量,以便稍后在我的脚本中使用。但是,当我 运行 我的代码时,它只捕获 CSV 中的最后一个数据项,因为前一个数据项正在被覆盖。
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$Employee = ($CSVFile.Employee); $Manager = ($CSVFile.Manager)
}
那是因为每次循环运行时你都在覆盖添加的数据。在 PowerShell 中,+=
附加到一个对象。试试这个 -
$Employee = @()
$Manager = @()
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$Employee += $CSVFile.Employee; $Manager += $CSVFile.Manager
}
$Employee = $CSVFiles | Select -Expand Employee
$Manager = $CSVFiles | Select -Expand Manager
很好地解释了您的方法存在的问题并提供了解决方案。
这是一个更简单的替代方案 (PSv3+),假设您无论如何都要将整个 CSV 文件加载到(自定义对象)到内存中:
$CSV = Import-CSV "C:\T2\Employee.csv"
$Employees = $CSV.Employee # collect the Employee column values across all input rows
$Managers = $CSV.Manager # ditto for Manager
这种方法利用了 PSv3+ member-access enumeration feature。
在 PSv2 中,使用 。
比较解决方案的性能:
- 这个答案中的成员枚举解决方案是最快的,
- 接着是 iRon 的
Select -Expand
解决方案
- Vivek 的
foreach
循环是迄今为止最慢的,尤其是因为使用 +=
来(概念上)扩展数组需要 创建一个新实例 每次迭代的幕后。
这最终成为我实现最终目标的最终编码。我要感谢所有发帖者,我从你们所有人那里学到了一些东西,从而得出了我想要的解决方案。
$EmployeeLists = @()
$ManagerLists = @()
$Employees = @()
$Managers = @()
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$EmployeeLists += ($CSVFile.Employee)
}
ForEach($CSVFile in $CSVFiles)
{
$ManagerLists += ($CSVFile.Manager)
}
$Employees += ForEach ($EmployeeList in $EmployeeLists) { Get-ADUser -Properties * -Filter { DisplayName -like $EmployeeList } | Select SamAccountName -ExpandProperty SamAccountName }
$Managers += ForEach ($ManagerList in $ManagerLists) { Get-ADUser -Properties * -Filter { DisplayName -like $ManagerList } | Select SamAccountName -ExpandProperty SamAccountName }
$EmployeeLists
"`n`n`n"
$Employees
我有一个包含两列 Employee 和 Manager 的 CSV 文件,我想将它们导入两个变量,以便稍后在我的脚本中使用。但是,当我 运行 我的代码时,它只捕获 CSV 中的最后一个数据项,因为前一个数据项正在被覆盖。
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$Employee = ($CSVFile.Employee); $Manager = ($CSVFile.Manager)
}
那是因为每次循环运行时你都在覆盖添加的数据。在 PowerShell 中,+=
附加到一个对象。试试这个 -
$Employee = @()
$Manager = @()
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$Employee += $CSVFile.Employee; $Manager += $CSVFile.Manager
}
$Employee = $CSVFiles | Select -Expand Employee
$Manager = $CSVFiles | Select -Expand Manager
这是一个更简单的替代方案 (PSv3+),假设您无论如何都要将整个 CSV 文件加载到(自定义对象)到内存中:
$CSV = Import-CSV "C:\T2\Employee.csv"
$Employees = $CSV.Employee # collect the Employee column values across all input rows
$Managers = $CSV.Manager # ditto for Manager
这种方法利用了 PSv3+ member-access enumeration feature。
在 PSv2 中,使用
比较解决方案的性能:
- 这个答案中的成员枚举解决方案是最快的,
- 接着是 iRon 的
Select -Expand
解决方案 - Vivek 的
foreach
循环是迄今为止最慢的,尤其是因为使用+=
来(概念上)扩展数组需要 创建一个新实例 每次迭代的幕后。
这最终成为我实现最终目标的最终编码。我要感谢所有发帖者,我从你们所有人那里学到了一些东西,从而得出了我想要的解决方案。
$EmployeeLists = @()
$ManagerLists = @()
$Employees = @()
$Managers = @()
$CSVFiles = Import-CSV "C:\T2\EmployeeManager.csv"
ForEach($CSVFile in $CSVFiles)
{
$EmployeeLists += ($CSVFile.Employee)
}
ForEach($CSVFile in $CSVFiles)
{
$ManagerLists += ($CSVFile.Manager)
}
$Employees += ForEach ($EmployeeList in $EmployeeLists) { Get-ADUser -Properties * -Filter { DisplayName -like $EmployeeList } | Select SamAccountName -ExpandProperty SamAccountName }
$Managers += ForEach ($ManagerList in $ManagerLists) { Get-ADUser -Properties * -Filter { DisplayName -like $ManagerList } | Select SamAccountName -ExpandProperty SamAccountName }
$EmployeeLists
"`n`n`n"
$Employees