jeudi 31 mars 2016

If And statement with a foreach for AD Users in Powershell

$names = Import-CSV C:\PowerShell\TerminatedEmployees.csv $Date = Get-Date foreach ($name in $names) { Get-ADPrincipalGroupMembership -Identity "$($name.TextBox37)" | select Name | Out-File "C:\Powershell\ADUserMemberships\$($name.TextBox37)Memberships.txt" $ADgroups = Get-ADPrincipalGroupMembership -Identity "$($name.TextBox37)" | where {$_.Name -ne "Domain Users"} Remove-ADPrincipalGroupMembership -Identity "$($name.TextBox37)" -MemberOf $ADgroups -Confirm:$false Disable-ADAccount -Identity "$($name.TextBox37)" Get-ADUser -Identity "$($name.TextBox37)" | Move-ADObject -TargetPath "OU=DisabledAccounts,OU=XXX,DC=XXX,DC=XXXX,DC=XXX" Set-ADUser -Identity "$($name.TextBox37)" -Description "Disabled $Date" }

This is an already working script I have. However, I realized I need to check 2 properties on the AD user to determine if they need to need to go through my foreach statement. Both properties need to be met. If they are then there's no reason for the AD users to be processed.

  1. The AD user is already disabled.

  2. The AD user already resides in the Disabled OU.

I'm thinking this needs to be done in an If -And statement. But does this need to be done before the foreach or inside the foreach?

Aucun commentaire:

Enregistrer un commentaire