gpt4 book ai didi

powershell - 将 sql 查询的结果集添加到 Powershell 中的电子邮件正文

转载 作者:行者123 更新时间:2023-12-02 23:28:31 28 4
gpt4 key购买 nike

我正在尝试将 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)
}

感谢您的帮助。

最佳答案

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。

关于powershell - 将 sql 查询的结果集添加到 Powershell 中的电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com