简体   繁体   English

在Powershell中读取CSV文件以从Active Directory获取信息

[英]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. 我可以通过手动键入命令来完成大多数步骤,但无法弄清楚如何使用整个列表自动完成所有步骤。

. 我的CSV看起来像这样 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.

相关问题 如何使用Powershell从.Csv文件中获取一行信息? - How to get one row of info from .Csv file with Powershell? 导入CSV的Powershell脚本,将信息与Active Directory比较,按OU排序,然后将信息导出到csv - Powershell script that imports a CSV, compares the info to Active Directory, sorts it by OU, then exports the Info to csv 如何使用 PowerShell 从 Active Directory 合并多行信息属性 - How to merge multiline info property from Active Directory using PowerShell 通过PowerShell脚本查询Active Directory以在csv文件中获取用户的属性 - Query Active Directory via PowerShell script to get attributes for users in a csv file 如何使用PowerShell将用户从CSV文件添加到Active Directory(AD)和Exchange中? - How do you add users from a CSV file to Active Directory (AD) and Exchange using PowerShell? 如何使用Powershell从csv文件更新Active Directory中的更多数据行? - How can I update more data rows in Active Directory from a csv file using Powershell? Powershell导出包含活动目录安全组数据的csv文件 - Powershell exports csv file containing active directory security group data 使用 PowerShell 从.csv 更新 Active Directory 管理器属性 - Updating Active Directory Manager Attribute from .csv Using PowerShell 使用来自csv的powershell删除活动目录samaccount名称中的空白区域 - remove white space in active directory samaccount name with powershell from csv 如何在 PowerShell 中从 csv 查找 Active Directory 组名称? - How to look for Active Directory group name from csv in PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM