Ricky Ricky - 2 months ago 6
HTML Question

Null argument on variable

I'm trying to use PowerShell to run a MySQL script and convert the output to an HTML file or e-mail the output.
However I'm having a trouble with the variable which stores the output.

The code works fine, as I'm able to output the results, it just fails at moving to HTML or e-mail.

#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()


This is the error I'm getting:


Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.


It doesn't seem to recognise the
$reader
variable. Where am I going wrong here?

Answer

You are passing a SqlDataReader object to the -body parameter which is expecting a string[]. Just collect the values in your while loop in a $result array and pass that to the body:

#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()