我正在尝试将SQL(invoke-sqlcmd)查询的输出添加到我的Powershell脚本发送的电子邮件正文中。
sql查询工作正常,电子邮件工作正常。我无法弄清楚如何将结果添加到电子邮件正文中。
function SendEmail
{
invoke-sqlcmd -Query "SELECT 'Sql {Select} Query" | format-table
$MsgBody = "All files have been downloaded correctly for " + $Date
$smtp = new-object Net.Mail.SmtpClient($EmailSMTPServer)
$credentials=new-object system.net.networkcredential("MailServer","Password")
$smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
$smtp.Send($emailFrom, $emailTo, $subject,$MsgBody)
}
感谢您的帮助。
答案 0 :(得分:3)
function SendEmail
{
$Query=invoke-sqlcmd -Query "SELECT 'Sql {Select} Query" | out-string
$MsgBody = "All files have been downloaded correctly for " + $Date + "`n"
$MsgBody += $Query
$smtp = new-object Net.Mail.SmtpClient($EmailSMTPServer)
$credentials=new-object system.net.networkcredential("MailServer","Password")
$smtp.credentials=$credentials.getcredential($smtpserver,"25","basic")
$smtp.Send($emailFrom, $emailTo, $subject,$MsgBody)
}
以上似乎已经成功了。我用out-string替换了Format-table。