Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $dest="C:\Users\$env:Username\Downloads\eBooks\"
  2.  
  3. # Find Book Titles
  4. $webPage = 'https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepo/'
  5. $webPageHTML = Invoke-WebRequest -URI $webPage
  6. $tableList = $webPageHTML.ParsedHtml.body.getElementsByTagName('td') | where width -eq "673"
  7. $webPageLinks = $webPageHTML.ParsedHtml.body.getElementsByTagName('td') | where innerHTML -match '<a'
  8.  
  9. # Create folder for each title and download the respective files into said folder
  10. for($i = 1; $i -lt $tableList.length; $i++){
  11.     [System.Collections.ArrayList]$downloadList = @()
  12.  
  13.     # Create folder for each book title
  14.     New-Item -Path $dest -Name $tableList[$i].innerText.replace(":","-") -ItemType Directory
  15.  
  16.     # Pull each link out of HTML source and add to download queue
  17.     $explodedLinks = $webPageLinks[$i-1].innerHTML.Split('"')
  18.     foreach($link in $explodedLinks){
  19.         if($link.StartsWith('http://')){
  20.             $downloadList.add($link)
  21.         }
  22.     }
  23.  
  24.     # Download each file associated with the current title
  25.     foreach($download in $downloadList){
  26.         $hdr = Invoke-WebRequest $download -Method Head
  27.         $title = $hdr.BaseResponse.ResponseUri.Segments[-1]
  28.         $title = [uri]::UnescapeDataString($title)
  29.         $saveTo = $dest + $tableList[$i].innerText.replace(":","-") + "\" + $title
  30.         Invoke-WebRequest $download -OutFile $saveTo
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement