【问题标题】:Setting HTML font color in Powershell email在 Powershell 电子邮件中设置 HTML 字体颜色
【发布时间】:2012-06-20 17:04:37
【问题描述】:

我有一个 powershell 脚本,它将为密码将在

这是我目前正在使用的代码。我还要补充一下,这是我第一次尝试在 powershell 中编写脚本,所以如果我做的事情很长,我愿意接受一些输入。

    # Import ActiveDirectory module for Powershell V2 AD cmdlets
    import-module activedirectory
    # Uncomment the following line to include optional cmdlets included with Exchange  2010 schema changes. No such cmdlets are included in this script
    # add-pssnapin microsoft.exchange.management.powershell.e2010 

    #Import the maximum password age from Active Directory GPO policy from domain
    $maxdays=(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays
    $date = date

    # Simple HTML Message to format body of email. Body is broken up into four parts for appearance and for easy function insertion into message.
    $body1 +=   "<html><body><br> Your network password will expire in "
    $body2 +=   " day(s).</body><html>"
    $body3 +=   "<html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password."
    $body3 +=   "<br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website."
    $body3 +=   "<br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309"
    $body3 +=   "<br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also."
    $body3 +=   "<br><br>Thank you,"
    $body3 +=   "<br> IS Department"
    $body3 +=   "<br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>"
    $body3 +=   "<br><br><hr>"
    $body3 +=   "From <b> IS Department</b>"
    $body3 +=   "<br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is"
    $body3 +=   "<br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this"
    $body3 +=   "<br>communication, or any part thereof, is strictly prohibited and may be unlawful.  The IT Department thanks you for your cooperation.<br>"
    $body4 +=   "<br><hr><br></body></html>"

    # Combine body segments into string for display
    $bod1y=$body1 | out-string 
    $body2=$body2 | out-string 
    $body3=$body3 | out-string 
    $body4=$body4 | out-string 

    #Gather ADusers which are enabled, password is set not set to never expire and all properties of user object. *Note Extension Attributes will not show up unless they are populated.
    (Get-ADUser -filter {(Enabled -eq "True") -and (PasswordNeverExpires -eq "False")} -properties *) | Sort-Object pwdLastSet |

    #Loop to validate password age of each account and generate email. Emails to non-domain addresses are generated based on extensionattribute1 and extensionattribute2. 
    #Active Directory is pre-populated with the user address as extensionattribute1 and domain information in extensionattribute2. For example, johndoe = extensionattribute1
    # gmail.com = extensionattribute2.
    foreach-object {
    $lastset=Get-Date([System.DateTime]::FromFileTimeUtc($_.pwdLastSet))
    $expires=$lastset.AddDays($maxdays).ToShortDateString()
    $daystoexpire=[math]::round((New-TimeSpan -Start $(Get-Date) -End $expires).TotalDays)
    $samname=$_.samaccountname
    $firstname=$_.GivenName
    $lastname=$_.SN
    $extensionattribute1=$_.extensionattribute1
    $extensionattribute2=$_.extensionattribute2
    $recipient="$extensionattribute1@$extensionattribute2"
                if (($daystoexpire -ge 1) -and ($daystoexpire -le 10)) {
                $ThereAreExpiring=$true

                $email = @{
                to = "$recipient" 
                from = 'IS_Notifications@org.org'
                subject = "$firstname $lastname your network password will expire in $daystoexpire day(s)"
                body = "$firstname $lastname" +  " $body1" + "$daystoexpire" + "$body2" + "$body3" + "$date" + "$body4"
                smtpserver = 'smtp.server.org'
                # attachments = "p:\documents\citrix\citrix_password_reset.doc"
            }
        Send-MailMessage @email  -BodyAsHTML
        }

}`

【问题讨论】:

    标签: html email powershell fonts colors


    【解决方案1】:

    你看过Powershell的“这里字符串”功能吗?有一个technet article that discusses the feature。我一直将它们用于作为东西模板的字符串。

    我喜欢在此类模板中使用 {0} 等 c# 样式的占位符。这允许对日期和货币进行花哨的格式化。 (在我的示例中,我使用了“花式”日期格式。)

    使用带有占位符的模板还意味着我不必记住以特定顺序将字符串连接在一起,或者记住像 $firstname 这样的东西必须在这些连接中的什么位置。它也应该更容易国际化,但我从未这样做过。

    这是一个快速示例,您需要将其集成到您的循环逻辑中。

    # first, stick the template into a variable for later use. Use a "here string" for ease of formatting and editting.
    $bodyTemplate = @"
    {0} {1}
    <html><body><br> Your network password will expire in {2} day(s).</body><html>
    <html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password.
    <br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website.
    <br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309
    <br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also.
    <br><br>Thank you,
    <br> IS Department
    <br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>
    <br><br><hr>
    From <b> IS Department</b>
    <br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is
    <br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this
    communication, or any part thereof, is strictly prohibited and may be unlawful.  The IT Department thanks you for your cooperation.<br>
    {3:D}
    <br><hr><br></body></html>
    "@
    
    # Now, loop through your users, calculate $DaysUntilExpiry, test the value and build the email to be sent
    # I'm just making up some dumb values here
    $daystoexpire = 42 # or whatever
    $firstname =  "George"
    $lastname = "Washington"
    $date = date
    
    # using the template, stick the appropriate values into place and store that in a variable for convenience
    $body = $bodyTemplate -f $firstname, $lastname, $daystoexpire, $date
    
    # do whatever we want with $body
    write-host $body
    

    【讨论】:

    • 感谢您向我指出这一点。它进行了一些调整。双引号和 # 结合起来将句子的其余部分注释掉,但是我将代码更改为 并且它起作用了。还感谢有关使用 c# 占位符的提示。这在未来的脚本中将非常有用。
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2012-03-18
    • 1970-01-01
    • 2013-10-01
    • 2014-10-24
    • 2014-03-01
    相关资源
    最近更新 更多