[英]Return job titles from Get-ADUser that match csv
I want to import job titles from a csv file into Powershell then use the Get-ADUser
to return all active users with those titles and then export to a new csv 我想将职位名称从csv文件导入Powershell,然后使用
Get-ADUser
返回具有这些标题的所有活动用户,然后导出到新的csv
Can someone please help? 有人可以帮忙吗?
$csvjobtitle = Import-Csv -Header csvTitle -Path c:\Job_titles.csv
Get-ADUser -Filter {enabled -eq "true" -and Title -eq "$csvjobtitle"} -
Properties SamAccountName, GivenName, Surname, EmailAddress, Title | select
SamAccountName, GivenName, Surname, EmailAddress, Title | Export-CSV "C:\Ad
Extract.csv"
Here's one possible solution: 这是一种可能的解决方案:
$csvjobtitle = (Import-Csv -Header csvTitle -Path c:\Job_titles.csv).'csvTitle'
$csvjobtitle | ForEach-Object {
Get-ADUser -Filter {enabled -eq "true" -and Title -eq "$_"} -Properties SamAccountName, GivenName, Surname, EmailAddress, Title | Select SamAccountName, GivenName, Surname, EmailAddress, Title
} | Export-CSV "C:\Ad Extract.csv"
There are two changes to your previous code: 您之前的代码有两项更改:
ForEach-Object
loop which gets the AD user for each title (where the current title is represented via $_
). ForEach-Object
循环遍历这些值,该循环为每个标题获取AD用户(当前标题通过$_
表示)。 Because ForEach-Object
supports the pipeline (and our Get-ADUser
cmdlet returns its result to the pipeline), we can simply pipe the results of the ForEach-Object
to Export-CSV
. 因为
ForEach-Object
支持管道(并且我们的Get-ADUser
cmdlet将其结果返回到管道),所以我们可以简单地将ForEach-Object
的结果通过管道ForEach-Object
到Export-CSV
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.