Html 变量上的Null参数
我正在尝试使用PowerShell运行MySQL脚本,并将输出转换为HTML文件或通过电子邮件发送输出。 但是,存储输出的变量有问题 代码运行得很好,因为我能够输出结果,但它无法移动到HTML或电子邮件Html 变量上的Null参数,html,mysql,powershell,variables,Html,Mysql,Powershell,Variables,我正在尝试使用PowerShell运行MySQL脚本,并将输出转换为HTML文件或通过电子邮件发送输出。 但是,存储输出的变量有问题 代码运行得很好,因为我能够输出结果,但它无法移动到HTML或电子邮件 #The dataset must be created before it can be used in the script: $dataSet = New-Object System.Data.DataSet #MYSQL query $command = $myconnection.
#The dataset must be created before it can be used in the script:
$dataSet = New-Object System.Data.DataSet
#MYSQL query
$command = $myconnection.CreateCommand()
$command.CommandText = "
SELECT ID, Date_Close, Time_Close FROM systemcontrol.database_close
WHERE Date_Close >= CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-01')
AND Database_Close_ID = 1
ORDER BY Date_Close DESC
";
Write-Host "4B - Sales Reports Month End Database"
$reader = $command.ExecuteReader()
#The data reader will now contain the results from the database query.
#Processing the Contents of a Data Reader
#The contents of a data reader is processes row by row:
while ($reader.Read()) {
#And then field by field:
for ($i= 0; $i -lt $reader.FieldCount; $i++) {
Write-Output $reader.GetValue($i).ToString()
}
}
ConvertTo-Html -Body "$reader" -Title "4B - Sales Reports Month End Database" | Out-File C:\************.HTML
Send-MailMessage -From " Daily Check <server@company.com>" -To "Admin <admin@admin>" -Subject "Daily Check: Server Times" -Body "$reader" -Priority High -Dno onSuccess, onFailure -SmtpServer 1.xx.xx.xx
$myconnection.Close()
这就是我得到的错误:
无法验证参数“Body”上的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令
它似乎无法识别$reader变量。这里哪里出错了?您正在向-body参数传递一个SqlDataReader对象,该参数需要字符串[]。只需在$result数组中收集while循环中的值,并将其传递给主体:
#The dataset must be created before it can be used in the script:
$dataSet = New-Object System.Data.DataSet
#MYSQL query
$command = $myconnection.CreateCommand()
$command.CommandText = "
SELECT ID, Date_Close, Time_Close FROM systemcontrol.database_close
WHERE Date_Close >= CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-01')
AND Database_Close_ID = 1
ORDER BY Date_Close DESC
";
Write-Host "4B - Sales Reports Month End Database"
$reader = $command.ExecuteReader()
#The data reader will now contain the results from the database query.
$result = @()
#Processing the Contents of a Data Reader
#The contents of a data reader is processes row by row:
while ($reader.Read()) {
#And then field by field:
for ($i= 0; $i -lt $reader.FieldCount; $i++) {
$value = $reader.GetValue($i).ToString()
Write-Output $value
$result += $value
}
}
ConvertTo-Html -Body $result -Title "4B - Sales Reports Month End Database" | Out-File C:\************.HTML
Send-MailMessage -From " Daily Check <server@company.com>" -To "Admin <admin@admin>" -Subject "Daily Check: Server Times" -Body "$reader" -Priority High -Dno onSuccess, onFailure -SmtpServer 1.xx.xx.xx
$myconnection.Close()