【问题标题】:New lines and/or carriage returns not getting converted新行和/或回车未转换
【发布时间】: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 应该将它们作为纯文本放置新行和/或回车符。我已包含图像以尝试帮助我所看到的。

在 Visual Studio Code 调试器中将鼠标悬停在 $BodyHTML 字符串变量上

GMail 浏览器上的电子邮件视图

Microsoft Outlook 浏览器上的电子邮件视图

【问题讨论】:

  • HTML 中会丢失换行符,除非有&lt;br&gt;&lt;pre&gt; 标签?
  • 感谢您回复@vonPryz。我重新编辑了我的问题。希望它更有意义。
  • 在创建$body 时删除| Format-Table -AutoSize | Out-String。这将返回一个表格样式的字符串,其格式仅为控制台屏幕。 ConvertTo-Html 无法从中创建 HTML 样式表。如果您需要在日志中显示格式表,请为此创建一个变量,特别是用于日志记录,但保留 $body 对象数组。

标签: powershell


【解决方案1】:

如评论所述,您需要在创建 $body 变量时删除 | Format-Table -AutoSize | Out-String
这将返回一个仅针对控制台屏幕格式化的表格样式字符串。 您可以使用$body.GetType().FullName 进行检查,因为| Format-Table -AutoSize | Out-String 将返回System.String

如果你忽略它,$body.GetType().FullName 会显示它是 System.Object[]

这是ConvertTo-Html 所需要的,因此它可以根据该数据创建 HTML 样式的表格。

如果您需要日志中的 Format-Table,请使用新变量在其中获取您想要的表格格式,或者有这么长的一行:

$LogMsg = "The following drive '$pDriveLetter', on server '$ServerName', is low on space.`r`n{0}" -f ($body | Format-Table -AutoSize | Out-String).Trim()

【讨论】:

  • 非常感谢西奥。我尝试了所有不同的事情,但都是错误的。我只需要移动一些东西,但就像一个魅力...... :-)
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
相关资源
最近更新 更多