首页 > 解决方案 > 使用 powershell 将 .md 文件的内容作为 Outlook 的电子邮件正文

问题描述

我想获取 .md 文件的内容并将其显示在生成的 Outlook 电子邮件正文中。我可以生成只是找到的电子邮件,但正文给出了以下错误,我还没有找到解决方法。

错误:

The object does not support this method.
At line:6 char:1
+ $new.HTMLBody = $a
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

代码:

$out= New-Object -comObject Outlook.Application
# $sign= Get-Content "C:\Users\Roaming\Microsoft\Signatures\sign.htm"
$recipient= "user@.com"
$new= $out.CreateItem(0)
$new.Subject = "Meeting details"
$a = Get-Content -Path "c:\temp\file.md"
$new.HTMLBody = $a
$new.Recipients.Add($recipient) 
$new.save() 
# $new.HTMLBody += $sign

$display= $new.GetInspector
$display.Display()

标签: powershelloutlookmarkdown

解决方案


要绕过您发布的错误消息,您需要将文件作为字符串而不是字符串数组读取。原因是因为$new.Body需要一个字符串。默认情况下,Get-Content返回一个数组,文件的每一行都是该数组的一个元素。您可以使用开关更改此行为-Raw,它将内容作为一个字符串读取。

$a = Get-Content -Path "c:\temp\file.md" -Raw

如果-Raw开关更改了换行符格式,您始终可以Get-Content使用您选择的换行符加入默认数组。

$a = Get-Content -Path "c:\temp\file.md"
$new.HTMLBody = $a -join "`r`n"

GetType()您可以使用PowerShell 对象可用的方法查看原始代码中的类型差异。

$a.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


$new.Body.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

推荐阅读