【问题标题】:Sending output via Email from Powershell从 Powershell 通过电子邮件发送输出
【发布时间】:2018-11-01 16:42:34
【问题描述】:

See attached photo (link) of Output in PowerShell and Outlook.

我正在尝试通过电子邮件发送输出,但无法让脚本将输出放入电子邮件正文。它不是抓取介于所需阈值(70 天)之间的服务名称和到期日期,而是抓取 CSV 中的所有内容(例如,查看 2022 年到期的服务)并将其放入电子邮件正文中。

我的问题是我如何让它仅通过电子邮件发送我在 PowerShell 中提取的信息(在指定阈值日期之间的信息)?

有点新的脚本,谢谢! :)

$now=get-date
$cert = Import-Csv C:\Users\userID\Desktop\Certificate_CSV2.csv
$threshold = 70  
$deadline = (Get-Date).AddDays($threshold) 
$cert | Select-Object "Service Name", "Certificate Expiration Date" | Where-Object {$_."Certificate Expiration Date" -as [datetime] -le $deadline} 

Send-mailMessage -to "SentTo@email.com" -subject "test message 8" -from "SentTo@email.com" -body ($cert | Out-String) -SmtpServer pobox.email.com

【问题讨论】:

    标签: powershell email output send


    【解决方案1】:

    您只是使用过滤器打印出证书,而不是将其应用回证书。要解决此问题,只需将 $cert 设置为等于其自身的过滤版本即可。

    $cert = $cert | Select-Object "Service Name", "Certificate Expiration Date" | Where-Object {$_."Certificate Expiration Date" -as [datetime] -le $deadline} 
    

    【讨论】:

    • 就是这样!!谢谢你:)
    • 有什么方法可以对齐电子邮件本身的输出? (右、左或中)我尝试使用 -BodyAsHTML 但这会将所有内容组合在一起。这比任何东西都更美观......感谢所有帮助
    • 我想你想使用 -BodyAsHTML 但也使用
       标签来保留间距。所以你必须按照-body "<pre>$($cert | Out-String)</pre>" -BodyAsHTML 的方式做一些事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多