Powershell COM 对象

发布于 2024-10-28 05:14:31 字数 1255 浏览 3 评论 0原文

我尝试使用以下代码通过 Powershell 从共享日历中获取日历项目:

$outlook = new-object -ComObject Outlook.application
$session = $outlook.Session
$session.Logon("Outlook")
$namespace = $outlook.GetNamespace("MAPI")
$recipient = $namespace.CreateRecipient("John Smith")
$theirCalendar = $namespace.GetSharedDefaultFolder($recipient, "olFolderCalendar")

但出现类型不匹配错误:

无法将参数“0”(值为“System.__ComObject”)转换为“GetSharedDefaultFolder”以键入“Microsoft.Office.I” nterop.Outlook.Recipient": "无法转换类型为“System.__ComObject#{00063045-0000-00”的“System.__ComObject”值 00-c000-000000000046}”键入“Microsoft.Office.Interop.Outlook.Recipient”。 行:1 字符:34 + $namespace.GetSharedDefaultFolder <<<<< ($recipient,“olFolderCalendar”) + 类别信息:未指定:(:) [],MethodException + FullQualifiedErrorId:MethodArgumentConversionInvalidCastArgument

我尝试直接将 $recipient 转换为 Microsoft.Office.Interop.Outlook.Recipient,但这不起作用,而且我还尝试了 invoke-method( ) 过程在这里有详细记录:http://www.mcleod。 co.uk/scotty/powershell/COMinterop.htm

看起来后者应该可以工作,但它似乎没有提供 GetSharedDefaultFolder() 所需的多个参数。

I am attempting to get calendar items from a shared calendar via Powershell with the following code:

$outlook = new-object -ComObject Outlook.application
$session = $outlook.Session
$session.Logon("Outlook")
$namespace = $outlook.GetNamespace("MAPI")
$recipient = $namespace.CreateRecipient("John Smith")
$theirCalendar = $namespace.GetSharedDefaultFolder($recipient, "olFolderCalendar")

but I am getting a type mismatch error:

Cannot convert argument "0", with value: "System.__ComObject", for "GetSharedDefaultFolder" to type "Microsoft.Office.I
nterop.Outlook.Recipient": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{00063045-0000-00
00-c000-000000000046}" to type "Microsoft.Office.Interop.Outlook.Recipient"."
At line:1 char:34
+ $namespace.GetSharedDefaultFolder <<<< ($recipient, "olFolderCalendar")
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

I've tried directly casting $recipient to a Microsoft.Office.Interop.Outlook.Recipient, which doesn't work, and I have also tried the invoke-method() procedure well documented here: http://www.mcleod.co.uk/scotty/powershell/COMinterop.htm

It seems like the latter should work, but it doesn't appear to have provisions for the multiple parameters that GetSharedDefaultFolder() requires.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

递刀给你 2024-11-04 05:14:31

我已经设法使用 System.__ComObject 的 InvokeMember 方法来实现此功能。为了将多个参数传递给该方法,只需将它们括在括号中即可。

该代码行的示例如下所示:

PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder" [System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))

$user 是之前设置的收件人对象。
$mapi 是 MAPI 命名空间对象(也是之前设置的)。

I have managed to get this working using the InvokeMember method of System.__ComObject. In order to pass multiple parameters to the method, simply enclose them in parentheses.

An example of the line of code is shown here:

PS C:> $usercontacts=[System.__ComObject].InvokeMember("GetSharedDefaultFolder" [System.Reflection.BindingFlags]::InvokeMethod,$null,$mapi,($user,10))

$user is the recipient object previously set up.
$mapi is the MAPI namespace object (also set up previously).

弱骨蛰伏 2024-11-04 05:14:31

在这里找到了解决方案: http://cjoprey.blog .com/2010/03/09/getting-another-users-outlook-folder/

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook

$class = @”
using Microsoft.Office.Interop.Outlook;public class MyOL
{
    public MAPIFolder GetCalendar(string userName)
    {
        Application oOutlook = new Application();
        NameSpace oNs = oOutlook.GetNamespace("MAPI");
        Recipient oRep = oNs.CreateRecipient(userName);
        MAPIFolder calendar = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderCalendar);
        return calendar;
    }
}
“@

Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook

Found a solution here: http://cjoprey.blog.com/2010/03/09/getting-another-users-outlook-folder/

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook

$class = @”
using Microsoft.Office.Interop.Outlook;public class MyOL
{
    public MAPIFolder GetCalendar(string userName)
    {
        Application oOutlook = new Application();
        NameSpace oNs = oOutlook.GetNamespace("MAPI");
        Recipient oRep = oNs.CreateRecipient(userName);
        MAPIFolder calendar = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderCalendar);
        return calendar;
    }
}
“@

Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
骄兵必败 2024-11-04 05:14:31

尝试将 olFolderCalendar 替换为数字 9
COM 对象需要实际值。它们无法将明文名称转换为常量值。

Try replacing olFolderCalendar with the number 9.
COM objects need the actual values. They cannot convert clear text names to constant values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文