Reputation: 1
I have a list of named folders that I need to find .pdf files for and save the .pdf files in the folders.
My company has a file retention system that saves the .msg file to a drive.
I can find the correct .msg file by searching for the folder name.
The code I have right now will save the .msg file but I would prefer to save the pdf attachment in the email.
Is there a way I can have powershell extract the pdf? I know the pdf isn't actually in the .msg file so guessing this would involve opening the .msg in outlook then saving the attachment?
I can only find script online that uses PS to save attachments from .msg directly from Outlook, not when they are saved locally.
Thanks
My script:
$ucrfolder = "C:\Users\Desktop\Powershell Test\2022\Jan"
$UCRarray = Get-ChildItem -Path $ucrfolder -Directory | ForEach-Object {
if (-not (ls $_.FullName -file -Recurse *CDS-Import*)) {
write-output $_.Name
}
}
$source = "C:\Users\jcollier\OneDrive - Deloitte (O365D)\Desktop\Source"
#for each UCR folder in the array do something
ForEach ($UCR in $UCRarray) {
$destination = Join-Path -path $ucrfolder -childpath $UCR
#find all files in source folder that mention the UCR in the filename. Then copy item to the correct UCR folder. Change filepath below to folder that holds your entry documents.
Get-ChildItem $source -recurse -Include *$UCR* | copy-Item -Destination $destination
}
Upvotes: 0
Views: 1118
Reputation: 66225
Use Namespace.OpenSharedItem method in the Outlook Object Model to open MSG files. Once you have the MailItem object, you can loop through its MailItem.Attachments collection and call [Attachment.SaveAsFile][4]
for each attachment (you can combine the folder name with Attachment.FileName
when specifying the fully qualified file name to use with Attachment.SaveAsFile
.
Upvotes: 0