【问题标题】:Exclude Multiple OUs in a foreach loop在 foreach 循环中排除多个 OU
【发布时间】:2020-06-06 07:19:58
【问题描述】:

我需要弄清楚如何通过可分辨名称从下面的 foreach 循环中排除多个 OU,这样用户/计算机帐户就不会被找到两次(一次在它的原始 OU 中,然后再次在它移动到的 OU 中。

我尝试使用 Where 子句尝试过滤单个 OU 以进行测试,但是当我使用它运行脚本时,它下面没有任何处理。从我的 foreach 循环中排除 OU 的最佳方法是什么?

foreach ($OU in $OUs) {
    #Where($OU.DistinguishedName -ne 'OU=Disabled Users,DC=test,DC=local') {
        $params = @{
        SearchBase = [String]$OU.DistinguishedName
        SearchScope = [String]"OneLevel"
        AccountInactive = $true
        TimeSpan = ([timespan]$days)
        Verbose = $true
        }

        If($users) { 
            $params.Add("UsersOnly",$true)
        }
        ElseIf($computers) { 
            $params.Add("ComputersOnly",$true)
        }

        $accounts = Search-ADAccount @params

        $params.Clear()

        foreach($account in $accounts) {
            If ($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "User" ) {
                $params = @{
                    Identity = [string]$account.DistinguishedName
                    Verbose = $true
                }
                Disable-ADAccount @params @whatIf

                $params.Add("Description",$description)

                Set-ADUser @params @WhatIf

                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Users,DC=test,DC=local')

                Move-ADObject @params @WhatIf
            }
            elseif($noDisable -notcontains $account.Name -and $account.ObjectClass -eq "Computer") {
                $params = @{
                    Identity = [string]$account.DistinguishedName
                    Verbose = $true
                }
                Disable-ADAccount @params @whatIf

                $params.Add("Description",$description)

                Set-ADComputer @params @WhatIf

                $params.Remove('Description')
                $params.Add("TargetPath", 'OU=Disabled Computers,DC=test,DC=local')

                Move-ADObject @params @WhatIf
            }   

        }
    #}
}

【问题讨论】:

    标签: powershell foreach where-clause


    【解决方案1】:

    这不是 where 的工作原理。

    Where(){} # Wont Work
    

    您需要通过管道传送到 Where-Object

    $OUs | Where-Object{$_.DistinguishedName -ne 'OU=Disabled Users,DC=test,DC=local'} | Foreach-Object{
        #Do Stuff Here
    }
    

    例子

    $Names = @("Steve", "John", "Smith")
    $Names | Where-Object{ $_ -ne "John"} | Foreach-Object{
        $_
    }
    

    这将返回

    Steve
    Smith
    

    现在您说要排除许多 OU,所以让我们稍微修改一下以排除多个名称。

    $Names = @("Steve", "John", "Smith", "Billy", "Sally")
    $ExcludeList = @("Steve", "Smith", "Sally")
    $Names | Where-Object{ $ExcludeList -notcontains $_ } | Foreach-Object{
        $_
    }
    

    这将返回

    John
    Billy
    

    【讨论】:

    • 非常感谢您的精彩解释。我搞定了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2017-03-28
    • 1970-01-01
    • 2021-11-18
    • 2023-03-14
    相关资源
    最近更新 更多