Share via


HOW TO: Using WebDAV from PowerShell 1.0

I was just trying my hands on PowerShell and wanted to try using WebDAV from it. I wrote a small sample that searches the inbox of a user for unread mails and lists out the Href and the Subject of the email.

Create a .PS1 file as below:

 #Change the Server name and Credentials
[System.String]$serverName ="EX200701"
[System.String]$UserName="Administrator"
[System.String]$Password = "TestLab!"
[System.String]$UserDomain ="mycompany"

[System.Net.HttpWebRequest]$Request
[System.Net.WebResponse]$Response
[System.Net.CredentialCache]$MyCredentialCache
[System.IO.Stream]$RequestStream
[System.IO.Stream]$ResponseStream
[System.Xml.XmlDocument]$ResponseXmlDoc
[System.Xml.XmlNodeList]$SubjectNodeList
[System.Xml.XmlNodeList]$HrefNodeList
[System.String]$strQuery
[byte[]]$bytes

write-host("Accessing Inbox for User:" + $UserName + " using URI " + $URI +"..") -ForeGroundColor Yellow
$URI = "https://" + $serverName + "/exchange/" + $UserName + "/Inbox/"
$Request = [System.Net.HttpWebRequest]::Create($URI)

#Set the Credentials
write-host("Setting the Credentials now..") -ForeGroundColor Yellow
[System.Net.NetworkCredential]$NetworkCredential = New-Object System.Net.NetworkCredential($UserName, $Password, $UserDomain)
$MyCredentialCache = New-Object System.Net.CredentialCache
$MyCredentialCache.Add($URI, "Basic", $NetworkCredential)
$Request.Credentials = $MyCredentialCache

#Set the headers.    
write-host("Setting the Headers now..") -ForeGroundColor Yellow
$Request.ContentType = "text/xml"
$Request.Method = "SEARCH"
$Request.Timeout = 300000
    
$strQuery = "<?xml version=""1.0""?><g:searchrequest xmlns:g=""DAV:"">"
$strQuery = $strQuery +    "<g:sql>SELECT ""urn:schemas:httpmail:subject"" FROM Scope('SHALLOW TRAVERSAL OF """ + $URI + """') WHERE"
$strQuery = $strQuery +    " ""urn:schemas:httpmail:read"" = false</g:sql></g:searchrequest>"

$bytes = [System.Text.Encoding]::UTF8.GetBytes($strQuery)
$Request.ContentLength = $bytes.Length
$RequestStream = $Request.GetRequestStream()
$RequestStream.Write($bytes, 0, $bytes.Length)
$RequestStream.Close()
$Response = $Request.GetResponse()

#Check the Status
[System.int32]$intStatusCode = $Response.StatusCode

if ($intStatusCode -ge 200 -and $intStatusCode -lt 300)
{
    $ResponseStream = $Response.GetResponseStream()
    [System.IO.StreamReader]$streamReader = New-Object System.IO.StreamReader $ResponseStream
    [System.String]$strResponse = $streamReader.ReadToEnd()
    $ResponseStream.Close()
    $ResponseXmlDoc = New-Object System.Xml.XmlDocument
    $ResponseXmlDoc.LoadXml($strResponse)
    $HrefNodeList = $ResponseXmlDoc.GetElementsByTagName("a:href")
    $SubjectNodeList = $ResponseXmlDoc.GetElementsByTagName("d:subject")
        
    if($HrefNodeList.Count -gt 0)
    {
        for($i = 0; $i -lt $HrefNodeList.Count; $i++)
        {
            #Write Out the Href and the Subject of the mail
            write-host("HREF:" + $HrefNodeList.Item($i).psbase.InnerText) -ForeGroundColor Yellow
            write-host("Subject: " + $SubjectNodeList.Item($i).psbase.InnerText) -ForeGroundColor Yellow
            write-host("")
        }
    }
    
}

$Response.Close()

To Run the script:

1)Open the Windows PowerShell Console

2)Navigate to the location where you have stored the script file.

3)Type in .\ScriptName.PS1 and hit enter to execute the script.