Exchange Web Services

Started by mathia, February 07, 2017, 10:24:12 AM

Previous topic - Next topic


I'm wondering if anyone here has been able to get this working in Winbatch. I have made some intial attempts, but I'm kinda lost.  Trying to convert from Powershell.
Here's what I'm working with in PS:

Import-Module -Name "c:\Program Files (x86)\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"

# Create a new Exchange Service Object
$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

# Set the Credentials
#$exchservice.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList, "Cor3Valu3s2016!"
$exchservice.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList  "something\cash", "Cor3Valu3s2016!"
#Set the URL for the service
$exchservice.AutodiscoverUrl('', {$true})
$exchService.Url= new-object Uri("")

My code so far in Winbatch:

assemblydir = 'c:\Program Files (x86)\Microsoft\Exchange\Web Services\1.2\'
ObjectClrOption ('use','Microsoft.Exchange.WebServices, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35')

service = Objectclrnew('Microsoft.Exchange.WebServices.Data.ExchangeService')

setver and service both return object references, but when I try to set parameters such as url or exchange version, I always get an error. I'm assuming I don't know the correct syntax for this to work. I'm hoping to find someone who has been through this and can point me in the right direction.


In the future when you post something like "I got an error", also post the error number and any additional error information that is displayed by clicking the "More Error Info" button when enabled.   

CLR methods and properties are strongly type so whenever you encounter a problem, parameter and right-hand side type information is usually the first place to check.  There are several ways to solve type related problems.  Sometimes all you need to do is specify the COM Automation type with a method parameter and WinBatch will convert it to the correct FCL system type.   There are times when this is not sufficient and you need to specify the FCL system type like "system.string" or whatever.  There are times when this is not sufficient (enum types being the most common) and you need to use the ObjectClrType function to create a properly typed value to pass to a class object method.  The function ObjectClrType is also useful for type casting the right-hand side of class object property assignments.   About the only way to discover and fix typing issues is through trial-and-error.  Once you get the hang of it, it becomes relatively straight forward and often obvious.

There are numerous example in the Tech Database that should help you get started.  Try searching on 'dotNet' or 'ObjectClrNew' for starters.

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade


Here's an unrelated example of using a strongly typed enum value as a parameter to a class constructor.

Code (winbatch) Select
ObjectClrOption ( 'use', 'System.Core, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089')

;; The following line creates an enum 'object'.
enumPathType   = ObjectClrNew('System.Diagnostics.Eventing.Reader.PathType')

;; The following line creates a type object with the desired enum enumerator value.
LogName         = ObjectClrType('System.Diagnostics.Eventing.Reader.PathType', enumPathType.LogName)

strQuery        = `<QueryList><Query Id="0" Path="Microsoft/Windows">`
strQuery        = strQuery:`<Select Path="Microsoft-Windows-Diagnostics-Performance/Operational">`
strQuery        = strQuery:` *[System/Opcode=34]</Select></Query></QueryList>`

;; The following line uses the typed enum value as a parameter to the class constructor for the 'EventLogQuery' class. 
objLogQuery     = ObjectClrNew('System.Diagnostics.Eventing.Reader.EventLogQuery','Microsoft', LogName, strQuery)

objEventReader  = ObjectClrNew('System.Diagnostics.Eventing.Reader.EventLogReader', objLogQuery )

strDescription = ''
While 1
   objEvent = objEventReader.ReadEvent()
   if ObjectTypeGet(objEvent) == "EMPTY" then break
   id = objEvent.Id
   strDescription := objEvent.FormatDescription():@Lf

Pause("Boot List", strDescription)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade