Hi,
I am trying to create a script that will look at the OU where the disabled accounts are and then move the mailboxes associated to those accounts to another database plus then delete the account if this has not been logged into for the last 6 months. I have created a script but I am not sure if this will be correct.
Has anyone else done this before?
This is what I have used from reading online and will then generate a report but I have not included the report in this
#load AD moduleimport-module activedirectory
$oldDate = [DateTime]::Today.AddDays(-180)
$warnDate = [DateTime]::Today.AddDays(-120)
$AMSearchBase = "OU=Old Users,DC=DOMAIN,DC=com"
$delUsers = @()
$warnUsers = @()
$wlistUsers = @()
$6MonthUsers = @()
##AM Section##
##Retrieves disabled user accounts and stores in an array
$disabledUsers = Get-ADUser -filter {(Enabled -eq $False)} -Searchbase $AMSearchBase -Searchscope 1 -Properties Name,SID,Enabled,LastLogonDate,Modified,info,description
foreach ($name in $disabledUsers) {
if ($name.info -ne "WHITELIST" -and $name.modified -le $oldDate)
{
Get-Mailbox | ?{$_.ExchangeUserAccountControl -eq 'AccountDisabled'}| Fl Name,Database,UserAccountControl -and New-MoveRequest | ?{$_.ExchangeUserAccountControl
-eq 'AccountDisabled'} -TargetDatabase Old Users -ArchiveDatabase -Old Users}
{
Remove-ADUser -id $name.SID -confirm:$false
$delUsers = $delUsers + $name
}
elseif ($name.info -eq "WHITELIST") {
#Write-Host $name.name " is Whitelisted"
$wlistUsers = $wlistUsers + $name
}
elseif ($name.info -ne "WHITELIST"-and $name.modified -le $warnDate) {
#Write-Host $name.name " is will be deleted in 2 Months"
$warnUsers = $warnUsers + $name
}
else {
#Write-Host $name.name " was modified less than 6 Months ago"
$6MonthUsers = $6MonthUsers + $name
}
}
If you have some suggestions with this then please can you help me
Thanks
Marty