Sunday, September 18, 2011

The Build Videos–Getting Them With PowerShell

I’m just back from the //BUILD/ concerence in Anaheim – where I missed far more sessions than I could have ever hoped to attend. It was 3 1/2 days of the fire hose – I get tired just thinking about it. Luckily for us, Microsoft has taped every session and has put them up for download.

By day 2, superstar PowerShell guru James Brundage had created a great PowerShell function to download the whole set.

Get-EnclosureFile -Directory $home\Videos\Build -Feed http://channel9.msdn.com/Events/BUILD/BUILD2011/RSS

function Get-EnclosureFile  {
<#
    .Synopsis
        Downloads enclosure files from a feed
    .Author
        James Brundage
    .Description
        Downloads enclosure files from a RSS feed using the BitsTransfer module
    .Example
        Get-EnclosureFile -Directory $home\Videos\Build -Feed
http://channel9.msdn.com/Events/BUILD/BUILD2011/RSS
    #>

param(
# The directory where the files should go
[string]$Directory,
# The Feed
[uri]$Feed
)

begin {
Import-Module BitsTransfer –Global
}

process {
New-Item -ErrorAction SilentlyContinue $directory -ItemType Directory
Push-Location $directory
$Rss =(New-Object Net.Webclient).DownloadString("$Feed")
$xfer = $Rss |
    Select-Xml //item/enclosure |
        ForEach-Object {
            $url = [uri]$_.Node.Url
            $destinationFile = $_.Node.ParentNode.Title
            foreach ($char in [io.path]::GetInvalidFileNameChars()) {
                $destinationFile = $destinationFile.Replace("$char", "")
            }
            $destinationExtension= $url.Segments[-1].Substring($url.Segments[-1].LastIndexOf("."))
            if (-not (Test-Path $destinationFile)) {
                Start-BitsTransfer -Source $url -Description $_.Node.ParentNode.Title -Destination "${destinationFile}$destinationExtension"
            }
        }

    Pop-Location
    }
}

To call the function, just do something like this:

Get-EnclosureFile -Directory $home\Videos\Build -Feed http://channel9.msdn.com/Events/BUILD/BUILD2011/RSS

Of course, you better have a lot of disk space (I’ve taken up 21GB so far!). Oh – you also need a reliable connection (and you have Windows Bloody Update not decide to reboot).

Enjoy!

2 comments:

John J. Kavanagh said...

Hmmm odd ... the Technorati line before the closing bracket of the function causes issues....

Thomas said...

thanks john - fixed!