如何使用 Powershell 指定收件箱的子文件夹

Ple*_*Guy 0 powershell outlook

我正在尝试使用 Powershell 访问 Outlook(2010)中名为“子文件夹”的“收件箱”子文件夹。

$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)

# how do I specify a subfolder that's inside Inbox???
# I mean, "Inbox\subfolder" where "subfolder" is the name of the subfolder...
Run Code Online (Sandbox Code Playgroud)

我如何指定这个子文件夹?

我确信这真的很简单,这就是为什么我即将“失去它”。提前致谢!

*稍后在我的代码中,我在正文中搜索“searchterm”,如果匹配,则将结果发送到文本文件。以下代码适用于我的收件箱:

$inbox.items | foreach {
if($_.body -match "searchterm") {$_.body | out-file -encoding ASCII foo.txt} # prints to file...
Run Code Online (Sandbox Code Playgroud)

我想查看收件箱的子文件夹而不是收件箱,如上所述...

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++

编辑:

$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$targetfolder = $inbox.Folders | where-object { $_.name -eq "Subfolder" }
$targetfolder.items | foreach {
if($_.body -match "keyword") {$_.body | out-file -Append -encoding ASCII foo.txt} # keyword match prints body to file...
}
Run Code Online (Sandbox Code Playgroud)

好的,我认为这现在有效......

我不知道我做错了什么,尽管这是我使用 Powershell 的第一天,所以这并不奇怪,真的。

Dan*_*nak 5

$targetfolder = $inbox.Folders | where-object { $_.name -eq "subfolder" }
$targetfolder.items | where-object { $_.body -match "keyword" } | % { $_.body } # can then redirect the body to file etc.
Run Code Online (Sandbox Code Playgroud)

编辑:不知道为什么你最新的编辑不起作用。你的在结构上看起来与我上面的相似,我用我自己的邮箱验证过。

编辑 编辑:确保如果您使用的是输出文件,则附加结果而不是覆盖每个匹配项。