PowerShell:将标题从.msg文件转换为.txt -当前目录dom dim dim dim dut tul header Information,但特定目录确实

发布于 2025-02-03 12:16:21 字数 839 浏览 2 评论 0原文

因此,我正在尝试制作一个脚本以获取一批.msg文件,提取其标头信息,然后将标题信息扔到.txt文件中。当我使用此代码时,这一切都很好:

$directory = "C:\Users\IT\Documents\msg\"
$ol = New-Object -ComObject Outlook.Application
$files = Get-ChildItem $directory -Recurse
foreach ($file in $files)
{
  $msg = $ol.CreateItemFromTemplate($directory + $file)
  $headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
  $headers > ($file.name +".txt")
}

但是,当我更改目录以使用从$ directory =“。将所有文件放入文本文档中,但它们将完全空白,而没有标头信息。我尝试了不同的变体:
$ directory = -path”。\ msg \ \“
$ files = get -childitem -path $目录
get -childitem -path”。

$ files = 。我正在尝试设置此问题,以便可以通过简单地将其放入文件夹并运行它来完成。

谢谢!任何帮助都非常感谢!

注意:我确实已经安装了Outlook,因此不是无法拉动标头的问题,因为它在指定代码中指定目录

So I am trying to make a script to take a batch of .msg files, pull their header information and then throw that header information into a .txt file. This is all working totally fine when I use this code:

$directory = "C:\Users\IT\Documents\msg\"
$ol = New-Object -ComObject Outlook.Application
$files = Get-ChildItem $directory -Recurse
foreach ($file in $files)
{
  $msg = $ol.CreateItemFromTemplate($directory + $file)
  $headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
  $headers > ($file.name +".txt")
}

But when I change the directory to use the active directory where the PS script is being run from $directory = ".\msg\", it will make all the files into text documents but they will be completely blank with no header information. I have tried different variations of things like:
$directory = -Path ".\msg\"
$files = Get-ChildItem -Path $directory
$files = Get-ChildItem -Path ".\msg\"

If anyone could share some ideas on how I could run the script from the active directory without needing to edit the code to specify the path each location. I'm trying to set this up so it can be done by simply putting it into a folder and running it.

Thanks! Any help is very appreciated!

Note: I do have outlook installed, so its not an issue of not being able to pull the headers, as it works when specifying a directory in the code

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

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

发布评论

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

评论(1

轻许诺言 2025-02-10 12:16:21

最简单的方法实际上可能是这样做

$msg = $ol.CreateItemFromTemplate($file.FullName)

,因此,完整的脚本将看起来像这样

$directory = ".\msg\"
$ol = New-Object -ComObject Outlook.Application
$files = Get-ChildItem $directory

foreach ($file in $files)
{
    $msg = $ol.CreateItemFromTemplate($file.FullName)
    $headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
    $headers > ($file.name +".txt")
}

说的一切,值得在自动变量上阅读(get -automatic_variables) - for实例有关$ PWD$ PSSCRIPTROOT$ PSCommandPath可能很有用。

替代方法 - 即使它们似乎不必要地复杂。

$msg = $ol.CreateItemFromTemplate((Get-Item $directory).FullName + $file)

或这样的东西

$msg = $ol.CreateItemFromTemplate($file.DirectoryName + "\" $file)

The easiest way might actually be to do it this way

$msg = $ol.CreateItemFromTemplate($file.FullName)

So, the complete script would then look something like this

$directory = ".\msg\"
$ol = New-Object -ComObject Outlook.Application
$files = Get-ChildItem $directory

foreach ($file in $files)
{
    $msg = $ol.CreateItemFromTemplate($file.FullName)
    $headers = $msg.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
    $headers > ($file.name +".txt")
}

All that said, it could be worthwhile reading up on automatic variables (Get-Help about_Automatic_Variables) - for instance the sections about $PWD, $PSScriptRoot and $PSCommandPath might be useful.

Alternative ways - even though they seem unnecessarily complicated.

$msg = $ol.CreateItemFromTemplate((Get-Item $directory).FullName + $file)

Or something like this

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