从CSV文件中过滤启用的AD用户 [英] Filter enabled AD users from CSV file

查看:303
本文介绍了从CSV文件中过滤启用的AD用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以导入用户列表,并希望检查是否禁用了这些用户中的任何一个.我确实尝试运行下面的脚本,但是它没有过滤CSV文件中的用户,而是过滤了整个组织中的每个人.任何建议,将不胜感激.如果需要使用标头,请在CSV文件的标头之一中显示名称和SIP地址.

I have a script to import a list of users and want to check if any of these users are disabled. I did try to run the script below but it doesn't filter the users in the CSV file it filters everyone in the entire organization. any suggestions would be appreciated. displayname and SIP address in one of the headers in the CSV file if needed to use the header.

Import-CSV -Path .\Piscataway-+1732.csv  | ForEach-Object {
  Get-ADUser -Filter "Enabled -eq '$true'"  | select Enabled,EmailAddress,SamAccountName
} | Export-CSV .\results77.csv -NoTypeInformation

推荐答案

您遇到了几个问题:

  1. 您正在将管道从Import-Csv传递到ForEach-Object.因此,Get-ADUser并不真正知道您正在管道输入对象.
  2. Get-ADUser的-Identity参数是按值而不是属性名称.因此您需要回显相应的列以将其发送到管道.
  3. 如果通过管道传输并使用-Filter参数,则过滤器将应用于整个域.不会将过滤器限制为管道输入.
  4. 如果要输出电子邮件地址,则必须告诉Get-ADUser检索它.
  1. You are piping From Import-Csv to ForEach-Object. So Get-ADUser doesn't really know you are piping it input objects.
  2. Get-ADUser's -Identity parameter is by value, not by property name. so you need to echo the appropriate column to send it down the pipe.
  3. If you pipe and use the -Filter parameter the filter is going to apply to the whole domain. It's not going to limit the filter to what you piped in.
  4. If you want the email address to be output you have to tell Get-ADUser to retrieve it.

尝试这样的事情:

Import-CSV -Path .\Piscataway-+1732.csv  | 
ForEach-Object{ $_.samAccountName }
Get-ADUser -Properties mail | 
Where-Object{ $_.Enabled }
Select-Object Enabled,mail,SamAccountName  | 
Export-CSV .\results77.csv -NoTypeInformation

注意:电子邮件地址的属性为"mail".

Note: The Property for the email address is "mail".

注意:由于我们没有CSV文件的示例,因此上述示例 假设有一个列名称为samAccountName.

Note:Since we don't have a sample of the CSV file the above example assumes there's a column names samAccountName.

现在,如果您希望输出来自CSV文件,但要根据AD中用户的状态对其进行验证,则必须更改方法.与往常一样,有几种方法可以做到这一点.

Now, if you want the output to come from the CSV file but validate it according to the user's status in AD we have to change the approach. As always there are several ways to do this.

示例1:

Import-CSV -Path "c:\temp\test.csv"  | 
Select-Object @{Label = 'Enabled'; Expression = { ( Get-ADUser $_.samAccountName ).Enabled } },EmailAddress,samAccountName |
Export-CSV -Path "c:\temp\Output.csv" -NoTypeInformation

这再次假定列名称(samAccountName).它还假定还没有已启用"列.因此,我们添加了一个名为enable的属性,该属性是通过Get-ADUser获取的.然后最终重新导出到Csv.

This again assumes the column name (samAccountName). It also assumes there is not already an "enabled" column. So we are adding a property called enabled that we're getting via Get-ADUser. Then finally re-exporting to Csv.

示例2:

$CsvData = Import-CSV -Path "c:\temp\test.csv"

$EnabledUsers = 
( 
    $CsvData | 
    ForEach-Object{ $_.samAccountName } |
    Get-ADUser |
    Where-Object{ $_.Enabled }
).samAccountName

$CsvData | 
Where-Object{ $EnabledUsers -contains $_.samAccountName } |
Select-Object @{Label = 'Enabled'; Expression = { $true } },EmailAddress,samAccountName |
Export-Csv -Path "c:\temp\Output.csv" -NoTypeInformation

示例1对于小型工作非常有用,但是对于较大的运行,对Get-ADUser的太多单独调用可能会很慢.在此示例中,一次导入CSV数据.然后使用它来获取在AD中启用的那些条目的平面列表.拥有该帐户后,您可以使用-contains运算符检查该帐户是否已启用.再次需要做一些额外的工作来添加已启用"属性.

Example 1 is great for small jobs but too many individual calls to Get-ADUser might be slow for larger runs. In this example Import the CSV data once. Then use it to get a flat list of those entries that are enabled in AD. Once you have that you can use the -contains operator to check if the account is enabled. Once again there's a little extra work to add the "Enabled" property.

这应该给您一个大致的想法.可能还有更多的方法可以执行此操作,但是希望这可以使您对将要发生的事情有个很好的了解.让我知道是否有帮助.

This should give you a general idea. There are probably a dozen more ways to do this, but hopefully this give you a good idea of what has to happen. Let me know if this helps.

这篇关于从CSV文件中过滤启用的AD用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆