[英]Reading CSV file in powershell to get info from Active directory
I have a new question for you. 我有一个新问题要问你。 So I just got a list with employee names and IDs, I am supposed to write a script that can look up the employee and search for their department info in active directory and then add it to the CSV file. 因此,我只得到了一个包含员工姓名和ID的列表,我应该编写一个脚本来查找员工并在活动目录中搜索他们的部门信息,然后将其添加到CSV文件中。
I thought that I can just look at all the employee numbers, pad them to 5, look for that number in AD, use that to look up the department and add that to the CSV. 我以为我可以查看所有员工编号,将其填充为5,在AD中查找该编号,使用它来查找部门并将其添加到CSV中。 I can get most steps done by typing the commands manually but cant figure out how to make it all work automatically using the whole list. 我可以通过手动键入命令来完成大多数步骤,但无法弄清楚如何使用整个列表自动完成所有步骤。
. 。 I would like to add the retrieved department info in the next line over. 我想在下一行中添加检索到的部门信息。 My code so far looks like this. 到目前为止,我的代码看起来像这样。
Set-ExecutionPolicy Unrestricted $csvlines = import-csv .\\test.csv foreach($line in $csvlines) { $ID=$line.EmployeeID; $padding = "$ID".PadLeft(5,"0") $filter = "ID -like '$padding'" $getdepartment = Get-ADUser -Filter $filter -Properties Department | Select-Object -ExpandProperty Department Write-Host “ID=” $ID, $getdepartment; }
I am messing up somewhere, could you guys please help me out. 我在某个地方搞砸了,请您帮我一下。 Thank you. 谢谢。
Just in case anybody needs to do something similar, I just figured out how to solve my problem. 万一有人需要做类似的事情,我只是想出了解决问题的方法。 I will share the code with you guys. 我将与你们分享代码。
$csvlines = import-csv .\\list.csv foreach($line in $csvlines) { $first = $line.FirstName $last = $line.LastName $ID = $line.EmployeeID; $padding = "$ID".PadLeft(5,"0") $filter = "EmployeeID -like '$padding'" Get-ADUser -Filter $filter -Properties UserPrincipalName, Department, Title, EmployeeID | Select-Object GivenName, Surname, EmployeeID, Department, Title | export-csv -NoTypeInformation -Append -Path .\\new.csv" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.