Geeks With Blogs

Connected Systems Chilled Out Blog Hanging stuff together in a meaningful way with some fun added

I recently had to come up with a script to check a particular shared mailbox to see if there were any messages greater than 2 hours old. The following Power shell script was my prototype.

cls
# ------------------------------------------------------------
# Setup credentials
# ------------------------------------------------------------
$servername = "ex02"
$objUserCredentials = new-object System.Net.NetworkCredential("AndrewJames57", "password1#", "Solidsoft")
$dateNow = get-date
$dateNow.ToString()
# ------------------------------------------------------------
# Setup Soap Request
# ------------------------------------------------------------
$requestSoapMessage  = "<?xml version='1.0' encoding='utf-8'?>" `
+ "<soap:Envelope xmlns:soap=`"http://schemas.xmlsoap.org/soap/envelope/`" " `
+ " xmlns:xsi=`"http://www.w3.org/2001/XMLSchema-instance`" xmlns:xsd=`"http://www.w3.org/2001/XMLSchema`"" `
+ " xmlns:t=`"http://schemas.microsoft.com/exchange/services/2006/types`" >" `
+ "<soap:Body>" `
+ "<FindItem xmlns=`"http://schemas.microsoft.com/exchange/services/2006/messages`" " `
+ "xmlns:t=`"http://schemas.microsoft.com/exchange/services/2006/types`" Traversal=`"Shallow`"> " `
+ "<ItemShape>" `
+ "<t:BaseShape>AllProperties</t:BaseShape>" `
+ "</ItemShape>" `
+ "<Restriction>" `
+ "<t:And>" `
+ "<t:IsEqualTo>" `
+ "<t:FieldURI FieldURI=`"message:IsRead`"/>"`
+ "<t:FieldURIOrConstant>" `
+ "<t:Constant Value=`"0`"/>" `
+ "</t:FieldURIOrConstant>" `
+ "</t:IsEqualTo>" `
+ "<t:IsLessThan>" `
+ "<t:FieldURI FieldURI=`"item:DateTimeSent`"/>"`
+ "<t:FieldURIOrConstant>" `
+ "<t:Constant Value=`"" + $dateNow.ToUniversalTime().AddHours(-2).ToString("yyyy-MM-ddThh:mm:ssZ")  + "`"/>"`
+ "</t:FieldURIOrConstant>"`
+ "</t:IsLessThan>"`
+ "</t:And>"`
+ "</Restriction>"`
+ "<ParentFolderIds>" `
+ "<t:DistinguishedFolderId Id=`"inbox`"/>" `
+ "</ParentFolderIds>" `
+ "</FindItem>" `
+ "</soap:Body></soap:Envelope>"
# ------------------------------------------------------------
# End of Soap Request setup
# ------------------------------------------------------------

# ------------------------------------------------------------
# Web service request
# ------------------------------------------------------------
$webserviceRequest = [System.Net.WebRequest]::Create($rootURI)
$webserviceRequest.ContentType = "text/xml"
$webserviceRequest.Headers.Add("Translate", "F")
$webserviceRequest.Method = "Post"
$webserviceRequest.Credentials = $objUserCredentials
$bytes = [System.Text.Encoding]::UTF8.GetBytes($requestSoapMessage)
$webserviceRequest.ContentLength = $bytes.Length
$webserviceRequestStream = $webserviceRequest.GetRequestStream()
$webserviceRequestStream.Write($bytes, 0, $bytes.Length)
$webserviceRequestStream.Close()

# ------------------------------------------------------------
# Receive response
# ------------------------------------------------------------
$webserviceResponse = $webserviceRequest.GetResponse()
$webserviceResponseStream = $webserviceResponse.GetResponseStream()
$ResponseXmlDoc = new-object System.Xml.XmlDocument
$ResponseXmlDoc.Load($webserviceResponseStream)
$subjectnodes = @($ResponseXmlDoc.getElementsByTagName("t:Subject"))
$FromNodes = @($ResponseXmlDoc.getElementsByTagName("t:Name"))
$SentNodes = @($ResponseXmlDoc.getElementsByTagName("t:DateTimeSent"))
$SizeNodes = @($ResponseXmlDoc.getElementsByTagName("t:Size"))

# ------------------------------------------------------------
# Output process
# ------------------------------------------------------------
"Title"
for($i=0;$i -lt $subjectnodes.Count;$i++){
    $Senttime = [System.Convert]::ToDateTime($SentNodes[$i].'#text'.ToString())
    $Senttime = $Senttime.ToString()  + "    " + $FromNodes[$i].'#text' + "    " + $subjectnodes[$i].'#text' + "    " + $SizeNodes[$i].'#text'
    $Senttime.ToString()   
    }
# ------------------------------------------------------------
#
# ------------------------------------------------------------

Posted on Friday, April 24, 2009 11:06 AM | Back to top


Comments on this post: Power shell: Checking for unread emails in an exchange inbox

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © BizTalk Visionary | Powered by: GeeksWithBlogs.net