powershell - 使用 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()
解决方案
要绕过您发布的错误消息,您需要将文件作为字符串而不是字符串数组读取。原因是因为$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
推荐阅读
- python - 带有 where 条件的 Pyodbc SQL 插入查询
- javascript - 这里 API Maps 设置标记
- ibm-integration-bus - 如何在 ESQL 中检查变量的类型
- bash - 带参数和评估的条件构造
- google-cloud-platform - gsutill:使用客户提供的密钥加密的备份文件
- reactjs - 将 props 中的对象值传递给另一个组件
- linux - Ansible playbook 卡在 Task 部分而没有写入日志
- css - 对话框有两个滚动条,但我想要一个
- mysql - Grafana 仪表板导致 Firefox 浏览器崩溃
- objective-c - 使用 NSURL* 初始化 NSString 的不兼容指针类型