使用 Outlook 2016 将 Azure 信息保护标签添加到 Powershell 脚本



>我正在尝试将AIP msip_labels的自定义标头添加到我正在编写的Powershell脚本中。 我已经想出了如何使用.Net.SMTP 使用:

$message.Headers.Add("msip_labels","MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Enabled=True; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SiteId=00000000-1111-2222-3333-444444444444; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Owner=user2@domain.tld; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SetDate=$((Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")); MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Name=Internal; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Application Microsoft Azure Information Protection; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_ActionId ffffffff-5555-6666-7777-888888888888; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Extended_MSFT_Method Manual")

根据研究,我所做的应该在理论上使用Outlook 2016:

$Outlook = New-Object -ComObject Outlook.Application
$message = $Outlook.CreateItem(0)
$message.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/msip_labels", "MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Enabled=True; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SiteId=00000000-1111-2222-3333-444444444444; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Owner=user2@domain.tld; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SetDate=$((Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")); MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Name=Internal; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Application=Microsoft Azure Information Protection; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_ActionId=ffffffff-5555-6666-7777-888888888888; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Extended_MSFT_Method=Manual")
$message.To = "user1@domain.tld"
$message.Cc = "user3@domain.tld"
$message.Subject = "Report"
$message.HTMLBody = @"
<p><font face = "Calibri" size = "3">Hello World</p></font>
"@
$reportMessage.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null

我使用 MAPI 查看器确认了这一点,这就是 Outlook 本身与我仅使用 Outlook 发送的其他电子邮件一起使用的内容。但是,当我尝试在我的脚本中运行它时,出现此错误:

Exception setting "SetProperty": Cannot convert the "MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Enabled=True; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SiteId=00000000-1111-2222-3333-444444444444;
MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Owner=user2@domain.tld; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_SetDate=$((Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")); MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Name=Internal;
MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Application=Microsoft Azure Information Protection; MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_ActionId=ffffffff-5555-6666-7777-888888888888;
MSIP_Label_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee_Extended_MSFT_Method=Manual" value of type "string" to type "Object".
At C:emailtest.ps1:21 char:1
+ $message.PropertyAccessor.SetProperty("http://schemas.microsoft ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : RuntimeException

这似乎没有多大意义,它应该是 MAPI 架构中的一个字符串,所以我不确定为什么它认为它应该是一个对象。我什至尝试使用ConvertFrom-String将这些值转换为对象,但它不起作用。对此的任何建议将不胜感激。

尝试减少作为值传递的字符串长度。它是否正常工作?

似乎您需要使用Outlook所基于的低级API - 扩展MAPI。如果使用OpenProperty方法,则与 OOM 不同,它对字符串长度没有任何限制。此外,您可以考虑在该 API 周围使用任何第三方包装器,例如 Redemption。

如果使用PropertyAccessor,则必须对异常处理逻辑有很好的了解。下面我列出了您可能会遇到的一些障碍:

  • Outlook 项目附件的正文和内容无法通过PropertyAccessor访问。
  • PropertyAccessor忽略日期/时间值的任何秒数 字符串属性的大小受限,具体取决于信息存储类型。
  • 限制:个人文件夹文件 (.pst( 和 Exchange 脱机文件夹文件 (.ost( 不能超过 4,088 字节。
  • 限制:对于对 Exchange 邮箱或公用文件夹层次结构的直接联机访问,限制为 16,372 字节。
  • 二进制属性 只能检索或设置值低于 4,088 字节的那些属性。(如果尝试使用较大的值,则会出现内存不足错误(。

您可能会发现在 Outlook 2007 中使用 PropertyAccessor 和 StorageItem 类时不要绊倒石头一文很有帮助。

通过在字符串末尾附加一个空字符("'0"(对我有用 这是属性所需的类型"PtypString"所必需的。

https://learn.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxcdata/0c77892e-288e-435a-9c49-be1c20c7afdb