简体   繁体   English

从Get-ADUser返回与csv匹配的职位

[英]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: 您之前的代码有两项更改:

  1. We get back just the 'csvTitle' values from the CSV, rather than an object which has a csvTitle property. 我们仅从CSV中获取“ csvTitle”值,而不是具有csvTitle属性的对象。
  2. We loop through those values with a 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-ObjectExport-CSV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM