Once again I have broken a foreach loop and cannot figure out why. It appears to iterate twice instead of once for some of the accounts. I think I have my {} in the correct format but apparently not. Can anyone help me find what I'm missing?
Relevant Code:
foreach ($OU in $OUs) {
# Search for User and/or Computer Objects inactive for XX days. Disable object if not in DoNotDisable Security Groups
$days = $days + "D"
$accounts = Search-ADAccount -SearchBase $OU.DistinguishedName -AccountInactive -TimeSpan ([timespan]7D) @scope
foreach($account in $accounts){
If ($noDisable -notcontains $account.Name) {
Write-Host $account
# #Disable-ADAccount -Identity $account.DistinguishedName -Verbose $whatIf | Export-Csv $logFile
}
}
}
Output: Each computer account is listed twice except the last 2.

-SearchScope OneLevelto theSearch-ADAccountcommand