微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Powershell,如何解析 Outlook 附件名称并保存

如何解决使用 Powershell,如何解析 Outlook 附件名称并保存

我是 Powershell 的新手(以及一般的编码),我正在尝试从特定发件人处以 .xlsx 格式保存 Outlook 附件。我设法编写了一个通用脚本来保存来自发件人的附件,但我还想解析附件名称以使用解析的组值将其与特定标题一起保存。

示例前景附件名称:生产价格变化 1-1 for-#70-Butte.xlsx

这是有效的通用脚本:

#Open outlook and define MAPI environment
$outlook = new-object -com outlook.application
$namespace = $outlook.GetNamespace("MAPI")

#Define InBox as an Object the "1" denotes the second listed account (pricing email account)
$inBox = $outlook.Session.Folders.Item(1).folders.item("InBox")
$inBox | ft Folderpath

#Define Cody Subfolder as an object
$Cody = $Outlook.Session.Folders.Item(1).Folders.Item("InBox").folders.item("Cody")
$Cody | ft FolderPath
$Cody.Items | Select-Object Sendername,senderemailaddress -unique

#Define save location
$filepath = "C:\Users\cody.weisz\Desktop\Produce"
$Cody.Items | where-object {$_.Sendername -match "Jeff Griffin"} | Foreach {
 $_.attachments | Foreach { 
 Write-Host $_.filename
 $name = $_.filename
 If($name.contains("xlsx")) { 
 $_.saveasfile((Join-Path $filepath "$name.xlsx"))}}}

以下是我为尝试解析附件名称以创建新文档标题添加的更改:

$filepath = "C:\Users\cody.weisz\Desktop\Produce"
$Cody.Items | where-object {$_.Sendername -match "Jeff Griffin"} | Foreach {
$_.attachments | Foreach { 
Write-Host $_.filename
$name = $_.filename | Foreach {
    Select-string -Pattern (^(\w+\s\w+\s\w+\s)(\d+-\d+)(\w+-)(\W\d+)) |
    ForEach-Object { 
        $Type,$Date,$NA,$Store = $_.Matches[0].Groups[1..4].Value
        [PSCustomObject] @{
            Type = $Type
            Date = $Date 
            NA = $NA
            Store = $Store 
If($name.contains("xlsx")) { 
$_.saveasfile((Join-Path $filepath "$Type+$Store+$Date.xlsx"))}}}}}} 

非常感谢任何帮助。谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。