来自technet gallery的AD密码到期通知

时间:2016-06-24 07:14:23

标签: powershell

我尝试编辑来自的powershell密码电子邮件通知 - https://gallery.technet.microsoft.com/Password-Expiry-Email-177c3e27 要么 - https://blogs.technet.microsoft.com/askpfeplat/2015/05/04/how-to-setup-a-password-expiration-notification-email-solution/

我喜欢发送登录html文件,我尝试添加2个新列$ sent,$ expireson。 我为get-aduser查询添加了2列,但此列的结果很糟糕。 我不知道如何才能使代码正常运行。

我的代码如下:

$smtpServer="mail.company.com"
$expireindays = 52
$from = "Company Administrator <support@mycompany.com>"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "C:\scripts\test.html" # ie. c:\mylog.csv
$testing = "Enabled" # Set to Disabled to Email Users
$testRecipient = "testuser@company.com"
$filecontent = (Get-Content $logfile | Out-String)

# System Settings
$textEncoding = [System.Text.Encoding]::UTF8
$date = Get-Date -format ddMMyyyy
# End System Settings

# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties Name, ``PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress |
where ($_.Enabled -eq "True") | where {$_.PasswordNeverExpires -eq $false} | where {$_.passwordexpired -eq $false} |
@{N="column1";E= {$expireson}}, @{N="column1";E= {$sent}} |
Convertto-html | out-file $logfile

# Process Each User for Password Expiry
foreach ($user in $users)
{
$Name = $user.Name
$emailaddress = $user.emailaddress
$passwordSetDate = $user.PasswordLastSet
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
$sent = "" # Reset Sent Flag
# Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
    $maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
else
{
    # No FGP set to Domain Default
    $maxPasswordAge = $DefaultmaxPasswordAge
}


$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days

# Set Greeting based on Number of Days to Expiry.

 # Check Number of Days to Expiry
$messageDays = $daystoexpire

if (($messageDays) -gt "1")
{
    $messageDays = "in " + "$daystoexpire" + " days."
}
else
{
    $messageDays = "today."
}

# Email Subject Set Here
$subject="Your password will expire $messageDays"

# Email Body Set Here, Note You can use HTML, including Images.
$body ="
Dear $name,
<p> Your Password will expire $messageDays<br>
To change your password on a PC press CTRL ALT Delete and choose Change Password <br>
<p>Thanks, <br> 
</P>"

# If Testing Is Enabled - Email Administrator
if (($testing) -eq "Enabled")
{
    $emailaddress = $testRecipient
} # End Testing

# If a user has no email address listed
if (($emailaddress) -eq $null)
{
    $emailaddress = $testRecipient    
}# End No Valid Email

# Send Email Message
if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
{
    $sent = "Yes"
    # If Logging is Enabled Log Details
    if (($logging) -eq "Enabled")
    {
        Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent" 
    }
    # Send Email Message
    #Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $textEncoding   

} # End Send Message
else # Log Non Expiring Password
{
    $sent = "No"
    # If Logging is Enabled Log Details
    if (($logging) -eq "Enabled")
    {
        Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent" 
    }        
}

} # End User Processing

#Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $textEncoding

# End

1 个答案:

答案 0 :(得分:0)

我修复了将CSV转换为HTML并添加一些CSS的问题。现在我已登录html表格发送电子邮件。

$info = Import-Csv $logFile | ConvertTo-Html -head $style
$mailBody =
" 
$info
" 

Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject test -body $mailBody -bodyasHTML -priority High -Encoding $textEncoding