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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

(taken out the -passthru parameter as it was unnecessary)
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);
    }

4 Comments

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
Hi, ive managed to get that running, only needed 1 folder so section:$inbox.Folders ` | ? Name -eq 'colour' ` | % Items ` | % Attachments ` | % {
Is correct above, last puzzle for me is, how would i auto print each attachment as it is saved to the output folder?
@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 <DocumentFileName> -Verb Print, but that may or may not work perfectly and you might need to play around if it leaves the document open. I'm not sure how you'd print documents that don't have a Print verb, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.