0 votes

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
}
by (610 points)

1 Answer

0 votes
by (294k points)

Hello,

There is actually no need to use scripts. You can use the If <property> <relation> <value> condition in a custom command, business rule or scheduled task: image.png

0

I tried this and it didn't seem to work. For clarification, I was using a timestamp at first, must it be a date field instead?

0

Hello,

It does not matter, the condition will work fine for both. If you face issues, please, describe the exact desired behavior and provide screenshots of your configuration.

0

Let me do some more testing to triple check it did not work as I expected. I certainly could have misapplied the time comparisons.

0

I've confirmed that works. Thank you.

Related questions

0 votes
1 answer

Need help creating and updating users from a fixed formatted file exported daily by the HR system system. I just want to make sure that I am on the right track before ... this work? Any suggestions as to how to handle this scenario, best practices? Thanks!

asked Feb 2, 2016 by afshin (50 points)
0 votes
1 answer

Hello, I am trying to create a custom command that handles the process of converting a user account for a temp to a FTE. This needs to: 1. move the user to a new OU. 2. change ... to "xxx" OU Set 2: Always --Modify the user: set employee type to...... etc.

asked Jul 26, 2013 by DFassett (710 points)
0 votes
1 answer

Hi, we just recently installed Adaxes and would like to implement a PowerShell script that I have previously written which cleans up user objects if they have been manually ... to perform the operation Stack trace: at &lt;ScriptBlock&gt;, &lt;No file&gt;".

asked Oct 2, 2023 by Mark.Monaco (20 points)
0 votes
1 answer

We will be adding email aliases to the proxyaddresses attribute in order to track our Google Apps mailbox aliases (no Exchange here). I currently have a script that Support helped ... is already in use. Please verify that account is not being duplicated"); } }

asked Dec 10, 2014 by jiambor (1.2k points)
0 votes
1 answer

Good afternoon everyone, I have a web form that will create a simple OU with nothing more than a name and description. There next lies a business rule that kicks off ... is being ran from the same domain controller that the web interface ran its creation from?

asked Mar 30, 2017 by strikk (360 points)
3,588 questions
3,277 answers
8,303 comments
548,076 users