You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改PowerShell脚本:将服务状态以表格形式嵌入邮件正文

修改后的PowerShell脚本(邮件正文显示服务状态表格)

要让邮件正文显示表格格式的服务状态,需要将服务数据转换为HTML格式,同时给Send-MailMessage添加-BodyAsHtml $true参数来支持HTML内容。以下是修改后的完整脚本:

# 获取服务数据并转换为HTML表格
$serviceTable = Get-Service -DisplayName Windows* |
    Select-Object -Property DisplayName, Status, Description |
    ConvertTo-Html -Property DisplayName, Status, Description -PreContent "<h3>Windows相关服务状态报告</h3>" -PostContent "<p>请勿回复此邮件</p>" |
    Out-String

# 保存服务状态到TXT文件(保留原功能)
Get-Service -DisplayName Windows* |
    Select-Object -Property DisplayName, Status, Description |
    Out-File C:\Users\ABC\Downloads\Report\ServiceStatus.txt

# 发送邮件,正文使用HTML表格
Send-MailMessage -From ABC@Gmail.com `
    -Subject "Services Status" `
    -To XYZ@gmail.com `
    -Attachments C:\Users\ABC\Downloads\Report\ServiceStatus.txt `
    -Body $serviceTable `
    -BodyAsHtml $true `
    -DeliveryNotificationOption OnFailure,OnSuccess,Delay `
    -Port 25 -Priority High -SmtpServer 11.19.17.78

关键修改说明

  • 使用ConvertTo-Html将服务对象转换为HTML表格,通过-PreContent和-PostContent添加标题和提示文本
  • 用Out-String把HTML输出转为字符串,赋值给变量$serviceTable作为邮件正文
  • 添加-BodyAsHtml $true参数,让邮件客户端解析HTML内容,从而显示表格格式
  • 保留了原脚本中保存TXT文件和附件发送的功能

内容的提问来源于stack exchange,提问作者SV12 VS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:17:06