【发布时间】:2020-05-08 09:26:38
【问题描述】:
重新编辑了我的问题以尝试使其更清晰 - 版本 2 :-)
我在一个函数中的磁盘模块中有以下代码,该函数使用 Get-WmiObject 将数据推送到 $LogMsg 字符串中,然后将实习生用于另一个模块中的 LogToEmail 函数(LogToEmail“驱动器空间是低" $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;
}
$LogMsg 然后传递给日志模块
$Message = New-MimeMessage $global:EmailFrom $global:EmailTo $Subject $LogMsg $global:EmailCC
然后将 $Body 作为 New-MimeMessage 函数中的字符串参数传递给电子邮件模块,该函数应该通过 ConvertTo-HTML 功能将其转换为 HTML。
$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
但是,即使 Visual Studio Code 中的调试器似乎正确显示,但在发送电子邮件后通过 GMail 或 Microsoft Outlook 上的浏览器,ConvertTo-Html 并未编码新行/回车符,它显示为连续的单线。我不做任何 HTML 编码来强制换行,因为 Get-WmiObject 应该将它们作为纯文本放置新行和/或回车符。我已包含图像以尝试帮助我所看到的。
【问题讨论】:
-
HTML 中会丢失换行符,除非有
<br>或<pre>标签? -
感谢您回复@vonPryz。我重新编辑了我的问题。希望它更有意义。
-
在创建
$body时删除| Format-Table -AutoSize | Out-String。这将返回一个表格样式的字符串,其格式仅为控制台屏幕。ConvertTo-Html无法从中创建 HTML 样式表。如果您需要在日志中显示格式表,请为此创建一个变量,特别是用于日志记录,但保留$body对象数组。
标签: powershell