Recently i build below code to automatically move disabled account to other OU that are older then 30 days (in disabled state)
$checkcurrentdate = Get-Date -Format dd/MM/yyyy
$DateMaxTime = (Get-date).AddDays(-30)
$getData = get-ADUser -Filter * -Properties * -SearchBase "OU=Disabled,DC=love,DC=donuts" `
|where {$_.Description -like "LEFT*"} |select name,samaccountname,description
$FinalResult = New-object System.Collections.ArrayList
Foreach ($Data IN $getData){
$DataPart = $null
$UserdistinguishedName = $null
$DataPart=$Data.description
$DatePart= $DataPart.substring(5,10)
$FinalDate = [datetime]::ParseExact($DatePart,'dd/MM/yyyy',$null)
If ($FinalDate -lt $DateMaxTime ){
Write-Host "$($Data.samaccountname), $($Data.description) moved to deleteMe"
$User = $Data.samaccountname
$UserdistinguishedName = (Get-ADUser -Identity $User).distinguishedName
Move-ADObject -Identity $UserdistinguishedName -TargetPath "OU=DeleteMe,,DC=love,DC=donuts," #move user to disabled
$FinalResult.Add(New-Object psobject -Property @{User=$User})
}
else{
Write-Host "$($Data.samaccountname), $($Data.description) still in disabled"
}
}
$list = $FinalResult |Select User |Out-String
$list.gettype()
I start task scheduler and it was working for w few month in automation But recently i discovered an error which says below :
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
I run this code manually again and error dissapeard but it stuck my code and was not working
I am curios why this happend and if there is any way to alert me if this error occurs ?
probably this happends at line :
Move-ADObject -Identity $UserdistinguishedName -TargetPath "OU=DeleteMe,,DC=love,DC=donuts," #move user to disabled
$FinalResult += New-Object psobject -Property @{User=$User}