2016-06-24 211 views
0

我嘗試編輯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/AD密碼過期通知

我喜歡在HTML文件發送日誌,我嘗試添加了2個新列$發送,$ expireson 。 我將2列添加到get-aduser查詢,但結果在這個列是不好的。 我不知道如何才能使代碼正常工作。

我下面的代碼:

$smtpServer="mail.company.com" 
$expireindays = 52 
$from = "Company Administrator <[email protected]>" 
$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 = "[email protected]" 
$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 
+0

或者我可以使用csv文件製作日誌文件,但我怎樣才能使csv文件與分隔符「;」。但是我不知道我可以在哪裏添加Export-Csv -Path c:\ temp.csv -Delimiter「;」在我的代碼中。 – cinq2

+0

我可以爲$ expireson變量創建一些循環嗎? – cinq2

回答

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