Powershell foreach循环导出到csv中的新列 [英] Powershell foreach loop export to new column in csv
问题描述
我目前正在使用一个CSV文件,该文件具有经理的员工编号,但不是其SAMAccountName。我想要做的是利用 Get-ADUser
从他们的EmployeeNumber属性获取经理的SAMAccountName,并将它放在同一个CSV文件的新列中。
CSV示例:
全名,用户名 Manager User Sys ID
User 1,u1,1,Manager,123456
User 2,u2,2,Manager,234567
User 3,u3,3,Manager,345678
我想要的是:
全名,用户名,经理,经理用户系统ID 经理SamAccountName
用户1,u1,1,经理,123456,m1
用户2,u2 234567,m2
User 3,u3,3,Manager,345678,m3
我花了一些时间把以下代码放在一起。我可以得到一个新的列添加,并可以进一步抓住SAMAccountName,但它只导出一个新的CSV文件中的行,如下所示:
SAMAccountName,managerUsername
m1,@ {SAMAccountName = m1}
以下是代码:
$ managers = Import-Csv -Path .\test.csv
$ usermananger = @()
foreach($ manager中的$ manager)
{
$ getmanagersam = Get-ADUser -FilteremployeeNumber -eq $ ID) - 属性SAMAccountName |
选择对象SAMAccountName
$ exportstring = $ testmanager |
Select-Object *,@ {Name ='managerUsername'; Expression = {$ getmanagersam}}
$ exportstring | Export-Csv -Path .\simpletest.csv -NoTypeInformation
}
任何帮助这将非常感谢!
解决方案As @ MathiasR.Jessen在注释中提到:你需要扩展
SamAccountName
属性来获取值。此外,您每次迭代都覆盖输出CSV。添加到文件,或将Export-Csv
cmdlet移到循环外。前者需要PowerShell v3或更新版本,后者需要将循环改为ForEach-Object
循环(或运行foreach
loop in a subexpression)。
我个人更喜欢使用管道,所以我选择后者:
Import-Csv -Path .\test.csv | ForEach-Object {
$ acct = Get-ADUser -FilteremployeeNumber -eq $($ _。'Manager User Sys ID')|
select -Expand SamAccountName
$ _ | select *,@ {Name ='managerUsername'; Expression = {$ acct}}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation
I am currently working with a CSV file that has a manager's employee number but not their SAMAccountName. What I want to do is utilize
Get-ADUser
to grab the manager's SAMAccountName from their EmployeeNumber attribute and place that inside a new column in the same CSV file.CSV Sample:
"Full Name","Username","Manager","Manager User Sys ID" "User 1","u1","1, Manager","123456" "User 2","u2","2, Manager","234567" "User 3","u3","3, Manager","345678"
What I want is:
"Full Name","Username","Manager","Manager User Sys ID","Manager SamAccountName" "User 1","u1","1, Manager","123456","m1" "User 2","u2","2, Manager","234567","m2" "User 3","u3","3, Manager","345678","m3"
I have spent some time putting together the following code. I can get a new column added and can further grab the SAMAccountName, but it only exports a single line in the new CSV file like this:
"SAMAccountName","managerUsername" "m1","@{SAMAccountName=m1}"
Here is the code:
$managers = Import-Csv -Path .\test.csv $usermananger = @() foreach ($manager in $managers) { $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($manager."Manager User Sys ID")" -Properties SAMAccountName | Select-Object SAMAccountName $exportstring = $testmanager | Select-Object *,@{Name='managerUsername';Expression={"$getmanagersam"}} $exportstring | Export-Csv -Path .\simpletest.csv -NoTypeInformation }
Any help with this would be greatly appreciated!
解决方案As @MathiasR.Jessen mentioned in the comments: you need to expand the
SamAccountName
property to get just the value. Also, you're overwriting your output CSV with every iteration. Either append to the file, or move theExport-Csv
cmdlet outside the loop. The former requires PowerShell v3 or newer, the latter requires that you change the loop to aForEach-Object
loop (or run theforeach
loop in a subexpression).Personally I'd prefer using a pipeline, so I'd pick the latter:
Import-Csv -Path .\test.csv | ForEach-Object { $acct = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -Expand SamAccountName $_ | select *,@{Name='managerUsername';Expression={$acct}} } | Export-Csv -Path .\simpletest.csv -NoTypeInformation
这篇关于Powershell foreach循环导出到csv中的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!