James O'Neill's Blog

November 30, 2007

A blog post about using PowerShell to get blog posts (about PowerShell)

Filed under: How to,Powershell — jamesone111 @ 4:28 pm

I guess the thing I like best about PowerShell its ability to make the business of getting stuff on the screen so easy. I’ve used format-List and Format-Table a lot. And looking for something else I’d come across the “Write-progress” cmdlet.

Now … I’ve been playing with a script I found on Lee Holmes’ blog.  Lee works for us and is author of the powershell cookbook  – the library here in Microsoft-UK has ordered it but I haven’t seen it yet. The script gets blog posts using the metaWeblog api

With community server I put in

.\get-blogposts -posturl "http://blogs.technet.com/metablog.ashx" -blogID "jamesone" -username jamesone -password LikeIWillPutItHere  -numberofposts 5000 

And it returns a bunch of objects, one for each of my blog posts (all 400+) FANTASTIC. But, as you can imagine, it takes some time. It would be really good to have a progress indicator, so this seemed like time  to try out Write progress

Lee’s code makes some XML, sends it to the server and the converts the string that comes back into XML – like this

$responseContent =  (new-object System.Net.WebClient).UploadString($postUrl, $postTemplate) 

$results = [xml] $responseContent

Both operations are time consuming – personally I would probably have written

$results = [xml]  (new-object System.Net.WebClient).UploadString($postUrl, $postTemplate) 

But Lee’s way makes it dead easy to put in the Write-progress, thus

Write-progress "Getting blog posts" "Fetching from server" 

$responseContent =  (new-object System.Net.WebClient).UploadString($postUrl, $postTemplate)
write-progress "Getting blog posts" "Checking XML"
$results = [xml] $responseContent

and if I am piping the posts to something else I want to be able to see which of my 400 posts we’ve reached,so just before returning each post I insert another write-progress

write-progress "Getting blog posts" "Checking XML" -cu $blogEntry.title  

Of course, having changed the script, I might not want the progress output after all – in which case I can change $ProgressPreference from  “Continue” to “Silentlycontinue”. I love these details that I keep finding in Powershell, not only does it handle something which is always a pain to do in a script , but I don’t have to code in a “-silent” option with an if statement every time I wanted to output progress.  

Technorati tags:

This post originally appeared on my technet blog.


Create a free website or blog at WordPress.com.

%d bloggers like this: