Setting HTML font color in Powershell email

后端 未结 1 887
孤独总比滥情好
孤独总比滥情好 2021-01-27 03:08

I have a powershell script that will generate an email for users whose password will expire in <=10 days. The email is formatted into HTML, but I want to change the font colo

相关标签:
1条回答
  • 2021-01-27 03:33

    Have you looked at the "here string" feature of Powershell? There is a technet article that discusses the feature. I use them all of the time for strings that are templates for stuff.

    I like to use c#-style placeholders like {0} in such templates. This allows for fancy formatting of dates and currency. (I am using a 'fancy' date format in my example.)

    Using a template with placeholders also means I don't have to remember to concatenate strings together in a particular order or keep in mind where stuff like $firstname has to go within those concatenizations. It's also supposed to be more easily internationalized, but I've never done that.

    Here is a quickie example, you would need to integrate it into your looping logic.

    # 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
    
    0 讨论(0)
提交回复
热议问题