Solved

Exchange 2007 Web Services Error with VBScript

Posted on 2012-03-20
2
1,451 Views
Last Modified: 2012-03-22
Ok so I'm a VBScript guy and never learned .NET so that being said what I'm trying to do is connect to a particular mailbox and download attachments from any email in the inbox.  I've made some real good progress with the Exchange Web Services SOAP requests.  I can use the MSXML2.XMLHTTP object to POST to the URL of https://MAILSERVER/ews/exchange.asmx and I'm posting the XML listed in Microsofts documentation for certain functions  (http://msdn.microsoft.com/en-us/library/aa494316(v=exchg.140).aspx).  

I can view all the email in the inbox (FindItem), I can view the details of each email (GetItem) but I'm failing when I'm trying to get the details of the attachment from the email (GetAttachment).  The error message I'm getting is:

Server was unable to process request.  System.Xml.XmlException:  'xmlns' is an unexpected token.  Expecting white space.  Line 1, position 107.

I think this is saying the error is in the response but I can't see what the response text is so I have no idea what the error is.  My end goal is I'm simply trying to download the attachments (which will always be PDF's in my case) and I can only use VBScript.  Any help or direction would be GREATLY appreciated!  Here is my code:


const TARGETURL="https://MAILSERVER/ews/exchange.asmx"
const USERNAME="domain\username"
const PASSWORD="password"
GetAttachmentTemplate = "C:\GetAttachment.xml"

set fso=CreateObject("Scripting.FileSystemObject")

'---------------------------------------------------------
'	Read contents of GetAttachment.xml file
'---------------------------------------------------------
set oTS=fso.OpenTextFile(GetAttachmentTemplate)
sTemplate = ""
while not oTS.AtEndOfStream
	sTemplate=sTemplate & oTS.ReadLine
wend
oTS.Close


'---------------------------------------------------------
'	POST to Exchange Server for Attachment
'---------------------------------------------------------
set oXMLHTTP=CreateObject("MSXML2.XMLHTTP")
set oXML=CreateObject("MSXML2.DOMDocument")
	
oXMLHTTP.Open "POST", TARGETURL, false, USERNAME, PASSWORD
oXMLHTTP.SetRequestHeader "Content-Type", "text/xml"
oXMLHTTP.Send sTemplate
	
if oXMLHTTP.Status="200" then

	if oXML.LoadXML(oXMLHTTP.ResponseText) then
		set CreateItemResponse = oXML
		wscript.echo "It Worked"
	end if

else
	wscript.echo "It did NOT work.  Response status: " & oXMLHTTP.Status & VbCrLf & oXMLHTTP.StatusText & VbCrLf & oXMLHTTP.ResponseText
end if

Open in new window

0
Comment
Question by:tphelps19
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
tphelps19 earned 0 total points
ID: 37753284
Well I think I figured the answer out on my own.  I have no idea what  I did differently but the GetAttachment function now works and returns the attachment data in the <t:Content> element of the XML response.  That data is a MIME type data string but the problem is that it's encoded in base64 and since I'm running VBScript (which is 32bit) I have to use a base64 decoder.  I found a great one called XStandard Base64 Component.

BUT wait......not done yet.  So the other problem is I'm running this script on a 64bit server and the COM object (or dll file) from the link above is actually 32bit.  So even more fun.....I had to figure out how to run a 32bit COM object on a 64bit server.  Thanks to a helpful link on GFI's Webpage explaining how to do it by making a small registry tweak.  I figured it out but it wasn't the easiest instructions to follow so here are a little easier ones:

1)  Locate your COM object GUID under the HKey_Classes_Root\Wow6432Node\CLSID\[GUID]
      *Easiest way is to search for "XStandard" from the HKey_Classes_Root\Wow6432Node\CLSID\

2)  Once located add a new REG_SZ (string) Value.
      *Name is "AppID" (without quotes) and Data should be COM object GUID from step #1.
      *The GUID needs to include the "{" and "}" at the end.  ex: {118BAD5F-AB34-4140-9C78-0AB786C8A66D}

3)  Add a new key under HKey_Classes_Root\Wow6432Node\AppID\  and call it the same as the COM object GUID.
      *Again, make sure to include the "{" and "}"

4)  Under the new key you just added, add a new REG_SZ (string) Value, and call it "DllSurrogate" and leave the Value empty.

5)  Create a new Key under HKey_Local_Machine\Software\Classes\AppID\
      *Again the new key should be called the same as the COM object’s GUID.  Leave the Value empty.
      *If you get an error saying a key with that name already exists then you can skip this step, that happened to me for some reason.


That's it.  I was able to run the script anad all is well.  :)
0
 
LVL 2

Author Closing Comment

by:tphelps19
ID: 37753287
Kind of wished I got a little help though.......
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question