Use PowerShell 3.0 to Easily Download 60 Spanned Files

Doctor Scripto

Summary: Windows PowerShell MVP, Marco Shaw, talks about using a Windows PowerShell 3.0 cmdlet to download 60 virtual machine files from the Microsoft download site. Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog from Windows PowerShell MVP, Marco Shaw. Take it away Marco… I was pretty excited when I recently saw that Microsoft was releasing packaged evaluation VHDs of their latest System Center 2012 SP1 CTP2 release (SP1 will support Windows Server 2012). Here’s one example of where you can find the download links. I thought that I could have the entire suite downloaded with about five clicks (App Controller, Operations Manager, Orchestrator, Service Manager, and Virtual Machine Manager). Much to my dismay, when I looked at the links, I found out that there wasn’t a single download available for each product. Instead, each product was roughly a dozen files—12 times 5 means 60 files to download. Now, I like to pride myself in trying to automate things, and this was something I wanted to try to script. At that time, I figured I’d have to start with some complicated regular expressions, and I hate—OK, you caught me—I’m not good at them. So I decided that I’d get around to this later—or even better, maybe there’d be a one-click download added later. I just started to look at getting the new MCSE Server Infrastructure certification, so I decided that it is time to face my demons and start building that regex. I was going to need that regex to do some parsing of the pages so I could pull out the URLs to the downloads. Or so I thought… I had a prerelease version of Windows PowerShell 3.0 on my laptop, and I decided to roll up my sleeves. I couldn’t remember the cmdlet offhand; but eventually, I arrived at Invoke-WebRequest. So I passed off one of the URLs directly to the cmdlet as follows:

PS> invoke-webrequest “http://www.microsoft.com/downloads/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2” I took a quick peak at the output, because it wasn’t just raw HTML. What’s this?! A Links property! Could it be? The cmdlet already parsed out all of the links from the page for me? A bit more Windows PowerShell magic:

PS> invoke-webrequest “http://www.microsoft.com/downloads/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2”|select -exp links|where{$_.href -like “*.rar” -or $_.href -like “*.exe”}|select -exp href Oh my, jackpot! I have string-based objects that give me the direct links to all of the downloads for App Controller. It was that easy.

PS> invoke-webrequest “http://www.microsoft.com/downloads/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2”|select -exp links|where{$_.href -like “*.rar” -or $_.href -like “*.exe”}|select -exp href All I needed to do with the last command is to invoke one of the Background Intelligent Transfer Service (BITS) cmdlets to download:

PS> invoke-webrequest “http://www.microsoft.com/downloads/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2”|select -exp links|where{$_.href -like “*.rar” -or $_.href -like “*.exe”}|select -exp href|foreach{start-bitstransfer $_ C:usersmy_home} That was it, and my download started. Oh, make sure you download to a drive that has enough free disk space.

Note   For some reason, the previous full code does not seem to work on Windows Server 2012 RC. ~Marco Thank you, Marco, for an interesting Windows PowerShell command that should come in very useful for a lot of people. Join me tomorrow when I will talk about the new release of the Script Explorer. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon