Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在电子邮件正文中的另一个变量内显示变量的值_Mysql_Database_Powershell - Fatal编程技术网

Mysql 在电子邮件正文中的另一个变量内显示变量的值

Mysql 在电子邮件正文中的另一个变量内显示变量的值,mysql,database,powershell,Mysql,Database,Powershell,我收到的电子邮件正文中的最终结果是:“System.Data.DataSet” 如何强制它在电子邮件正文中显示$ds.Tables中的值 我试过: --Connect and open MYSQL Connection to the database $strUser = "X" $strPW = "Y" $strDB = "Z" $strserver = "dataserver" [void][System.Reflection.ASsembly]::LoadWithPartialName(

我收到的电子邮件正文中的最终结果是:“System.Data.DataSet” 如何强制它在电子邮件正文中显示$ds.Tables中的值

我试过:

--Connect and open MYSQL Connection to the database
$strUser = "X"
$strPW = "Y"
$strDB = "Z"
$strserver = "dataserver"

[void][System.Reflection.ASsembly]::LoadWithPartialName("MySql.Data")
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection
$cs = "server=$($strserver);port=3306;database=$($strdb);uid=$($struser);pwd=$($strPW)"
$conn.connectionstring = $cs
$conn.Open()

$query = "select * from users"


$cmd = new-object MySql.Data.MySqlClient.MySqlCommand($StoresNotPolledToday,$conn)
$da = new-object MySql.Data.MySqlClient.MySqlDataAdapter($cmd)
$ds = New-Object System.Data.DataSet
$recordcount = $da.Fill($ds, "users")

--Display the values from the query
$ds.Tables



-- Compile the e-mail
$From = "test@gmail.com"
[string[]]$To = "Me@gmail.com"
$Subject = "Test Subject"
$Body = "Test Body and text
$ds.Tables "

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$password = "Password1" | ConvertTo-SecureString -asPlainText -Force
$username = "test@gmail.com"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $credential 
然后将$var放入电子邮件的$body中

$var = $ds.Tables

我在正文中收到的结果是:“用户”作为正文

我没有mysql db可供测试,但您需要将$ds.tables转换为一个对象,该对象至少可以转换为一个HTML片段,如果不能,则仅转换为整个HTML页面:

$Body = "Test Body and text
    $var "

如果这不能正确地显示数据(如果PowerShell这样做,它将代表您进行大量数据转换),则需要获取.Tables集合对象并将其转换为可转换为HTML片段的对象:

我通常通过先将基本集合对象转换为数组,然后执行转换来完成这类工作

$ds.tables | Convertto-Html -Fragment
编辑1:

此外,在Send-MailMessage cmdlet上,您还需要使用属性-BodyAsHtml$true

编辑2:

以下是我建议的更清晰的编辑:

@($ds.tables) | Covnertto-Html

应该是这样的吗?错误:param.not found接受参数'true'$ds.tables | Convertto HTML--编译电子邮件:$Subject=“Test Subject”$Body=“Test Body$ds.tables”$SMTPServer=“smtp.gmail.com”$SMTPPort=“587”$password=“Password1”| Convertto SecureString-asPlainText-Force$username=”test@gmail.com"$credential=New Object System.Management.Automation.PSCredential($username,$password)发送邮件消息-从$From-到$to-Subject$Subject`-Body$Body-BodyAsHtml$true-SmtpServer$SmtpServer-port$SMTPPort-usesl`-credential$credential@AtanasS. 请尝试编辑2中的更改,这会显示“由于未指定SMTP服务器,因此无法发送电子邮件”以及“术语'-Body'未被识别为cmdlet的名称”。无论如何,我设法将变量放入txt文件中,然后将.txt文件作为$arr=@($ds.Tables)插入正文中写入输出$arr | out file-filepath C:\test.txt,然后在body:$body=[IO.file]::ReadAllText(“C:\test.txt”)这对我来说很有效。谢谢,这是因为我在你的回帖后面加了我的评论。删除我的评论,你会很好——我的评论是带有双哈希标记(###)的。
@($ds.tables) | Covnertto-Html
$arr = @($ds.Tables)  ## Have PowerShell change the type from a probably SqlDataTable to an Array



-- Compile the e-mail
$From = "test@gmail.com"
[string[]]$To = "Me@gmail.com"
$Subject = "Test Subject"
$Body = ($arr | ConvertTo-Html)  ## Convert to the array to an HTML page
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$password = "Password1" | ConvertTo-SecureString -asPlainText -Force
$username = "test@gmail.com"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Send-MailMessage -From $From -to $To -Subject $Subject -BodyAsHtml $true ` ## Added the flag to allow the email to be sent in HTML format
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $credential