如果extensionAttribute等于,则发送邮件



我使用两个独立的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:Testreminder.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
}

我怎样才能最好地定义一个扩展属性作为一个日期,然后在未来的日期使用它来调用电子邮件?

谢谢!

您可以在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:Testreminder.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:Testreminder.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'"
}            
}

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

$date = (Get-Date).date

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

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

通过。

相关内容

  • 没有找到相关文章

最新更新