Skip to content →

Posting to Basecamp using Powershell

Problem:  I want to report to the “business” from our webapplication without having to get involved in the massive nightly reporting process.

Solution: The “business” is already familar with Basecamp, so with a little Powershell and a couple of scheduled jobs we can post summary reports to Basecamp for everybody to see.

$request = "<request><post><category-id>10574251</category-id><title>This is a test message</title><body>This is the body of the message</body></post></request>"

$uri = "http://yourbasecampurl/projects/yourprojectid/posts.xml";
$username="username";
$password="password";
$usernamePassword = $username + ":" + $password;
$mycache = New-Object System.Net.CredentialCache;
$uri = New-Object System.Uri $uri ;
$credentials = New-Object System.Net.NetworkCredential($username,$password);
$mycache.Add( $uri,"Basic" ,$credentials);
$encoding = New-Object System.Text.ASCIIEncoding
$headerValue = "Basic " + [System.Convert]::ToBase64String( $encoding.GetBytes($usernamePassword)  );
$req = [System.Net.WebRequest]::Create($uri);
$req.Method = "POST";        
$req.ContentType = "application/xml";
$req.Credentials = $mycache;
$req.Headers.Add("Authorization",$headerValue);
$writer = New-Object System.IO.StreamWriter $req.GetRequestStream();
$writer.WriteLine($request);
$writer.Close();
$response = $req.GetResponse();
$receiveStream = $response.GetResponseStream();
$reader = New-Object System.IO.StreamReader($receiveStream, [System.Text.Encoding]::UTF8);

Write-Output $reader.ReadToEnd();

Published in Powershell

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *