0

I have a script i scraped from other scripts. It works except i am quite new to powershell and am a sys admin not a dev, (but reading my ass off). I can get the scrtipt to work downloading attachments from inbox in outlook but need it to download attachments from a subfolder instead:

############################# Outlook Call ##############################

$olFolderInbox = 6 
$outlook = new-object -com outlook.application; 
$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$messages = $inbox.items 
write-host $messages.count 
$messcount = $messages.count 




foreach($message in $messages){ 


##############Save Attachments################

$filepath = "c:\attachments\" 
$message.attachments|foreach { 
    Write-Host $_.filename 
    $attr = $_.filename 

    $_.saveasfile((Join-Path $filepath $_.filename))

    $a = $_.filename 
    If ($a.Contains("")) { 
    $_.saveasfile((Join-Path $filepath $a)) 
                             } 
  } 

}



###########END##########

Any Ideas anyone? Would be massively grateful.

Royston
  • 433
  • 2
  • 9
  • 25

2 Answers2

1
$OutputFolder = 'C:\tests';
$ErrorActionPreference= 'silentlycontinue'
$outlook = New-Object -ComObject Outlook.Application; 
$olFolderInbox = 6;
$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox);
$inbox.Folders `
    | ? Name -eq 'colour' `
    | % Items `
    | % Attachments `
    | % {
        $OutputFileName = Join-Path -Path $OutputFolder -ChildPath $_.FileName;
        if (Test-Path $OutputFileName) {
            $FileDirectoryName = [System.IO.Path]::GetDirectoryName($OutputFileName);
            $FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($OutputFileName);
            $FileExtension = [System.IO.Path]::GetExtension($OutputFileName);

            for ($i = 2; Test-Path $OutputFileName; $i++) {
                $OutputFileName = "{0} ({1}){2}" -f (Join-Path -Path $FileDirectoryName -ChildPath $FileNameWithoutExtension), $i, $FileExtension;
            }
        }
        Write-Host $OutputFileName;
        $_.SaveAsFile($OutputFileName)
    }

Remove-Item -Path C:\tests\*.jpg
Remove-Item -Path C:\tests\*.png

Start-Process –FilePath “c:\tests\*.docx” –Verb Print -PassThru  
Start-Process –FilePath “c:\tests\*.xlsx” –Verb Print -PassThru  
Start-Process –FilePath “c:\tests\*.doc” –Verb Print -PassThru  
Start-Process –FilePath “c:\tests\*.xls” –Verb Print -PassThru  
Start-Process –FilePath “c:\tests\*.pptx” –Verb Print -PassThru 
Start-Process –FilePath “c:\tests\*.ppt” –Verb Print -PassThru
Start-Process –FilePath “c:\tests\*.xls” –Verb Print -PassThru 
Start-Process –FilePath “c:\tests\*.msg” –Verb Print -PassThru 
Start-Process –FilePath “c:\tests\*.xlsm” –Verb Print -PassThru 
Start-Process –FilePath “c:\tests\*.rtf” –Verb Print -PassThru 
Start-Process –FilePath “c:\tests\*.pdf” –Verb Print -PassThru `
%{sleep 3;$_}
Remove-Item -Path C:\tests\*.*

This accomplishes what i need, i might put a kill process in after each one just to make sure there is no doc stuck.

But - The 1 thing left to accomplish is i need this to write to a non default printer and not change the existing default? This runs on a server with a vbscript already running utilizing default printer so it will mess that up if it changes.

Royston
  • 433
  • 2
  • 9
  • 25
0

As usual with COM Objects, it's pretty ugly to do.

You're supposed to do it like the question here, but I can't get that to work at all. $Inbox.Folders['FolderName'] returns nothing, and $Inbox.Folders('FolderName') returns an error.

Here's how I can get it to work. I'm on PowerShell v4 with Office 2013.

Say you've got a folder at \\Inbox\eReports\Amazon\StatsReports, and you want all the messages in it.

$Messages = $inbox.Folders `
    | ? Name -eq 'eReports' `
    | % Folders `
    | ? Name -eq 'Amazon' `
    | % Folders `
    | ? Name -eq 'StatsReports' `
    | % Items;

Note that the way I'm using ForEach-Object and Where-Object require PowerShell v3. Under earlier versions this would be considerably more verbose.


Here's a full version of the script that works for me on Win 7 x64/PowerShell v4/Office 2013. I've tested it on a few different folders, so it should work.

It will automatically rename files with duplicate names, following the Windows convention of adding a (2), then a (3) and so on.

$OutputFolder = 'C:\OutputFolder';

$outlook = New-Object -ComObject Outlook.Application; 
$olFolderInbox = 6;
$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox);
$inbox.Folders `
    | ? Name -eq 'eReports' `
    | % Folders `
    | ? Name -eq 'Amazon' `
    | % Folders `
    | ? Name -eq 'StatsReports' `
    | % Items `
    | % Attachments `
    | % {
        $OutputFileName = Join-Path -Path $OutputFolder -ChildPath $_.FileName;
        if (Test-Path $OutputFileName) {
            $FileDirectoryName = [System.IO.Path]::GetDirectoryName($OutputFileName);
            $FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($OutputFileName);
            $FileExtension = [System.IO.Path]::GetExtension($OutputFileName);

            for ($i = 2; Test-Path $OutputFileName; $i++) {
                $OutputFileName = "{0} ({1}){2}" -f (Join-Path -Path $FileDirectoryName -ChildPath $FileNameWithoutExtension), $i, $FileExtension;
            }
        }
        Write-Host $OutputFileName;
        $_.SaveAsFile($OutputFileName);
    }
Community
  • 1
  • 1
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Thanks for the help, Ive got PSVersion 5.0 and Office 2010. When i add this code in though and replace the variable $messages and folder name with my subfolder i am not getting the result, where would i place your code in the existing script above? Sorry for the lame Q – Royston Mar 23 '16 at 16:03
  • Hi, ive managed to get that running, only needed 1 folder so section:$inbox.Folders ` | ? Name -eq 'colour' ` | % Items ` | % Attachments ` | % { – Royston Mar 29 '16 at 07:43
  • Is correct above, last puzzle for me is, how would i auto print each attachment as it is saved to the output folder? – Royston Mar 29 '16 at 07:44
  • @Royston Printing is possible, but not straightforward. For plain text, you can pipe the files to `Out-Printer`. For some docs like Word or PDF, you should be able to use `Start-Process -Path -Verb Print`, but that may or may not work perfectly and you might need to [play around](https://gregcaporale.wordpress.com/2012/01/18/powershell-to-print-files-automatically/) if it leaves the document open. I'm not sure how you'd print documents that don't have a Print verb, however. – Bacon Bits Mar 29 '16 at 12:40