我正在尝试使用PowerShell运行MySQL脚本并将输出转换为HTML文件或通过电子邮件发送输出。 但是我遇到了存储输出的变量的问题。
代码工作正常,因为我能够输出结果,它只是无法移动到HTML或电子邮件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryWork
{
class Program
{
static void Main(string[] args)
{
var bookList = new List<string>();
string ansSearch = String.Empty;
string search = String.Empty;
int i = 1;
for (int zero = 0; i > zero; i++)
{
Console.Write("Type ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("'New'");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" if you would you like to enter a new book. Type ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("'List' ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("to see a list of books entered. Type ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("'Search' ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("to look up a specific book.");
Console.Write(" And if you want to exit. Type ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("'Stop'.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
string answer = Console.ReadLine();
if (answer == "Stop")
{
return;
}
if (answer == "New")
{
Console.Write("Please format the Entry of your book as follows: ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("'Name of the Book',");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("'Author (first, last)',");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("'Category',");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write("'Dewey Decimal Number'.");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
bookList.Add("Entry " + i + ": " + Console.ReadLine());
continue;
}
if (answer == "List")
{
bookList.ForEach(Console.WriteLine);
Console.WriteLine("Press enter to continue");
Console.ReadLine();
i--;
continue;
}
if (answer == "Search")
{
Console.WriteLine("What would you like to search for (Title: Full Title; Author: first, last): ");
search = Console.ReadLine();
var results = bookList.Where(x => x.Contains(search)).ToList();
bool isEmpty = !results.Any();
if (isEmpty)
{
i--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Sorry, we could not find that.");
Console.ForegroundColor = ConsoleColor.White;
continue;
}
foreach (var result in results)
{
Console.WriteLine(result);
}
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
results.Clear();
i--;
continue;
}
i--;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Incorrect Response, please try again");
Console.ForegroundColor = ConsoleColor.White;
}
}
}
}
这是我得到的错误:
无法验证参数'Body'的参数。参数为null或空。提供非null或空的参数,然后再次尝试该命令。
似乎无法识别#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()
变量。我在哪里错了?
答案 0 :(得分:1)
您正在将SqlDataReader
对象传递给期望-body
的{{1}}参数。只需在string[]
数组中收集while循环中的值,然后将其传递给正文:
$result