Video Screencast Help
Symantec Appoints Michael A. Brown CEO. Learn more.

EWS API with Powershell

Created: 13 Nov 2013 | 6 comments

I've installed the EWS API and I want to use it with Powershell...I've taken a sample code that works in VBScript for simple downloading an email from teh store and saving as a .MSG file.  This works fine in VBScript, but when I run it in powershell it says a property is not settable on the com object...the following error :-

"Property 'Data' cannot be found on this object; make sure it exists and is settable"

The code is below :-

#Load API
$EVAPI = New-Object -comobject EnterpriseVault.ContentManagementAPI -Strict
#Create new item
$EVITEM = $EVAPI.Item
#Set Archive ID
$EVITEM.ArchiveID = "1330E58127A6234469C258BAD9EF5C5321110000evserver"
#Set Saveset ID
$EVITEM.Id = "201206280910464~201203281231260000~Z~C10F18AA0191BF6058DC93BADE506FE1"
#Set path to save to
$EVITEM.Content.Data = "c:\temp\temp888.msg"
#Get email into path
$EVITEM.Get(3)
 
The error is the line $EVITEM.Content.Data = "c:\temp\temp888.msg"...this works fine in VBScript though.
 
 
Going on from that, I don't actually want to download the file as a .msg file...but rather instruct EV to restore the item back into the users mailbox.
Operating Systems:

Comments 6 CommentsJump to latest comment

EV_Ajay's picture

Hi,

Try to change the path $EVITEM.Content.Data = "c:\temp\temp888.msg" to another drive where have full rights to account.

 

Thanks,

Ajay

neil.doody-gl's picture

Hi, I do have full access to c:\temp...I'm a local admin on this machine.  Again this exact same procedure works in VB with the code being converted almost entirely like for like.

Just for clarity I've tried specifying my own temp directly under my users profile as the path, and as expected it still didn't work :-

PS C:\Users\neil.doody> $EVITEM.Content.Data = "C:\Users\NEIL~1.DOO\AppData\Local\Temp\test.msg"

Exception setting "Data": "Property 'Data' cannot be found on this object; make sure it exists and is settable."
At line:1 char:1
+ $EVITEM.Content.Data = "C:\Users\NEIL~1.DOO\AppData\Local\Temp\test.msg"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting
 
 
Going forward, I *could* just use VBScript...but I'm more interested on what we have to call on the API to simply restore an item back to the users mailbox as appoosed to saving to a local MSG file.
 
If only EV made the documentation to the API avaialble to customers...it's a one-off small time job I want to perform and I do not want to have to join an entire partner program to achieve it.
JesusWept3's picture

Can you use a get-member against the evapi?
Also you'll definitely need to save out to a message, there's no way to do the restore in memory from the API

neil.doody-gl's picture

No problem, I will post below...I've tried everything and I think there is simply a bug.  I even tried to use the .NET object instead of the COM object with the following :-

Add-Type -Path "c:\Program Files (x86)\Enterprise Vault\EVClient\KVS.EnterpriseVault.Interop.EVContentManagementAPI.dll"

$EVAPI = New-Object KVS.EnterpriseVault.Interop.ContentManagementAPIClass
 
Even this came back with the same error.  I then tried some tricks about using "invokemember" when properties are not beind defined propertly :-
 
$EVITEM = [System.__ComObject].InvokeMember("Item","GetProperty",$null,$EVAPI,$null)
[System.__ComObject].InvokeMember("Content","GetProperty",$null,$EVITEM,$null) | %{[System.__ComObject].InvokeMember("Data","SetProperty",$null,$_,"c:\temp\temp888.msg")}
 
Finally the same error...$EVITEM looks correct when I run this, here is get-member of the $EVAPI and the $EVITEM.  FIrst API as .NET object :-
 
PS P:\> $EVAPI| get-member
 
 
   TypeName: KVS.EnterpriseVault.Interop.ContentManagementAPIClass
 
Name                                              MemberType Definition
----                                              ---------- ----------
CreateObjRef                                      Method     System.Runtime.Remoting.ObjRef CreateObjRef(type reques...
Equals                                            Method     bool Equals(System.Object obj)
GetHashCode                                       Method     int GetHashCode()
GetLifetimeService                                Method     System.Object GetLifetimeService()
GetType                                           Method     type GetType()
IContentManagementAPI3_IDispatchQueryInterface    Method     System.Object IContentManagementAPI3_IDispatchQueryInte...
IContentManagementAPI4_IDispatchQueryInterface    Method     System.Object IContentManagementAPI4_IDispatchQueryInte...
IContentManagementAPI5_IDispatchQueryInterface    Method     System.Object IContentManagementAPI5_IDispatchQueryInte...
IDispatchQueryInterface                           Method     System.Object IDispatchQueryInterface(System.Object pUn...
InitializeLifetimeService                         Method     System.Object InitializeLifetimeService()
IsActiveEnterpriseVaultServer                     Method     bool IsActiveEnterpriseVaultServer(string LogicalServer...
ToString                                          Method     string ToString()
AdministrationLanguage                            Property   string AdministrationLanguage {get;}
APIClientVersion                                  Property   string APIClientVersion {get;}
APIServerVersion                                  Property   string APIServerVersion {get;}
Archive                                           Property   KVS.EnterpriseVault.Interop.IArchive Archive {get;}
ArchiveFolder                                     Property   System.Object ArchiveFolder {get;}
Archives                                          Property   System.Object Archives {get;}
ArchiveType                                       Property   KVS.EnterpriseVault.Interop.IArchiveType ArchiveType {g...
ArchiveTypes                                      Property   System.Object ArchiveTypes {get;}
AuthenticationIdentity                            Property   string AuthenticationIdentity {get;set;}
AuthenticationIdentityType                        Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTH_TYPE Authen...
AuthenticationMode                                Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
ConsumerGUID                                      Property   string ConsumerGUID {get;set;}
DirectoryDNSAlias                                 Property   string DirectoryDNSAlias {get;set;}
EnterpriseVaultServerInstalled                    Property   bool EnterpriseVaultServerInstalled {get;}
EnterpriseVaultServerName                         Property   string EnterpriseVaultServerName {get;}
ExtensionContentProvider                          Property   System.Object ExtensionContentProvider {get;}
ExtensionContentProviders                         Property   System.Object ExtensionContentProviders {get;}
Hold                                              Property   KVS.EnterpriseVault.Interop.IHold Hold {get;}
HoldGroups                                        Property   System.Object HoldGroups {get;}
Holds                                             Property   KVS.EnterpriseVault.Interop.IHolds Holds {get;}
IContentManagementAPI2_Archive                    Property   KVS.EnterpriseVault.Interop.IArchive IContentManagement...
IContentManagementAPI2_Archives                   Property   System.Object IContentManagementAPI2_Archives {get;}
IContentManagementAPI2_AuthenticationMode         Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
IContentManagementAPI2_DirectoryDNSAlias          Property   string IContentManagementAPI2_DirectoryDNSAlias {get;set;}
IContentManagementAPI2_Hold                       Property   KVS.EnterpriseVault.Interop.IHold IContentManagementAPI...
IContentManagementAPI2_Holds                      Property   KVS.EnterpriseVault.Interop.IHolds IContentManagementAP...
IContentManagementAPI2_Item                       Property   KVS.EnterpriseVault.Interop.IItem IContentManagementAPI...
IContentManagementAPI2_VaultStore                 Property   System.Object IContentManagementAPI2_VaultStore {get;}
IContentManagementAPI2_VaultStores                Property   System.Object IContentManagementAPI2_VaultStores {get;}
IContentManagementAPI3_Archive                    Property   KVS.EnterpriseVault.Interop.IArchive IContentManagement...
IContentManagementAPI3_Archives                   Property   System.Object IContentManagementAPI3_Archives {get;}
IContentManagementAPI3_AuthenticationMode         Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
IContentManagementAPI3_ConsumerGUID               Property   string IContentManagementAPI3_ConsumerGUID {get;set;}
IContentManagementAPI3_DirectoryDNSAlias          Property   string IContentManagementAPI3_DirectoryDNSAlias {get;set;}
IContentManagementAPI3_Hold                       Property   KVS.EnterpriseVault.Interop.IHold IContentManagementAPI...
IContentManagementAPI3_Holds                      Property   KVS.EnterpriseVault.Interop.IHolds IContentManagementAP...
IContentManagementAPI3_Item                       Property   KVS.EnterpriseVault.Interop.IItem IContentManagementAPI...
IContentManagementAPI3_Items                      Property   System.Object IContentManagementAPI3_Items {get;}
IContentManagementAPI3_VaultStore                 Property   System.Object IContentManagementAPI3_VaultStore {get;}
IContentManagementAPI3_VaultStores                Property   System.Object IContentManagementAPI3_VaultStores {get;}
IContentManagementAPI4_Archive                    Property   KVS.EnterpriseVault.Interop.IArchive IContentManagement...
IContentManagementAPI4_ArchiveFolder              Property   System.Object IContentManagementAPI4_ArchiveFolder {get;}
IContentManagementAPI4_Archives                   Property   System.Object IContentManagementAPI4_Archives {get;}
IContentManagementAPI4_AuthenticationIdentity     Property   string IContentManagementAPI4_AuthenticationIdentity {g...
IContentManagementAPI4_AuthenticationIdentityType Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTH_TYPE IConte...
IContentManagementAPI4_AuthenticationMode         Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
IContentManagementAPI4_ConsumerGUID               Property   string IContentManagementAPI4_ConsumerGUID {get;set;}
IContentManagementAPI4_DirectoryDNSAlias          Property   string IContentManagementAPI4_DirectoryDNSAlias {get;set;}
IContentManagementAPI4_Hold                       Property   KVS.EnterpriseVault.Interop.IHold IContentManagementAPI...
IContentManagementAPI4_Holds                      Property   KVS.EnterpriseVault.Interop.IHolds IContentManagementAP...
IContentManagementAPI4_Item                       Property   KVS.EnterpriseVault.Interop.IItem IContentManagementAPI...
IContentManagementAPI4_Items                      Property   System.Object IContentManagementAPI4_Items {get;}
IContentManagementAPI4_LastError                  Property   System.Object IContentManagementAPI4_LastError {get;}
IContentManagementAPI4_VaultStore                 Property   System.Object IContentManagementAPI4_VaultStore {get;}
IContentManagementAPI4_VaultStores                Property   System.Object IContentManagementAPI4_VaultStores {get;}
IContentManagementAPI5_Archive                    Property   KVS.EnterpriseVault.Interop.IArchive IContentManagement...
IContentManagementAPI5_ArchiveFolder              Property   System.Object IContentManagementAPI5_ArchiveFolder {get;}
IContentManagementAPI5_Archives                   Property   System.Object IContentManagementAPI5_Archives {get;}
IContentManagementAPI5_AuthenticationIdentity     Property   string IContentManagementAPI5_AuthenticationIdentity {g...
IContentManagementAPI5_AuthenticationIdentityType Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTH_TYPE IConte...
IContentManagementAPI5_AuthenticationMode         Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
IContentManagementAPI5_ConsumerGUID               Property   string IContentManagementAPI5_ConsumerGUID {get;set;}
IContentManagementAPI5_DirectoryDNSAlias          Property   string IContentManagementAPI5_DirectoryDNSAlias {get;set;}
IContentManagementAPI5_Hold                       Property   KVS.EnterpriseVault.Interop.IHold IContentManagementAPI...
IContentManagementAPI5_HoldGroups                 Property   System.Object IContentManagementAPI5_HoldGroups {get;}
IContentManagementAPI5_Holds                      Property   KVS.EnterpriseVault.Interop.IHolds IContentManagementAP...
IContentManagementAPI5_Item                       Property   KVS.EnterpriseVault.Interop.IItem IContentManagementAPI...
IContentManagementAPI5_Items                      Property   System.Object IContentManagementAPI5_Items {get;}
IContentManagementAPI5_LastError                  Property   System.Object IContentManagementAPI5_LastError {get;}
IContentManagementAPI5_VaultStore                 Property   System.Object IContentManagementAPI5_VaultStore {get;}
IContentManagementAPI5_VaultStores                Property   System.Object IContentManagementAPI5_VaultStores {get;}
IContentManagementAPI_Archive                     Property   KVS.EnterpriseVault.Interop.IArchive IContentManagement...
IContentManagementAPI_AuthenticationMode          Property   KVS.EnterpriseVault.Interop.EV_STG_API_AUTHENTICATE_USI...
IContentManagementAPI_DirectoryDNSAlias           Property   string IContentManagementAPI_DirectoryDNSAlias {get;set;}
IContentManagementAPI_Hold                        Property   KVS.EnterpriseVault.Interop.IHold IContentManagementAPI...
IContentManagementAPI_Holds                       Property   KVS.EnterpriseVault.Interop.IHolds IContentManagementAP...
IContentManagementAPI_Item                        Property   KVS.EnterpriseVault.Interop.IItem IContentManagementAPI...
IsAPIServerCompatible                             Property   bool IsAPIServerCompatible {get;}
Item                                              Property   KVS.EnterpriseVault.Interop.IItem Item {get;}
Items                                             Property   System.Object Items {get;}
LastError                                         Property   System.Object LastError {get;}
Setting                                           Property   System.Object Setting {get;}
Settings                                          Property   System.Object Settings {get;}
SourceItems                                       Property   System.Object SourceItems {get;}
VaultStore                                        Property   System.Object VaultStore {get;}
VaultStores                                       Property   System.Object VaultStores {get;}
 
Now the COM object :-
 
PS P:\> $EVAPI | get-member
 
 
   TypeName: System.__ComObject#{54de8640-2b41-47b2-bcf1-ab85bd6a64a5}
 
Name                           MemberType Definition
----                           ---------- ----------
IDispatchQueryInterface        Method     IDispatch IDispatchQueryInterface (IDispatch, string)
IsActiveEnterpriseVaultServer  Method     bool IsActiveEnterpriseVaultServer (string)
AdministrationLanguage         Property   string AdministrationLanguage () {get}
APIClientVersion               Property   string APIClientVersion () {get}
APIServerVersion               Property   string APIServerVersion () {get}
Archive                        Property   IArchive Archive () {get}
ArchiveFolder                  Property   IUnknown ArchiveFolder () {get}
Archives                       Property   IUnknown Archives () {get}
ArchiveType                    Property   IArchiveType ArchiveType () {get}
ArchiveTypes                   Property   IUnknown ArchiveTypes () {get}
AuthenticationIdentity         Property   string AuthenticationIdentity () {get} {set}
AuthenticationIdentityType     Property   EV_STG_API_AUTH_TYPE AuthenticationIdentityType () {get} {set}
AuthenticationMode             Property   EV_STG_API_AUTHENTICATE_USING AuthenticationMode () {get} {set}
ConsumerGUID                   Property   string ConsumerGUID () {get} {set}
DirectoryDNSAlias              Property   string DirectoryDNSAlias () {get} {set}
EnterpriseVaultServerInstalled Property   bool EnterpriseVaultServerInstalled () {get}
EnterpriseVaultServerName      Property   string EnterpriseVaultServerName () {get}
ExtensionContentProvider       Property   IUnknown ExtensionContentProvider () {get}
ExtensionContentProviders      Property   IUnknown ExtensionContentProviders () {get}
Hold                           Property   IHold Hold () {get}
HoldGroups                     Property   IUnknown HoldGroups () {get}
Holds                          Property   IHolds Holds () {get}
IsAPIServerCompatible          Property   bool IsAPIServerCompatible () {get}
Item                           Property   IItem Item () {get}
Items                          Property   IUnknown Items () {get}
LastError                      Property   IUnknown LastError () {get}
Setting                        Property   IUnknown Setting () {get}
Settings                       Property   IUnknown Settings () {get}
SourceItems                    Property   IUnknown SourceItems () {get}
VaultStore                     Property   IUnknown VaultStore () {get}
VaultStores                    Property   IUnknown VaultStores () {get}
 
 
 
 
And finally the $EVITEM :-
 
PS P:\> $EVITEM | get-member
 
 
   TypeName: System.__ComObject#{ba0d5a21-a3bc-447d-927c-ad849d379177}
 
Name             MemberType Definition
----             ---------- ----------
CanBeDeleted     Method     Variant CanBeDeleted ()
CopyTo           Method     void CopyTo (IItem)
Delete           Method     void Delete ()
Get              Method     void Get (int)
Insert           Method     void Insert ()
MoveTo           Method     void MoveTo (IItem)
Undelete         Method     bool Undelete ()
ArchiveId        Property   string ArchiveId () {get} {set}
ArchiveMetaData  Property   IArchiveMetaData ArchiveMetaData () {get}
BrowserViewURL   Property   string BrowserViewURL () {get}
Content          Property   IContent Content () {get}
CopyOptions      Property   EV_STG_API_ITEM_COPYOPTIONS CopyOptions () {get} {set}
DefaultMSGFormat Property   string DefaultMSGFormat () {get} {set}
DeletionLevel    Property   EV_API_DELETION_LEVEL DeletionLevel () {get} {set}
DeletionReason   Property   EV_STG_API_DELETION_REASON DeletionReason () {get} {set}
Format           Property   string Format () {get} {set}
Holds            Property   IHolds Holds () {get}
Id               Property   string Id () {get} {set}
InsertOptions    Property   int InsertOptions () {get} {set}
NativeItemURL    Property   string NativeItemURL () {get}
 
 
 
It's strange tha $EVITEM.Content actually loops back on itself :-
 
PS P:\> $EVITEM.content | get-member
 
 
   TypeName: System.__ComObject#{ba0d5a21-a3bc-447d-927c-ad849d379177}
 
Name             MemberType Definition
----             ---------- ----------
CanBeDeleted     Method     Variant CanBeDeleted ()
CopyTo           Method     void CopyTo (IItem)
Delete           Method     void Delete ()
Get              Method     void Get (int)
Insert           Method     void Insert ()
MoveTo           Method     void MoveTo (IItem)
Undelete         Method     bool Undelete ()
ArchiveId        Property   string ArchiveId () {get} {set}
ArchiveMetaData  Property   IArchiveMetaData ArchiveMetaData () {get}
BrowserViewURL   Property   string BrowserViewURL () {get}
Content          Property   IContent Content () {get}
CopyOptions      Property   EV_STG_API_ITEM_COPYOPTIONS CopyOptions () {get} {set}
DefaultMSGFormat Property   string DefaultMSGFormat () {get} {set}
DeletionLevel    Property   EV_API_DELETION_LEVEL DeletionLevel () {get} {set}
DeletionReason   Property   EV_STG_API_DELETION_REASON DeletionReason () {get} {set}
Format           Property   string Format () {get} {set}
Holds            Property   IHolds Holds () {get}
Id               Property   string Id () {get} {set}
InsertOptions    Property   int InsertOptions () {get} {set}
NativeItemURL    Property   string NativeItemURL () {get}

 

Nathan Clark 2's picture

This is a PS limitation

Item.Content is effectively the same COM object as Item (just a QueryInterface) and .NET/PowerShell only “sees” the default interface which is IItem.

PS> $EVITEM  | gm

PS> $EVITEMCONTENT = $EVITEM.Content

PS> $EVITEMCONTENT | gm

Show that you have the same PS/.NET object for $EVITEM and $EVITEMCONTENT.

So unless/until PowerShell provides easy usage/support for Com’s QueryInterface …its not going to work.

neil.doody-gl's picture

Yeah, I figured as much...to be honest.

 

Well I've made the whole script to work, I made a small VB wrapper that takes command lines and the powershell calls it to run the API.  I use EWS to extract he vault id and saveset ID, the VB/API to extract he MSG file locally and finally the outlook.application com object to import the MSG file into the correct folder.

Kind of messy but does what I need.  Thanks.