none
save outlook attachement

    Question

  • I have a power shell script which will save outlook attachements to a folder. When i execute this script it works fine on the server but when i try to run this through autosys it fails with the below error.

    Retrieving the COM class factory for component with CLSID {0006F03
    A-0000-0000-C000-000000000046} failed due to the following error: 80080005.

    below is the sample code that i am using.

     

    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace("MAPI")

    $f = $n.PickFolder()

    $filepath = "c:\temp\"
    $f.Items| foreach {
     $SendName = $_.SenderName
       $_.attachments|foreach {
        Write-Host $_.filename
        $a = $_.filename
        If ($a.Contains("xlsx")) {
        $_.saveasfile((Join-Path $filepath "$SendName.xlsx"))
       }
      }
    }

     

    i made sure that the account under which i am running has the permissions but still the same problem. Can some one help me on this.

    Friday, November 26, 2010 8:23 AM

Answers

  • The error your getting back states the facility code is a Windows error:  5 is Access Denied.

    Unless you've changed the defaults Outlook will be using the defaults which should allow everyone for local activation; so I suspect that this in and of itself not to be the issue.

    However if this scheduler trys to instantiate Outlook or anything else which expects to be running under the contex of the interactive user (doesn't matter if are running under the same user account or not) and no ones logged on to this workstation this will fail.

    Are you logged on to this workstation when AutoSys is running?

    Can you get a VBScript running the same set of statements to work using AutoSys

     

    Friday, November 26, 2010 7:36 PM

All replies

  • Can some one help me on this.
    Friday, November 26, 2010 11:23 AM
  • What is "autosys"?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, November 26, 2010 11:30 AM
  • Its a scheduling tool.

    Friday, November 26, 2010 11:50 AM
  • When you tested it, did you log on with the same account that autosys is using to run it?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, November 26, 2010 12:12 PM
  • The error your getting back states the facility code is a Windows error:  5 is Access Denied.

    Unless you've changed the defaults Outlook will be using the defaults which should allow everyone for local activation; so I suspect that this in and of itself not to be the issue.

    However if this scheduler trys to instantiate Outlook or anything else which expects to be running under the contex of the interactive user (doesn't matter if are running under the same user account or not) and no ones logged on to this workstation this will fail.

    Are you logged on to this workstation when AutoSys is running?

    Can you get a VBScript running the same set of statements to work using AutoSys

     

    Friday, November 26, 2010 7:36 PM
  • I have tried logging off when Autosys is running but still failed. So i am trying to intantiate the Outlook (the script is provided above). What is the solution to this problem.

    Wednesday, December 08, 2010 12:06 PM
  • If your Outlook is connect to an Exchange 2007 or 2010 server, I'd use the EWS Managed API instead of Outlook.  It doesn't use MAPI, and doesn't require Outlook to be installed on the system that's running the script, so you avoid all the problems associated with it.  An example of using it with Powershell to retrieve attachments from emails can be found here:

    http://gsexdev.blogspot.com/2010/01/writing-simple-scripted-process-to.html


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, December 08, 2010 12:31 PM
  • If I Know the specific folder that I want to get the Attachement from, and I do not wat to use the PicFolder() method, what shall I use instead?

    Like if I want to get the file from Inbox\MYPersonalFolder?

    Friday, February 24, 2012 5:54 PM
  • Okay, Now, I got how to get the inbox, the inbox folder ID is 6

    $n.GetDefaultFolder(6)

    but I dont know how to get a sub-folder under inbox, for example " \inbox\MyFolder" I dont know if I can specify a foldert o loop in or its just the inbox and the other PreDefined folders?

    Monday, February 27, 2012 7:28 PM
  • Here is a humble recursive function that retrieves Outlook folders. It can/should be improve (sorry, no time now) by turning it into an advanced function to accept piped input, type check the arguments passed to it and check if Outlook is running or not to instantiate it.

    Since you want a working sample, line 2 assumes Outlook is running and is set to $outlook. Then, lines 12 and 13 use Where-Object to filter —and retrieve, if it exists— the target folder from Get-OutlookFolder. Do note that you may get more than folder if they share the same name. You can also filter on FolderPath or FullFolderPath to narrow the criteria.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    function Get-OutlookFolder {
      param($Root = $outlook.Session)
      $Root.Folders | ForEach-Object {
        $_
        if ($_.Folders) {
          Get-OutlookFolder -Root $_
        }
      }
    }
    
    # filter folders
    $MYPersonalFolder = Get-OutlookFolder |
      Where-Object {$_.Name -eq 'MYPersonalFolder'}
      
    $MYPersonalFolder | Format-List *Path, Name
    
    • Proposed as answer by Sam Gasgous Wednesday, February 29, 2012 7:53 PM
    Tuesday, February 28, 2012 5:03 AM
  • Awesome,Thanks "Leteo" That helped me alot :)

    Wednesday, February 29, 2012 7:54 PM