如何使用PowerShell通过CC创建MSG文件?

如何使用PowerShell通过CC创建MSG文件?,powershell,outlook,Powershell,Outlook,我尝试使用powershell创建msg文件。我已经可以添加主题和接收者了。但我不能添加抄送 这就是我到目前为止所做的: $obj = New-Object -ComObject Outlook.Application $mail = $obj.CreateItem(0) $Mail.Recipients.Add("email@email.com") $Mail.Recipients.Type = olCC $Mail.Recipients.Add("email2@email.com")

我尝试使用powershell创建msg文件。我已经可以添加主题和接收者了。但我不能添加抄送

这就是我到目前为止所做的:

$obj = New-Object -ComObject Outlook.Application
$mail = $obj.CreateItem(0) 

$Mail.Recipients.Add("email@email.com") 

$Mail.Recipients.Type = olCC
$Mail.Recipients.Add("email2@email.com") 
$Mail.Recipients.Add("email3@email.com") 

$Mail.Subject = "Some Subject" 
$Mail.Body = "test mail with powershell"

$Mail.Attachments.Add("c:\Users\se\Desktop\Attachment.txt")
$mail.SaveAs("c:\Users\se\Desktop\test.msg")

c:\Users\se\Desktop\test.msg
我试图将收件人对象从默认(“to”)更改为CC,但这不起作用。

在添加收件人之前,您需要更改收件人对象上的类型,而不是
收件人
集合上的类型

$cc = $Mail.Recipients.Add("email2@email.com")
$cc.Type = 2
$cc = $Mail.Recipients.Add("email3@email.com")
$cc.Type = 2
此外,
olCC
在PowerShell中不是有效的常量。您需要指定常量的名称(参见上文),或者自己定义常量

$olCC = 2
# alternatively, if you want $olCC to be an actual constant:
#New-Variable -Name olCC -Value 2 -Option Constant
...
$cc = $Mail.Recipients.Add("email2@email.com")
$cc.Type = $olCC
$cc = $Mail.Recipients.Add("email3@email.com")
$cc.Type = $olCC
或在互操作程序集中查找值(未测试):

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
...
$cc = $Mail.Recipients.Add("email2@email.com")
$cc.Type = [Microsoft.Office.Interop.Outlook.Constants]::olCC
$cc = $Mail.Recipients.Add("email3@email.com")
$cc.Type = [Microsoft.Office.Interop.Outlook.Constants]::olCC