powershell - Send-MailMessage,当 LastLogonDate = never 时状态从不?
问题描述
使用下面的 ps,我正在通过电子邮件向特定人员发送某些 AD 帐户属性,包括 LastLogonDate。如果该帐户从未使用过,则电子邮件将完全省略该值。我希望消息包含以下内容:John Smith 总共登录我们的系统 0 次,最后一次成功登录发布在 N/A 上。我该如何修改我必须完成的任务?谢谢!
# List every active account with a "SACRequest Account" desctription that will expire in 0,7,14 days and inlcude the name and email address of the original account requester (extensionAttribute1,extensionAttribute2)
import-module activedirectory
$Today = Get-Date -Format 'MM-dd-yyy'
$Rightnow = Get-Date -Format o
$reportObject = @()
$userList = get-aduser -filter {Description -like "SACRequest Account" -and Enabled -eq $True} -Properties displayname, accountExpires, description, passwordexpired,"msDS-UserPasswordExpiryTimeComputed",enabled,AccountExpirationDate,LastLogonDate,logoncount,passwordlastset, badlogoncount,lastbadpasswordattempt,extensionAttribute1,extensionAttribute2,department |
select displayname, accountExpires, description, passwordexpired,"msDS-UserPasswordExpiryTimeComputed",enabled,AccountExpirationDate,LastLogonDate,logoncount,passwordlastset, badlogoncount,lastbadpasswordattempt,extensionAttribute1,extensionAttribute2,department |
sort-object msDS-UserPasswordExpiryTimeComputed -descending
$obj = new-object PSobject
foreach ($user in $userList)
{
$obj = new-object PSobject
$obj | add-member noteproperty Name($user.displayname)
$obj | add-member noteproperty Description($user.description)
$obj | add-member noteproperty 'Password Expired'($user.Passwordexpired)
$obj | add-member noteproperty 'Account is Enabled'($user.Enabled)
$obj | add-member noteproperty 'AccountExpirationDate'($user.AccountExpirationDate.ToString('MM-dd-yyyy'))
$obj | add-member noteproperty 'LastLogonDate'($user.LastLogonDate.ToString('MM-dd-yyyy'))
$obj | add-member noteproperty 'Password Last Set'($user.PasswordLastSet)
$obj | add-member noteproperty 'Failed Logon Attempt'($user.lastbadpasswordattempt)
$obj | add-member noteproperty 'TotalLogonCount'($user.logoncount)
$obj | add-member noteproperty 'Total Failed Logons'($user.badlogoncount)
$obj | add-member noteproperty 'SACSubmitter'($user.extensionAttribute1)
$obj | add-member noteproperty 'SACSubmitterEmail'($user.extensionAttribute2)
$obj | add-member noteproperty 'Department'($user.department)
$reportObject += $obj}
$from = "System Access Control <systems@organization.org>"
foreach ($user in $reportObject) {
if (([datetime]$user.AccountExpirationDate).AddDays(-0) -eq $TodayOnly)
{
$Name = $user.name
$to = $user.SACSubmitterEmail
$subject = "Your contractors's login account will expire today!"
$hello = $user.SACSubmitter
$AccountExpirationDate = $user.AccountExpirationDate # -as [datetime]
$TotalLogonCount = $user.TotalLogonCount
$LastLogonDate = $user.LastLogonDate
$body = "Hello $hello,<br><br>"
$body += "The login account you requested for <b>$Name</b> will expire in one week on <b> $AccountExpirationDate</b>.<br><br>"
$body += "$name logged onto our systems a total of <b>$TotalLogonCount</b> times with the last successful log in posted on <b> $LastLogonDate</b>.<br><br>"
$body += "<a href='http://intranet/form.cfm?id=100'>If this account needs to remain active please submit a new System Access Control Request by clicking here.</a><br><br>"
$body += "Kind Regards,<br>"
$body += "Your friends in IT<br><br><br>"
$body += "<i>Data generated $RightNow</i>"
$mail = New-Object System.Net.Mail.Mailmessage $from, $to, $subject, $body
$mail.IsBodyHTML=$true
$server = "mail.organization.org"
$port = 25
$Smtp = New-Object System.Net.Mail.SMTPClient $server,$port
$Smtp.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
$smtp.send($mail)
}
}
foreach ($user in $reportObject) {
if (([datetime]$user.AccountExpirationDate).AddDays(-7) -eq $TodayOnly)
{
$Name = $user.name
$to = $user.SACSubmitterEmail
$subject = "Your contractors's login account will expire in one week!"
$hello = $user.SACSubmitter
$AccountExpirationDate = $user.AccountExpirationDate # -as [datetime]
$TotalLogonCount = $user.TotalLogonCount
$LastLogonDate = $user.LastLogonDate
$body = "Hello $hello,<br><br>"
$body += "The login account you requested for <b>$Name</b> will expire in one week on <b> $AccountExpirationDate</b>.<br><br>"
$body += "$name logged onto our systems a total of <b>$TotalLogonCount</b> times with the last successful log in posted on <b> $LastLogonDate</b>.<br><br>"
$body += "<a href='http://intranet/form.cfm?id=100'>If this account needs to remain active please submit a new System Access Control Request by clicking here.</a><br><br>"
$body += "Kind Regards,<br>"
$body += "Your friends in IT<br><br><br>"
$body += "<i>Data generated $RightNow</i>"
$mail = New-Object System.Net.Mail.Mailmessage $from, $to, $subject, $body
$mail.IsBodyHTML=$true
$server = "mail.organization.org"
$port = 25
$Smtp = New-Object System.Net.Mail.SMTPClient $server,$port
$Smtp.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
$smtp.send($mail)
}
}
foreach ($user in $reportObject) {
if (([datetime]$user.AccountExpirationDate).AddDays(-14) -eq $TodayOnly)
{
$Name = $user.name
$to = $user.SACSubmitterEmail
$subject = "Your contractors's login account will expire in two weeks!"
$hello = $user.SACSubmitter
$AccountExpirationDate = $user.AccountExpirationDate # -as [datetime]
$TotalLogonCount = $user.TotalLogonCount
$LastLogonDate = $user.LastLogonDate
$body = "Hello $hello,<br><br>"
$body += "The login account you requested for <b>$Name</b> will expire in one week on <b> $AccountExpirationDate</b>.<br><br>"
$body += "$name logged onto our systems a total of <b>$TotalLogonCount</b> times with the last successful log in posted on <b> $LastLogonDate</b>.<br><br>"
$body += "<a href='http://intranet/form.cfm?id=100'>If this account needs to remain active please submit a new System Access Control Request by clicking here.</a><br><br>"
$body += "Kind Regards,<br>"
$body += "Your friends in IT<br><br><br>"
$body += "<i>Data generated $RightNow</i>"
$mail = New-Object System.Net.Mail.Mailmessage $from, $to, $subject, $body
$mail.IsBodyHTML=$true
$server = "mail.organization.org"
$port = 25
$Smtp = New-Object System.Net.Mail.SMTPClient $server,$port
$Smtp.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
$smtp.send($mail)
}
}
解决方案
你在寻找类似的东西
if ($user.LastLogonDate){
#$user.LastLogonDate is not null, use the value
$obj | add-member noteproperty 'LastLogonDate'($user.LastLogonDate.ToString('MM-dd-yyyy'))
} else {
#$user.LastLogonDate is null, use never
$obj | add-member noteproperty 'LastLogonDate' 'Never'
}
推荐阅读
- javascript - 如何检测操作系统当前语言
- ios - 适用于 ios OAuth2、API V2 的 LinkedIn 登录。如何打开已安装的 LinkedIn 应用程序
- escaping - 如何逃脱 Emmet 中的转义字符?
- api - 如何让 Stripe 手动将事件重新发送到 webhook
- python - Django,Celery,Redis:AttributeError:模块“imp_expert”没有属性“celery”
- java - Update module version inside parent
- reactjs - 为孩子制作一个需要的 PropType 但在父母身上指定这个?
- android - 如何对List的所有相同数字值求和