The code is this one at the moment. There are other corrections to do I know.
$ComputersWithManagedBy = get-adcomputer -ldapfilter '(managedby=*)' -properties name, managedby | select-object name, managedby
$currentday = (Get-Date).ToUniversalTime()
$validedate = (Get-Date).adddays(7).ToUniversalTime()
Foreach ($CPT in $ComputersWithManagedBy){
$ExpiryDate = Get-ADcomputer -identity $CPT -Properties extensionAttribute15 | Select-Object extensionAttribute15
#struggling here with the date format
if ($ExpiryDate -gt $validedate){
Write-Host "Date is more than 7 days. Correcting to within 7 days. Maximum accepted value."
Set-ADcomputer –Identity $CPT -Clear "extensionAttribute15"
Set-ADcomputer -Identity $CPT -Add @{extensionAttribute15 = "$validedate"}
}elseif($ExpiryDate -le $currentday ) {
write-host "Emptying Managed by field"
#some code
}else{
write-host "Date is inferior we don't take action"
}
}