I have logical problem how can I make script more secure to NON Block users From IT Groups By some operations users which need to block them employees from AD. I am so close to automate this process, by share to them CSV file with username,DateDisable,DateEnable.
Get-Date
Write-Host $b
$b = (Get-Date).ToString('M"/"d"/"yyyy')
Import-Csv "I:\Clients\Block Accounts\Accounts Deactivation.csv" | ForEach-Object {
$SamAccountName = $_."SamAccountName"
$dateDisable = $_."dateDisable"
$dateEnable = $_."dateEnable"
#How can I search users in group like PLKAT-NON-BLOCK-USERS and don't block users from this group by IF function. Can you tell me more about this solution. I will be grateful for some clues.
if ( Get-ADPrincipalGroupMembership -And $dateDisable -eq $b) {
Get-ADUser -Identity $SamAccountName | Disable-ADAccount
Write-Host "-User "$SamAccountName" Disabled"
}
$dateEnable = $_."dateEnable"
if ( $dateEnable -eq $b) {
Get-ADUser -Identity $SamAccountName | Enable-ADAccount
Write-Host "-User "$SamAccountName" Enable"
}
}
At the top of your script, you can get a list of all users in the
PLKAT-NON-BLOCK-USERSgroup first.Then in the code check if the user you are iterating is a member of this group and if so, do not disable that user.
Something like: