简体   繁体   English

如何在Powershell中从Get-ADuser cmdlet输出中排除多个用户?

[英]How can I exclude multiple users from Get-ADuser cmdlet output in powershell?

I am trying export a list of all users in a specific office for an external guest check-in system. 我正在尝试为外部访客签到系统导出特定办公室中所有用户的列表。 Except some users (about 20 or so) are not to be added to the public directory, and I need them excluded from the output. 除了不将某些用户(大约20个左右)添加到公共目录外,我需要将它们从输出中排除。

Here's what I have so far... 到目前为止,这就是我所拥有的...

    Get-ADUser -Filter {City -eq "Dallas"} -Properties GivenName, Surname, EmailAddress, Name |
        Select GivenName, Surname, EmailAddress, Name | 
        Sort-Object -Property GivenName | 
        Export-Csv $env:USERPROFILE\Desktop\ADusers.csv -NoTypeInformation -Force

I am hoping to avoid a long filter line for example... 我希望避免例如较长的过滤线...

-filer {(name -ne "name 1")(name -ne "name 2")...} etc 

Ideally, I would like to create a variable listing the specified users. 理想情况下,我想创建一个列出指定用户的变量。 That way I can easily modify later. 这样我以后可以轻松修改。

    $excluded = "Name 1","Name 2","Name 3","Name 4"

Results are as expected (except for the users that need to be excluded)... 结果符合预期(需要排除的用户除外)...

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Assuming there isn't a custom attribute or other way to identify these users / set them apart in ActiveDirectory already the one thing you could do is build the filter string from your name array 假设没有自定义属性或其他方法来标识这些用户/已经在ActiveDirectory中将它们分开,您可以做的一件事就是从名称数组中构建过滤器字符串

$excluded = "Name 1","Name 2","Name 3","Name 4"
$namefilter = ($excluded | ForEach-Object{"name -ne '$_'"}) -join " -and " 
$cityfilter = "City -eq 'Dallas'"
Get-ADUser -Filter ($cityfilter, $namefilter -join " -and ") -Properties GivenName, Surname, EmailAddress, Name

So the filter would effectively be this: 因此,过滤器将有效地是这样的:

City -eq 'Dallas' -and name -ne 'Name 1' -and name -ne 'Name 2' -and name -ne 'Name 3' -and name -ne 'Name 4'

It might seem convoluted but $cityfilter, $namefilter -join " -and " allows for one of those filters to be missing without code changes. 它可能看起来很复杂,但是$cityfilter, $namefilter -join " -and "允许在不更改代码的情况下丢失其中一个过滤器。 So if the $excluded ended up being empty the code would still succeed. 因此,如果$excluded$excluded最终为空,则代码仍将成功。 You could easily simply this but it was something I thought of mitigating. 您可以轻松地做到这一点,但这是我想缓解的问题。

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

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