windows - powershell 和使用脚本接收邮件的问题
问题描述
我是powershell的新手。:( 但是我正在使用带有 gmail 邮件服务器的 powershell 配置邮件通知,一切正常,但我想在邮件正文中添加脚本的输出。
$username = 'test@gmail.com'
$password = '*****'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$body = C:\Users\esanchez\Desktop\script.ps1
$hash = @{
from = "test@gmail.com"
to = "receptor@gmail.com"
subject = "test"
smtpserver = "smtp.gmail.com"
port = "587"
body = GU
credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
usessl = $true
verbose = $true
}
Send-MailMessage -body $body @hash
解决方案
是否C:\Users\esanchez\Desktop\script.ps1
包含图像中显示的脚本?如果是这样,我认为你很接近。只需将该行更改为包含| Out-String
或| ConvertTo-Html -Fragment
在末尾,以便它将您的输出转换为您可以在正文中发送的内容,然后更新正文$hash
以使用$body
。最后修复实际的命令行
$username = 'test@gmail.com'
$password = '*****'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$body = C:\Users\esanchez\Desktop\script.ps1 | Out-String # or | ConvertTo-Html -Fragment
$hash = @{
from = "test@gmail.com"
to = "receptor@gmail.com"
subject = "test"
smtpserver = "smtp.gmail.com"
port = "587"
body = $body # update to use $body
credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
usessl = $true
verbose = $true
}
Send-MailMessage @hash # remove body parameter here since you are already passing it in with the hashtable
推荐阅读
- google-sheets - 使用间接函数来引用查询函数的串联导入范围
- python - 在字符串上应用正则表达式模式列表并返回找到的匹配项
- facebook-graph-api - Facebook Graph API Explorer 没有给我位置
- ruby - 替换 URL 字符串中的主机名
- android - 包含 proteus 库时出错 - “android.support.v7.appcompat.R$layout”
- wpf - 在 WPF 中使用 DevExpress ChartControl 和数据网格
- nginx - Nginx反向代理后面的Qtorrent Web GUI未加载登录网页css
- ios - 填充集合视图内的图像视图
- python-3.x - PYQT Selected Combobox 项目应删除另一个组合框中的项目
- visual-studio - 没有匹配的配置文件 [对于转移的应用程序]