Reputation: 917
Re-edited my question to try and make it more clear - version 2 :-)
I have the following code in a disk module within a function which uses the Get-WmiObject which pushes the data into a $LogMsg string which intern is then used in a LogToEmail function which is in another module (LogToEmail "Drive space is low" $LogMsg;)
if ($AlertPercent -gt 0)
{
$filter = "DeviceID='" + $DriveLetter + "'";
$body = (Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ServerName -Filter $filter |
Where-Object {$_.DriveType -eq 3 -and ((($_.FreeSpace/$_.Size*100) -as [float]) -lt $AlertPercent)} |
Sort-Object -Property Name |
Select-Object @{"Label"="Server";"Expression"={"{0:N}" -f ($_.SystemName)}},
Name,
VolumeName,
FileSystem,
Description,
VolumeDirty,
@{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}},
@{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},
@{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} |
Format-Table -AutoSize | Out-String);
}
else
{
$filter = "DeviceID='" + $DriveLetter + "'";
$body = (Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ServerName -Filter $filter |
Where-Object {$_.DriveType -eq 3} |
Sort-Object -Property Name |
Select-Object @{"Label"="Server";"Expression"={"{0:N}" -f ($_.SystemName)}},
Name,
VolumeName,
FileSystem,
Description,
VolumeDirty,
@{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}},
@{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},
@{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} |
Format-Table -AutoSize | Out-String);
}
if ($body.Length -gt 0)
{
#Set the log message
$LogMsg = "The following drive '" + $pDriveLetter + "', on server '" + $ServerName + "', is low on space.`n" + $body.Trim();
#Log the stopped service(s) to the log file
LogToFile $LogMsg;
#Log the stopped service(s) to the host screen
LogToHost $LogMsg;
#Log the stopped service(s) to the email receipient
LogToEmail "Drive space is low" $LogMsg;
}
#Set log information message
$LogInfoMsg = "Ended checking drive space on server:" + $ServerName;
#Log the date and time started checking searched service(s) on the server
LogToFile $LogInfoMsg
#Log the date and time started checking searched service(s) on the server
LogToHost $LogInfoMsg
}
catch
{
#write out the error into a string format
$LogMsg = "- ERROR: " + ($_.Exception.ErrorRecord | Out-String);
#Log the error message to the log file
LogToFile $LogMsg;
#Log the error message to the host screen
LogToHost $LogMsg;
#Log, the error message to the email recipient
LogToEmail ("PowerShell error on " + (split-path $MyInvocation.PSCommandPath -leaf)) $LogMsg;
}
The $LogMsg is then passed to the log module as
$Message = New-MimeMessage $global:EmailFrom $global:EmailTo $Subject $LogMsg $global:EmailCC
and then to the email module as $Body as a string parameter in the New-MimeMessage function which is supposed to convert it to HTML via ConvertTo-HTML functionality.
$BodyAsHtml = $global:BodyAsHTML
if ($BodyAsHtml)
{
$TextPart = New-Object MimeKit.TextPart("html")
#$RetConstructedMessage = $true
}
else
{
$TextPart = New-Object MimeKit.TextPart("plain")
}
$Header = "
<style>
DIV {font-family:'courier', monospace}
</style>
"
$BodyHTML = ConvertTo-Html -Head $Header -Body $Body | Out-String
$TextPart.Text = $BodyHTML
However, the ConvertTo-Html is not encoding the new line/carriage returns even though the debugger in Visual Studio Code seems to show correctly but via the browser on GMail or on Microsoft Outlook after the email has been sent, it shows as a continuous one-liner. I don't do any HTML coding to force line breaks where I want them as the Get-WmiObject should have them in as plain text places the new lines and/or carriage returns. I have included images to try and help what I am seeing.
Mousing over $BodyHTML string variable in Visual Studio Code debugger
The email view on the browser on GMail
The email view on the browser on Microsoft Outlook
Upvotes: 0
Views: 322
Reputation: 61228
As commented, you need to remove the | Format-Table -AutoSize | Out-String
when you create the $body
variable.
This will return a table-style string as formatted for the console screen only.
You can check with $body.GetType().FullName
, which because of the | Format-Table -AutoSize | Out-String
will return System.String
If you leave that out, $body.GetType().FullName
shows it is a System.Object[]
This is what you need for the ConvertTo-Html
so it can create a HTML-style table from that data.
If you need the Format-Table in the log, use a new variable to get the table format you want in there, or have this long line:
$LogMsg = "The following drive '$pDriveLetter', on server '$ServerName', is low on space.`r`n{0}" -f ($body | Format-Table -AutoSize | Out-String).Trim()
Upvotes: 1