So my ultimate goal is to export emails after a specific length of time as a .msg and then upload them to sharepoint for archival purposes.
My issue is all the examples I find never seem to work for me. I use PowerGUI for my ISE, I have powershell 2.0 and Outlook 2007 SP3
So for example, when I try something very simple according to the scripting guy Reference, I try this line of code:
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null
[enum]::GetNames("Microsoft.Office.Interop.Outlook.OlDefaultFolders")This should list the enums used to point to the different folders in outlook....here are some of them:
olFolderDeletedItems
olFolderOutbox
olFolderSentMail
olFolderInbox
olFolderCalendar
olFolderContacts
olFolderJournal
When I run it, either PowerGUI or straight in the console I receive the following error:
Cannot convert argument "0", with value: "Microsoft.Office.Interop.Outlook.OlDefaultFolders", for "GetNames" to type "System.Type": "Cannot convert the "Microsoft.
Office.Interop.Outlook.OlDefaultFolders" value of type "System.String" to type "System.Type"."
At C:\Users\clayman\AppData\Local\Temp\ceb3eca9-5cf1-4dd2-92aa-c2c29d3ca438.ps1:2 char:17+ [Enum]::GetNames <<<< ("Microsoft.Office.Interop.Outlook.OlDefaultFolders")+ CategoryInfo : NotSpecified: (:) [], MethodException+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgumentTruth be told, no matter what sample I try they all fail, for instance try and run this function (yet another scripting guy article)
Function Get-OutlookInBox
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items |
Select-Object -Property Subject, ReceivedTime, Importance, SenderName
}And this one fails with:
Add-Type : Could not load file or assembly 'Microsoft.Office.Interop.Outlook, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dep endencies. The system cannot find the file specified. At C:\Users\clayman\AppData\Local\Temp\51f81593-6a0f-4261-9888-5499432508e7.ps1:3 char:10+ Add-Type <<<< -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null+ CategoryInfo : NotSpecified: (:) [Add-Type], FileNotFoundException+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.AddTypeCommand Exception calling "GetDefaultFolder" with "1" argument(s): "Value does not fall within the expected range." At C:\Users\clayman\AppData\Local\Temp\51f81593-6a0f-4261-9888-5499432508e7.ps1:8 char:39+ $folder = $nameSpace.getDefaultFolder <<<< ($olFolders)+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : ComMethodTargetInvocation
If I change:
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
To:
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Outlook") | Out-NullThe first error goes away, but the second one remains. What the heck am I doing wrong?
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Don't Retire Technet