获取 msg 文件的内容到字符串中 [英] Get contents of msg file into string

查看:69
本文介绍了获取 msg 文件的内容到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Powershell 解析保存到本地文件夹的电子邮件的内容.

I am using Powershell to parse the contents of email messages that are saved to a local folder.

代码如下...

Get-ChildItem "C:\projtest\emails" -Filter *.msg |
ForEach-Object {
    $msg = ""
    $outlook = New-Object -comobject outlook.application
    $msg = $outlook.Session.OpenSharedItem($_.FullName)
    $msg | Select -ExpandProperty body 

    Write-Host $msg
}

$outlook.Quit()

现在,我只想打开文件,获取内容并显示它.

For now, I just want to open the file, get the contents, and display it.

我遇到的问题是,一旦运行脚本,OUTLOOK.EXE 不会关闭,因此我无法在同一条消息上再次运行脚本.

The issue I'm running into is that once the script is run, OUTLOOK.EXE does not close so I cannot run the script again on the same message.

有没有更好的方法来打开 Outlook 电子邮件,在 PowerShell 中将内容转换为字符串,然后关闭 Outlook 进程?

Is there a better way to open Outlook e-mail messages, get the contents into a string in PowerShell, and close the Outlook process?

推荐答案

不要打开和关闭 Outlook,打开它一次,做你所有的工作,然后在事情结束时关闭它.

Don't open and close Outlook, open it once, do all of your work, and then close it at the end of things.

$outlook = New-Object -comobject outlook.application
Get-ChildItem "C:\projtest\emails" -Filter *.msg |
    ForEach-Object {
        $msg = $outlook.Session.OpenSharedItem($_.FullName)
        $msg.body 
    }
$outlook.Quit()

好的,您需要关闭 .msg 文件,而不是 Outlook.只需将其添加到 ForEach 循环中:

Ok, you need to close your .msg files, not Outlook. Just add that to the ForEach loop:

$outlook = New-Object -comobject outlook.application
Get-ChildItem "C:\projtest\emails" -Filter *.msg |
    ForEach-Object {
        $msg = $outlook.Session.OpenSharedItem($_.FullName)
        $msg.body 
        $msg.Close()
    }
$outlook.Quit()

这篇关于获取 msg 文件的内容到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆