【问题标题】:send email if extensionAttribute equals如果 extensionAttribute 等于,则发送电子邮件
【发布时间】:2021-07-29 18:15:12
【问题描述】:

我正在使用两个单独的 powershell 脚本。第一个手动定义指定用户的extensionAttribute15 的日期。我们打算通过计划调用第二个,以便在 extensionAttribute15 日期后 14 天发送一封电子邮件,但我收到“解析查询错误”。它仍会发送电子邮件,但日期参考不起作用。

第一个脚本是:

    $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

第二个脚本是:

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
}

如何最好地将扩展属性定义为日期,然后将其用于在未来日期调用电子邮件?

谢谢!

【问题讨论】:

    标签: powershell get-aduser


    【解决方案1】:

    您可以在 extensionAttribute15 中按您喜欢的方式格式化日期,因此如果您更喜欢格式 MM/dd/yyyy,那么只要您以完全相同的方式解析它们就可以了。

    在脚本 1 中,更改

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

    # 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}
    

    然后使用脚本 2:

    $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
    }
    

    如果您想确保收到关于用户的 extensionAttribute15 属性值不是日期格式 MM/dd/yyyy 的警告,您可以将脚本 #2 的代码更改为:

    $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'"
        }            
    }
    

    这样,您应该能够看到是哪些用户导致了错误消息,并且您可以准确地看到该属性中的内容。为了更清楚起见,我在警告消息中用单引号引用了该属性的值,因此您还可以发现可能触发错误的无关空格。

    【讨论】:

    • 谢谢!即使日期格式看起来相同,脚本 2 也会产生以下错误:使用“3”参数调用“ParseExact”的异常:“字符串未被识别为有效的 DateTime。”在 C:\Test\NewHireEmailReminder.ps1:7 char:20 + ... re-Object { [datetime]::ParseExact($_.extensionAttribute15, 'MM/dd/yy ... + ~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException
    • @nominus 虽然对我有用.. 你确定 extensionAttribute15 中的日期格式正好是 MM/dd/yyyy (即 '08/02/2021',而不是偶然像 M/d/yyyy (即“2021 年 8 月 2 日”?
    • 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 -- -------------------- 用户名 07/19/2021
    • 使用您的示例,[datetime]::ParseExact("07/19/2021", 'MM/dd/yyyy', $null) 解析得很好。问题是,在第二个脚本中,您迭代的不仅仅是一个用户,因此其他用户可能在该属性中具有无法使用格式“MM/dd/yyyy”解析的内容。如果您想标准化属性 extensionAttribute15 中的格式,您必须确保 ALL 用户具有该格式的日期。
    • 非常有见地。就是这样。更正另一个帐户后,过滤将返回匹配的帐户且没有错误。感谢您的大力协助!
    【解决方案2】:

    $date = (Get-Date).date

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

    寻找具有某些属性的用户,然后在未来 14 天。

    via.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2011-08-09
      • 2015-08-10
      • 1970-01-01
      • 2013-09-14
      • 2011-01-06
      • 1970-01-01
      相关资源
      最近更新 更多