简体   繁体   中英

Exclude Multiple OUs in a foreach loop

I need to figure out how to exclude multiple OUs from the below foreach loop by distinguished name so the user/computer account is not found twice (once in it's original OU and then again in the OU it's moved to.

I tried using a Where clause trying to filter a single OU for testing but when I run the script with it nothing below it processes. What is the best method to exclude the OU(s) from my foreach loop?

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
            }   

        }
    #}
}

在此处输入图片说明

Thats not how where works.

Where(){} # Wont Work

You need to pipe to Where-Object

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

Example

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

That will return

Steve
Smith

Now you said you had many OUs to exclude so lets change it up a little to exclude multiple names.

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

This will return

John
Billy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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