0

I'm using two separate powershell scripts. The first one manually defines a date for the specified user's extensionAttribute15. The second one we intend to invoke via schedule to send an email 14 days from the extensionAttribute15 date but I'm getting "Error parsing query". It still sends the email but the date reference isn't functional.

First script is:

    $username = Read-Host 'Enter username'
    $ADuser = Get-ADUser -Filter 'sAMAccountName -eq $username'
    $string = Read-Host = 'Please enter a date using the format MM/DD/YYYY'
    $Date= [DateTime] $string
    Write-Host $date -ForegroundColor DarkYellow
    set-aduser $username -replace @{extensionattribute15="$Date"}
    Get-ADUser -Identity $username    -Properties * | select extensionattribute15

Second script is:

import-module activedirectory
#Show me who
Get-ADUser -filter {extensionAttribute15 -eq (Get-Date).adddays(14) -and Description -like 'Test Do not modify' -and Enabled -eq $True} -Properties * | select CN, extensionAttribute15
$users = Get-ADUser -filter {extensionAttribute15 -eq (Get-Date).adddays(14) -and Description -like 'Test Do not modify' -and Enabled -eq $True} -Properties * | select CN, extensionAttribute15
$users | Foreach-Object{
    $message = (Get-Content "C:\Test\reminder.htm" | Out-String )
    $message = $message -replace "USRname",$_.GivenName
    $message = $message -replace "USRalias",$_.SamAccountName
    $message = $message -replace "USRemail",$_.EmailAddress
    
    ### SMTP Mail Settings
    $SMTPProperties = @{
        To = $_.EmailAddress
        From = "me@org.org"
        Subject = "Reminder: Action Required"
        SMTPServer = "mail.org.org"
    }
    Send-MailMessage @SMTPProperties -Body $message -BodyAsHtml
}

How can I best define an extensionattribute as a date then use it for invoking an email at a future date?

Thanks!

nominus
  • 11
  • 5

2 Answers2

0

$date = (Get-Date).date

$usersToActive = Get-ADUser -Filter "extensionAttribute15 -like '*'" -Properties extensionAttribute15 | where-object { [datetime]::Parse($_.extensionAttribute15).AddDays(-14) -eq $date }

Looking for users with something in attribute and then 14 days in the future.

via.

0

You can format the dates how you like in the extensionAttribute15, so if you prefer format MM/dd/yyyy, then that's fine as long as you parse them out in the exact same manner.

In script 1, change

Set-ADUser $username -replace @{extensionattribute15="$Date"}

to

# make sure the formatting is exactly how you want to parse it later
$dateToInsert = '{0:MM/dd/yyyy}' -f $Date
Set-ADUser $username -replace @{extensionattribute15=$dateToInsert}

Then use script 2 like:

$refDate = (Get-Date).AddDays(14).Date   # 14 days from now
$message = Get-Content 'C:\Test\reminder.htm' -Raw
$filter  = "extensionAttribute15 -like '*' -and Description -like '*Test Do not modify*' -and Enabled -eq 'True'"
# or use -LDAPFilter "(&(extensionAttribute15=*)(description=*Test Do not modify*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"

Get-ADUser -Filter $filter -Properties extensionAttribute15, EmailAddress | 
    Where-Object { [datetime]::ParseExact($_.extensionAttribute15, 'MM/dd/yyyy', $null) -eq $refDate } | Foreach-Object {
    Write-Host "Sending email to user $($_.Name)"
    ### SMTP Mail Settings
    $SMTPProperties = @{
        To         = $_.EmailAddress
        From       = 'me@org.org'
        Subject    = 'Reminder: Action Required'
        SMTPServer = 'mail.org.org'
        # you can chain multiple -replace
        Body       = $message -replace 'USRname', $_.GivenName -replace 'USRalias', $_.SamAccountName -replace 'USRemail', $_.EmailAddress
        BodyAsHtml = $true
    }
    Send-MailMessage @SMTPProperties
}

If you want to make sure you get a warning about users with a value in their extensionAttribute15 property that is NOT in date format MM/dd/yyyy, you can change the code for script #2 into:

$refDate = (Get-Date).AddDays(14).Date   # 14 days from now
$message = Get-Content 'C:\Test\reminder.htm' -Raw
$filter  = "extensionAttribute15 -like '*' -and Description -like '*Test Do not modify*' -and Enabled -eq 'True'"
# or use -LDAPFilter "(&(extensionAttribute15=*)(description=*Test Do not modify*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"

Get-ADUser -Filter $filter -Properties extensionAttribute15, EmailAddress | Foreach-Object {
    $username = $_.Name  # capture these properties in case we hit the catch block
    $extn15   = $_.extensionAttribute15
    try {
        if ([datetime]::ParseExact($extn15, 'MM/dd/yyyy', $null) -eq $refDate) {
            Write-Host "Sending email to user $($_.Name)"
            ### SMTP Mail Settings
            $SMTPProperties = @{
                To         = $_.EmailAddress
                From       = 'me@org.org'
                Subject    = 'Reminder: Action Required'
                SMTPServer = 'mail.org.org'
                # you can chain multiple -replace
                Body       = $message -replace 'USRname', $_.GivenName -replace 'USRalias', $_.SamAccountName -replace 'USRemail', $_.EmailAddress
                BodyAsHtml = $true
            }
            Send-MailMessage @SMTPProperties
        }
    }
    catch {
        # inside a catch block the $_ automatic variable represents the actual exception object
        Write-Warning "User $username has a wrong date format in extensionAttribute15: '$extn15'"
    }            
}

With that, you should be able to see which of the users cause the error message and you can see exactly what is in that property. For extra clarity, I have single-quoted the value of the property in the warning message, so you will also be able to spot extraneous whitespaces that could trigger the error.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks! Script 2 produces the following error even though the date format looks identical: Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." At C:\Test\NewHireEmailReminder.ps1:7 char:20 + ... re-Object { [datetime]::ParseExact($_.extensionAttribute15, 'MM/dd/yy ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException – nominus Aug 02 '21 at 13:20
  • @nominus Works for me though.. Are you sure the date format in the extensionAttribute15 is exactly `MM/dd/yyyy` (i.e. '08/02/2021', not by chance something like `M/d/yyyy` (i.e. '8/2/2021' ? – Theo Aug 02 '21 at 13:43
  • Yes sir PS C:\test> C:\Test\User Accounts - [HireDate] Set extensionAttribute15.ps1 Enter username: username = Please enter a date using the format MM/DD/YYYY: 07/19/2021 7/19/2021 12:00:00 AM cn extensionattribute15 -- -------------------- Username 07/19/2021 – nominus Aug 02 '21 at 18:15
  • Using your example, `[datetime]::ParseExact("07/19/2021", 'MM/dd/yyyy', $null)` parses just fine. The thing is that in the second script, you are iterating over more than just that one user, so probably other users have something in that attribute that is not parsable using format 'MM/dd/yyyy'. If you want to standardize the format in property extensionAttribute15 you must make sure **ALL** users have a date in that exact format. – Theo Aug 02 '21 at 18:39
  • Very insightful. That was it exactly. After the other account was corrected the filtering is returning the matching accounts with no errors. Thank you kindly for the great assist! – nominus Aug 02 '21 at 19:46