{"id":5114,"date":"2012-08-17T00:01:00","date_gmt":"2012-08-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/17\/use-powershell-3-0-to-easily-download-60-spanned-files\/"},"modified":"2012-08-17T00:01:00","modified_gmt":"2012-08-17T00:01:00","slug":"use-powershell-3-0-to-easily-download-60-spanned-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-3-0-to-easily-download-60-spanned-files\/","title":{"rendered":"Use PowerShell 3.0 to Easily Download 60 Spanned Files"},"content":{"rendered":"<p><b>Summary<\/b>: 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.\nMicrosoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog from Windows PowerShell MVP, Marco Shaw. Take it away Marco&hellip;\nI was pretty excited when I recently saw that Microsoft was releasing packaged evaluation VHDs of their latest System Center&nbsp;2012&nbsp;SP1 CTP2 release (SP1 will support Windows Server&nbsp;2012). Here&rsquo;s one example of where you can find <a href=\"http:\/\/blogs.technet.com\/b\/momteam\/archive\/2012\/07\/05\/downloadable-virtual-machines-for-system-center-2012-evaluation-now-available.aspx\" target=\"_blank\">the download links<\/a>.\nI 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&rsquo;t a single download available for each product. Instead, each product was roughly a dozen files&mdash;12 times 5 means 60 files to download.\nNow, 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&rsquo;d have to start with some complicated regular expressions, and I hate&mdash;OK, you caught me&mdash;I&rsquo;m not good at them. So I decided that I&rsquo;d get around to this later&mdash;or even better, maybe there&rsquo;d be a one-click download added later.\nI 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&hellip;\nI had a prerelease version of Windows PowerShell&nbsp;3.0 on my laptop, and I decided to roll up my sleeves. I couldn&rsquo;t remember the cmdlet offhand; but eventually, I arrived at <b>Invoke-WebRequest<\/b>. So I passed off one of the URLs directly to the cmdlet as follows:<\/p>\n<p style=\"padding-left: 30px\">PS&gt; invoke-webrequest &ldquo;http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2&rdquo;\nI took a quick peak at the output, because it wasn&rsquo;t just raw HTML. What&rsquo;s this?! A <b>Links<\/b> property! Could it be? The cmdlet already parsed out all of the links from the page for me?\nA bit more Windows PowerShell magic:<\/p>\n<p style=\"padding-left: 30px\">PS&gt; invoke-webrequest &ldquo;http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2&rdquo;|select -exp links|where{$_.href -like &#8220;*.rar&#8221; -or $_.href -like &#8220;*.exe&#8221;}|select -exp href\nOh 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.<\/p>\n<p style=\"padding-left: 30px\">PS&gt; invoke-webrequest &ldquo;http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2&rdquo;|select -exp links|where{$_.href -like &#8220;*.rar&#8221; -or $_.href -like &#8220;*.exe&#8221;}|select -exp href\nAll I needed to do with the last command is to invoke one of the Background Intelligent Transfer Service (BITS) cmdlets to download:<\/p>\n<p style=\"padding-left: 30px\">PS&gt; invoke-webrequest &ldquo;http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=4da3d1d9-91d4-472e-acb7-10885df9d1c2&rdquo;|select -exp links|where{$_.href -like &#8220;*.rar&#8221; -or $_.href -like &#8220;*.exe&#8221;}|select -exp href|foreach{start-bitstransfer $_ C:usersmy_home}\nThat was it, and my download started. Oh, make sure you download to a drive that has enough free disk space.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp;&nbsp;For some reason, the previous full code does not seem to work on Windows Server&nbsp;2012&nbsp;RC.\n~Marco\nThank you, Marco, for an interesting Windows PowerShell command that should come in very useful for a lot of people.\nJoin me tomorrow when I will talk about the new release of the Script Explorer.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip; I was pretty excited when I recently [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[166,56,256,3,167,45],"class_list":["post-5114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-downloading-files","tag-guest-blogger","tag-marco-shaw","tag-scripting-guy","tag-using-the-internet","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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&hellip; I was pretty excited when I recently [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=5114"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=5114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}