0

我正在使用两个单独的 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
}

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

谢谢!

4

2 回答 2

0

$date = (Get-Date).date

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

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

通过。

于 2021-07-29T18:37:12.047 回答
0

您可以在 extensionAttribute15 中以您喜欢的方式格式化日期,因此如果您更喜欢 format 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'"
    }            
}

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

于 2021-07-31T10:04:19.120 回答