0 votes

I have a script in the condition "If PowerShell script returns true". For some reason the conditions in the script is not evaluated correctly.

Condition

Script

Log

As you can see, the log is supposed to be written only when the condition is met. I Don't know why the condition was met when the log clearly show that DisableDate is Friday, August 07, 2015 4:00:00 PM and Today is Tuesday, August 04, 2015 7:48:40 AM.

by (730 points)

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello,

The [System.DateTime]::Now method returns a DateTime structure that represents the current time. In your script, you use the DateTime property of this structure. In PowerShell, this will get you a string representing the date in text format, for example, Wednesday, August 5, 2015 3:53:36 PM.

The same is with the 'account disabled' date. $Context.TargetObject.Get($disableOn) will get you a DateTime structure representing the date when the user must be disabled. However, if you use $Context.TargetObject.Get($disableOn).DateTime, you will get a string representing the same date.

Thus, on the following line, you are comparing text strings representing the dates instead of actual dates: if ($disableDate -le $today).

To remedy the issue, remove the .DateTime part on the following lines:

$disableDate = $Context.TargetObject.Get($disableOn).DateTime
$today = [System.DateTime]::Now.DateTime

Also, [System.DateTime]::Now returns the date/time in local time, but $Context.TargetObject.Get("<DateTimeAttribute>") returns the date/time in UTC time, so your script doesn't take into account time zones. To remedy this, instead of [System.DateTime]::Now use [System.DateTime]::UtcNow:

$today = [System.DateTime]::UtcNow

0

Thank you! $Context.TargetObject.Get($disableOn) was returning the time in the local time zone (as entered), so I switched the UtcNow back to Now. Works fine now.

Related questions

0 votes
1 answer

Hello The danish/norwegian letter "Ø" is not handled correctly in Adaxes. Even if regexp is set to ^[A-Z] , it's still possible for the user to enter and use the letter "Ø" in their ... : - "Ø" = U+00d8 (upper case) - "ø" = U+00f8 (lower case) - Thanks

asked May 11, 2016 by Boxx.dk (2.6k points)
0 votes
1 answer

Hello! I am creating a scheduled task, which in pseudocode does the following: 1st action: If account is locked AND if extensionattribute6 is empty send email to the locked account set ... of my DC's, the value is correct. How do I work around this? Thanks!

asked Aug 19, 2014 by Erlend (160 points)
0 votes
1 answer

I tried searching and looking through the script repo but I coun't find what I was looking for. Is there a script I can use with "If PowerShell scripts returns true" ... want to run a scheduled job only on mailboxes that are NOT Shared Mailboxes. Thank you,

asked Sep 26, 2022 by hgletifer (1.3k points)
0 votes
1 answer

I am trying to send a $context.logmessage from a condition script in a Scheduled Task but I get nothing in the log. Is this not possible? Morten A. Steien

asked Jul 20, 2020 by Morten A. Steien (300 points)
0 votes
1 answer

Hello! As part of my onboarding routine, I have a custom form that has a Boolean value for send welcome email to employee. I also have an optional field for personal ... ? If not is the best method using a business rule check before creating the user?

asked Aug 17, 2023 by thatcher (120 points)
3,548 questions
3,238 answers
8,232 comments
547,810 users