在 Powershell 电子邮件中设置 HTML 字体颜色

2023-12-14

我有一个 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 = '[email protected]'
                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
        }

}`


您看过 Powershell 的“此处字符串”功能吗?有一个讨论该功能的 technet 文章。我一直将它们用于作为东西模板的字符串。

我喜欢在此类模板中使用 C# 风格的占位符,例如 {0}。这允许对日期和货币进行精美的格式化。 (我在示例中使用了“奇特”日期格式。)

使用带有占位符的模板还意味着我不必记住以特定顺序将字符串连接在一起,也不必记住像 $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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Powershell 电子邮件中设置 HTML 字体颜色 的相关文章

  • 文件和目录条目 API 在 Chrome 中损坏?

    我正在尝试使用文件和目录条目 API 创建一个文件上传器工具 该工具允许我将文件和目录的任意组合放入浏览器窗口中 以供读取和上传 我完全意识到 可以通过使用文件输入元素来实现类似的功能webkitdirectory已启用 但我正在测试一个用
  • 为什么smtpclient发送的邮件没有出现在已发送项目中

    我已经实现了一个通过 Net SmtpClient 发送电子邮件的服务器 邮件发送代码如下 private static MailMessage SendMail string to string subject string body M
  • 从 Google Chrome v20 中的日期输入中删除背景箭头

    自 Google Chrome v20 起 已添加新的日历来输入日期 问题是我正在使用 javascript 创建自己的日历 并且我的图标已经位于与默认镶边箭头相同的位置 我想知道如何删除箭头背景 据我所知 您目前无法禁用它 这里正在进行一
  • HTML Viber 链接到特定号码

    我需要帮助在我的应用程序中实施 Viber 号码 它应该直接使用 Viber 聊天选项连接用户 并且应该在需要发送消息的地方添加特定号码 可以通过 HTML 来做到这一点吗 还有什么其他方法呢 要打开与用户的 Viber 聊天 a href
  • 带有图像垂直对齐的内联框:与父框居中

    请运行演示 margin 0 padding 0 body font family Microsoft Yahei font size 16px background color lightblue height 200px width 2
  • 如何更改 .NET MAUI Blazor 项目中的默认字体?

    我有一个用于 NET MAUI Blazor 应用程序的默认 Visual Studio 项目 针对 Windows x64 构建 我尝试了两种不同的更改字体的方法 在MauiProgram cs中有一个字体的配置ConfigureFont
  • 为什么我会收到此 Javascript 运行时错误?

    我的网页上有以下 JavaScript 64 var description new Array 65 description 0 66 description 1 78 function init 79 document getEleme
  • jquery html() 默认解码 html 实体?

    我不知道为什么 jquery html 会这样做 但是在我这样做之后 html html 我得到 copy 自动转换为 无论如何要避免这种情况 我需要使用 javascript 转储页面的 html 并且不需要这种破坏 html 的无用转换
  • 如何像facebook和twitter一样显示帖子更新时间?

    我正在制作一个通知平台 而不是显示通知到来的时间 我想显示自通知到达以来经过的时间 就像在 Twitter 和 Facebook 上发生的那样 他们显示 32m 1 小时前等 我想用JavaScript Java来实现这段代码 任何帮助将非
  • 发送python电子邮件时添加excel文件附件

    使用 python 发送电子邮件时如何添加文档附件 我收到要发送的电子邮件 请忽略 我正在循环发送电子邮件以每 5 秒发送一次 仅用于测试目的 我希望它每 30 分钟发送一次 只需将 5 更改为 1800 到目前为止 这是我的代码 如何附加
  • 如何使用 php 处理传出 webhook (Slack)

    我已经配置了 Slack outgoing webhook 但我不确定如何处理 Slack 发送到我指定的 URL 的 HTTP POST 请求 工作流程是这样的 当有人向指定通道发送消息时 API 将向指定 URL 之一发送 HTTP P
  • CSS:如何在“div”内垂直对齐“标签”和“输入”?

    考虑以下代码 http jsfiddle net s2qBw 3 HTML div div
  • 滚动时静态标题

    我编写了一个主页 现在我正在尝试构建一个标题 该标题是静态的 就像在滚动内容时不滚动一样 当内容位于其下方时 它应该保持在顶部 我找到了这个解决方案here https stackoverflow com questions 9677894
  • PHP - 发送带有附件的电子邮件不显示消息内容

    尝试创建一个脚本 我可以在其中发送带有附件的电子邮件 一切正常 除了当我不在电子邮件中添加文件时 我仍然可以看到带有 0B 且没有名称的附件 if isset POST my send email to POST my email to r
  • 当我尝试转发电子邮件时,时事通讯无法隐藏 Gmail 上的响应内容

    我正在尝试写一份时事通讯 但当我测试时 我无法隐藏响应式内容GMail On Outlook and Yahoo一切正常 但如果我尝试转发电子邮件 隐藏的内容就会可见 我测试使用 putsmail https putsmail com gt
  • 禁用任何类型的浏览器窗口滚动?

    有没有办法禁用滚动 不仅仅是滚动条 还有浏览器窗口的全部功能 根据您对 Keit 的回答 您不想在打开灯箱时滚动处于活动状态 如果是这种情况 您可以使用以下 css 在打开灯箱的同时向正文添加一个类 这个解决方案的好处是它保留了滚动 空间
  • 如何使用 PHP 从 MySQL 查询中按升序对值进行排序?

    我使用以下 PHP 脚本从 MySQL 表中获取和更改数据 并将结果打印在 HTML 表中 我希望按升序对数据进行排序 utilization percentage变量 它是由创建的 total client time total avai
  • Python 中的颜色处理

    对于我的聚类 GUI 我目前对聚类使用随机颜色 因为我事先不知道最终会得到多少个聚类 在 Python 中 这看起来像 import random def randomColor return random random random ra
  • Javascript / jQuery - 转换特殊 html 字符

    我有一个pre元素中包含一些 html 代码 该代码中有特殊字符 例如 lt 所以它不会破坏页面 然后我有一个 javascript 函数 它获取此 pre 元素的内容 突出显示它 使用 codemirror 并用突出显示的文本替换元素内容
  • html5 canvas 使用图像作为蒙版

    是否可以使用具有形状的图像作为整个画布或画布内图像的蒙版 我想将图像放置在画布中 并在图像上添加蒙版 然后将其另存为新图像 您可以使用 source in globalCompositeOperation 将黑白图像用作蒙版 首先 将蒙版图

随机推荐