简体   繁体   English

如何在Powershell where-object中使用和或过滤器

[英]How to use And Or Filters in powershell where-object

I am trying to get a list of properties of AD user objects. 我正在尝试获取AD用户对象的属性列表。 Most of the script works except the "where-object" filters are applied. 除了应用“ where-object”过滤器外,大多数脚本都可以运行。 I want to get all users who meet the following: LastLogonDate is more than 75 days ago (this works), enabled (this works) AND either of the following - account expires in the future or never expires. 我想让所有满足以下条件的用户:LastLogonDate超过75天(此功能),已启用(此功能)并且具有以下任一功能-帐户将来会过期或永不过期。 $when is defined properly. $ when定义正确。

I've tried a number of options and I typically get no output or one or the other in the output. 我尝试了许多选项,但通常没有任何输出,或者在输出中没有一个或另一个。

| Where-Object{($_.lastlogondate -le $When -AND $_.enabled -eq $True) -AND Where-Object($_.AccountExpirationDate -gt (Get-Date) -OR $_.AccountExpirationDate -eq 0)} |

I should see accounts with expiry dates in the future AND accounts that do not expire, but I'm having problems with the "This AND this AND (this OR this)" 我应该会看到将来有到期日期的帐户以及未到期的帐户,但是“此与此与(此或此)”存在问题

The extra Where-Object in your code should throw an error... 代码中多余的Where-Object应该引发错误...

    Where-Object {
            $_.lastlogondate -le $When -AND 
            $_.enabled -eq $True -AND 
            ($_.AccountExpirationDate -gt (Get-Date) -OR $_.AccountExpirationDate -eq 0)
    }

This should work if your input is correct. 如果您的输入正确,这应该可以工作。

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

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