I recently got a question on Twitter if I could create a PowerShell script to download files from TechNet Edge. I thought this would be useful for many other people as well, so I have spent some time this weekend working on this script. I wanted to make the script as generic and re-usable as possible, so even though I started hardcoding it for downloading files from a given RSS-feed from TechNet Edge, I later on generalized it to make it easy to re-use on other websites as well. The script is called Get-WebVideoFile.ps1, and is available on both PoshCode and the Microsoft TechNet Script Center.
How to use the script
1) The script requires PowerShell 2.0 and you must have configured your PowerShell execution policy in order to run the script. Run $host.version from PowerShell to see what version you are running. If you need help configuring the execution policy or running the script, see “Running Windows PowerShell Scripts” on Microsoft TechNet.
3) Find the RSS URL for the category you want to download files from. On TechNet Edge you can find an overview of all categories (called tags) on this link.
In this example I have chosen the Microsoft Virtual Academy tag:
You can then find the RSS URL by clicking on the RSS icon to the right of the title on the top of the page.
4) Specify the URL on the RssUrl parameter when running the Get-WebVideoFile script:
You will get a progress bar which shows the progress for the current file:
Remember that you can also use Windows PowerShell ISE (or any 3rd party host) instead of the PowerShell console host:
More information about the script
Comment based help
At the top of the script you can see that I have added comment based help. This means you can use Get-Help to show information and example usage of the script:
RssUrl – This is the only mandatory parameter, which provides the script the RSS feed to go through. The script download the HTML code from each RSS entry
s link, and look for a URL that matches the regular expression.
Destination – The folder the files will be downloaded to. If not specified the files will be downloaded to the current users Video-folder (for example C:UsersUsernameVideos).
UseOriginalFileName – This is a switch parameter to specify that you want to use the original file names for the downloaded files. On TechNet Edge most video files got a non-descriptive name, for example e1ef34ca-cdef-463d-9c3f-94aa606bc6ab.wmv. Because of this I set the default behavior of the script to use the RSS item title as the file name.
UrlRegex – This is the regular expression that will be used to search for the video URL from the HTML code from each RSS item link. You can re-use the default regex and replace “wmv” with for example “mp4” if you want to download the MP4-versions of the videos. If you are interested in learning about Regular Expressions, I can recommend the book “Master Regular Expressions” by Jeffrey E.F. Friedl. If you want a tool to help you build the regular expression I can recommend RegexBuddy.
Verbose – This is not a parameter I have added, it is one of the common parameters in PowerShell which I want to highlight since I have added some verbose information. For example if the regular expression does not match, you can see it in the verbose output. This is a switch parameter, so you can add “-Verbose” to the script parameters to use it. You can also read more about the Verbose parameter in the about_CommonParameters help topic.
The download process
Initially when creating the script I started using the DownloadFile method of the Net.WebClient class in .NET. This worked fine, but did not provide any progress status during download. This makes it hard for the user of the script to know how long the download will take, and if anything is happening at all. I then turned to Background Intelligent Transfer Service (BITS), and the Start-BitsTransfer cmdlet which provides progress status as well as other features. For some reason the BITS jobs failed with the error message “HTTP status 404: The requested URL does not exist on the server.”. I am not sure why, but this may be because the server-side on TechNet Edge downloads does not support the necessary HTTP Requirements.
The script will check the destination folder to see if the file is already downloaded, which means you can run the script several times without downloading files you already have. This is also useful when running the script as a scheduled task, which may be of interest if you want to automatically download new videos in a specific category.
Update 17.06.2012: To download videos from TechEd North America 2012 the URL regex needs to be updated. Here is an example for downloading from the WMV RSS-feed: .\Get-WebVideoFile.ps1 -UrlRegex “(?<url>http://video.ch9.ms/teched/2012/na/(?<file>[^>]*?wmv))” -RssUrl “http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/RSS/wmv”
MMS 2012 Download Sessions – Offline Viewing (PowerShell script to download sessions from MMS 2012)