I need to change the following script to evaluate if the timestamp in a custom atribute is older than 1 year than the current date.
# The condition is met if $Context.ConditionIsMet is set to $True.
$Context.ConditionIsMet = $False
$property = "adm-CustomAttributeDate15" # TODO: modify me
# Get attribute value
try
{
$compareDate = $Context.TargetObject.Get($property)
}
catch
{
$Context.ConditionIsMet = $False
return
}
# Compare dates
$currentDate = [System.DateTime]::UtcNow
$Context.ConditionIsMet = (($compareDate.Date -eq $currentDate.Date) -and ($compareDate.Hour -eq $currentDate.Hour))
UPDATE
I managed to get it myself, this script will work to evaluate if it has been greater than one year compared to the custom data attribute
# Initialize condition to False
$Context.ConditionIsMet = $False
# Define the property to be evaluated
$property = "adm-CustomAttributeDate15" # TODO: Modify as needed
# Try to get the attribute value
try {
$compareDate = $Context.TargetObject.Get($property)
} catch {
Write-Error "Failed to retrieve the attribute value: $property"
$Context.ConditionIsMet = $False
return
}
# Validate if the attribute is a valid DateTime object
if (-not ($compareDate -is [System.DateTime])) {
Write-Error "The value of $property is not a valid DateTime."
$Context.ConditionIsMet = $False
return
}
# Get the current UTC date
$currentDate = [System.DateTime]::UtcNow
# Calculate the difference in years
$yearDifference = $currentDate.Year - $compareDate.Year
# Check if the compareDate is more than one year ago
if ($yearDifference -gt 1 -or ($yearDifference -eq 1 -and $currentDate.DayOfYear -ge $compareDate.DayOfYear)) {
$Context.ConditionIsMet = $True
} else {
$Context.ConditionIsMet = $False
}