0

I am new to Powershell (and coding in general) and am attempting to save outlook attachments that are all in the form of .xlsx from a specific sender. I managed to get a general script written to save attachments from the sender, but I also want to parse the attachment name to save it with a specific title using the parsed group values.

Sample outlook attachment name: Produce Price changes 1-1 for-#70-Butte.xlsx

Here is the general script that works:

#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"))}}}

Here are the changes I added to try and parse the attachment name to create a new document title:

$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"))}}}}}} 

Any help is greatly appreciated. Thanks

caweisz
  • 1
  • 3
  • You need to ask 1 question at a time on Stackoverflow. I suggestion you break your problem into a few different questions and ask them one at a time. You might find that when you understand the first bit of your puzzle the rest falls into place. Here's the guide on how to write a good question : https://stackoverflow.com/help/how-to-ask – snowcode Dec 31 '20 at 21:24
  • As for this [I am new to Powershell (and coding in general)], that is all well and good, but you jumped into the deep end and did not ramp up on PowerShell or programming first. I commend you on what you did thus far, but if you open your code in a PS editor (ISE/VSCode), for syntax checking and code completion and formatting. You'll see you have syntax errors in what you've posted, and the noted syntax errors, I am surprised this is working at all. Being new, I am going to assume you c&p the first part from elsewhere, and trying to tweak it, that's fine of course. Ditto to what snowcode said. – postanote Jan 01 '21 at 01:29
  • Use the PSScriptAnalyzer tools and Trace-Command, PS Best Practice guides, to validate your approach and what you are doing first. Just save the attachment, and use the Excel COM to do what as you choose. – postanote Jan 01 '21 at 01:31

0 Answers0