代码之家  ›  专栏  ›  技术社区  ›  SE1986

使用Powershell加载.msg文件内容-Null值错误

  •  1
  • SE1986  · 技术社区  · 3 年前

    我正在尝试使用Powershell访问.msg文件中的元数据(例如,获取发件人地址、主题行等)

    我有以下简单的代码

    Add-Type -assembly "Microsoft.Office.Interop.Outlook"
    
    $outlook = New-Object -comObject Outlook.Application
    $msg = $outlook.Session.OpenSharedItem("C:\Test\email.msg")
    

    但是我弄错了

    不能对空值表达式调用方法。第4行字符:1

    如果我跑步

    $outlook
    

    我明白

    Application        : 
    Class              : 
    Session            : 
    Parent             : 
    Assistant          : 
    Name               : 
    Version            : 
    COMAddIns          : 
    Explorers          : 
    Inspectors         : 
    LanguageSettings   : 
    ProductCode        : 
    AnswerWizard       : 
    FeatureInstall     : 
    Reminders          : 
    DefaultProfileName : 
    IsTrusted          : 
    Assistance         : 
    TimeZones          : 
    PickerDialog       : 
    

    所有属性都显示为null,包括 .Session ,这解释了我为什么会得到null值错误。

    我在创建 $outlook 对象

    我试过运行和不运行Outlook,结果都是一样的。

    我安装了Office 365,正在使用Powershell 5.1.19041.1320

    1 回复  |  直到 3 年前
        1
  •  1
  •   SE1986    3 年前

    我通过在程序和功能对话框中对O365进行“快速修复”来解决这个问题

        2
  •  1
  •   Chaetura    2 年前

    在Windows 10 Pro和“Microsoft 365 Apps for Business”上,2022年7月15日,当我创建选定Outlook联系人的CVS导出的类似脚本停止工作时,快速修复也对我起到了作用,原因相同,即$Outlook的分配正常完成,但$outlooks没有参数值,正如您上面所示,导致了相同的空值表达式错误。

    我已经有一个多月没有运行这个脚本了,所以不能说是哪种或哪种更新破坏了它。

    以下是修复后$outlook的有效值示例(我将“产品代码”设置为全0,以防该值标识我的许可证):

    Application        : Microsoft.Office.Interop.Outlook.ApplicationClass
    Class              : olApplication
    Session            : Microsoft.Office.Interop.Outlook.NameSpaceClass
    Parent             :
    Assistant          : System.__ComObject
    Name               : Outlook
    Version            : 16.0.0.15330
    COMAddIns          : System.__ComObject
    Explorers          : {}
    Inspectors         : {System.__ComObject, System.__ComObject, System.__ComObject, System.__ComObject...}
    LanguageSettings   : System.__ComObject
    ProductCode        : {00000000-0000-0000-0000-000000000000}
    AnswerWizard       :
    FeatureInstall     : msoFeatureInstallOnDemand
    Reminders          : {$null, $null, $null, $null...}
    DefaultProfileName : MS-2017-10
    IsTrusted          : False
    Assistance         : System.__ComObject
    TimeZones          : {System.__ComObject, System.__ComObject, System.__ComObject, System.__ComObject...}
    PickerDialog       : System.__ComObject