如果 extensionAttribute 等于则发送电子邮件
send email if extensionAttribute equals
我正在使用两个单独的 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
}
如何才能最好地将扩展属性定义为日期,然后将其用于在将来的某个日期调用电子邮件?
谢谢!
$date = (Get-Date).date
$usersToActive =
获取 ADUser -Filter "extensionAttribute15 -like '*'" -Properties extensionAttribute15 |
where-object { [datetime]::Parse($_.extensionAttribute15).AddDays(-14) -eq $date }
正在寻找具有某些属性的用户,然后在未来 14 天。
您可以在 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'"
}
}
有了它,您应该能够看到哪些用户导致了错误消息,并且您可以准确地看到其中的内容 属性。为了更加清晰,我在警告消息中用单引号将 属性 的值引了出来,这样您还可以发现可能触发错误的多余空格。
我正在使用两个单独的 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
}
如何才能最好地将扩展属性定义为日期,然后将其用于在将来的某个日期调用电子邮件?
谢谢!
$date = (Get-Date).date
$usersToActive = 获取 ADUser -Filter "extensionAttribute15 -like '*'" -Properties extensionAttribute15 | where-object { [datetime]::Parse($_.extensionAttribute15).AddDays(-14) -eq $date }
正在寻找具有某些属性的用户,然后在未来 14 天。
您可以在 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'"
}
}
有了它,您应该能够看到哪些用户导致了错误消息,并且您可以准确地看到其中的内容 属性。为了更加清晰,我在警告消息中用单引号将 属性 的值引了出来,这样您还可以发现可能触发错误的多余空格。