Advertisement
paulmir

Powershell Script for Microsoft eBook Giveaway

Jul 19th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ############################################################### 
  3. # Eric Ligmans Amazing Free Microsoft eBook Giveaway 
  4. # 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. # Link to download list of eBooks 
  6. # http://ligman.me/2tk1D2V
  7. # Thanks David Crosby for the template (https://social.technet.microsoft.com/profile/david%20crosby/)
  8. #
  9. # Modified by Robert Cain (http://arcanecode.me)
  10. # Added code to check to see if a book was already downloaded,
  11. # and if so was it the correct file size. If so, the book
  12. # download is skipped. This allows users to simply rerun the
  13. # script if their download process is interrupted.
  14. ############################################################### 
  15.  
  16. # Set the folder where you want to save the books to
  17. $dest = "C:\Book\" # Make sure the file path ends in a \
  18.  
  19. # Download the source list of books 
  20. $downLoadList = "http://ligman.me/2tk1D2V" 
  21. $bookList = Invoke-WebRequest $downLoadList 
  22.  
  23. # Convert the list to an array 
  24. [string[]]$books = "" 
  25. $books = $bookList.Content.Split("`n") 
  26.  
  27. # Remove the first line - it's not a book 
  28. $books = $books[1..($books.Length -1)] 
  29. $books # Here's the list 
  30.  
  31. # Get the total number of books we need to download
  32. $bookCount = $($books).Count
  33.  
  34. # Set a simple counter to let the user know what book
  35. # number we're currently downloading
  36. $currentBook = 0 
  37.  
  38. # As an option, we can have it log progress to a file
  39. $log = $true
  40.  
  41. if ($log -eq $true)
  42.  
  43. {
  44.  
  45.   # Construct a log file name based on the date that
  46.   # we can save progress to
  47.   $dlStart = Get-Date
  48.   $dlStartDate = "$($dlStart.Year)-$($dlStart.Month)-$($dlStart.Day)"
  49.   $dlStartTime = "$($dlStart.Hour)-$($dlStart.Minute)-$($dlStart.Second)"
  50.   $logFile = "$($dest)BookDlLog-$dlStartDate-$dlStartTime.txt"
  51.  
  52. }
  53.  
  54. # Download the books 
  55. foreach ($book in $books) 
  56.  
  57. { 
  58.  
  59.   # Increment current book number
  60.   $currentBook++
  61.   try
  62.  
  63.   {
  64.  
  65.     # Grab the header with the books full info
  66.     $hdr = Invoke-WebRequest $book -Method Head 
  67.  
  68.     # Get the title of the book from the header then
  69.     # make it a safe string (remove special characters)
  70.     $title = $hdr.BaseResponse.ResponseUri.Segments[-1] 
  71.     $title = [uri]::UnescapeDataString($title) 
  72.  
  73.    
  74.     # Construct the path to save the file to
  75.     $saveTo = $dest + $title 
  76.    
  77.     # If the file doesn't exist, download it
  78.     if ($(Test-Path $saveTo) -eq $false)
  79.  
  80.     {
  81.  
  82.       $msg = "Downloading book $currentBook of $bookCount - $title"
  83.       $msg
  84.       if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
  85.       Invoke-WebRequest $book -OutFile $saveTo 
  86.  
  87.     }
  88.  
  89.     else
  90.  
  91.     {
  92.  
  93.       # If it does exist, we need to make sure it wasn't
  94.       # a partial download. If the file size on the server
  95.       # and the file size on local disk don't match,
  96.       # redownload it
  97.    
  98.       # Get the size of the file from the download site
  99.       $dlSize = $hdr.BaseResponse.ContentLength
  100.       # Get the size of the file on disk
  101.       $fileSize = $(Get-ChildItem $saveTo).Length
  102.  
  103.       if ($dlSize -ne $fileSize)
  104.  
  105.       {
  106.  
  107.         # If not equal we need to download the book again
  108.         $msg = "Redownloading book $currentBook of $bookCount - $title"
  109.         $msg
  110.         if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
  111.         Invoke-WebRequest $book -OutFile $saveTo 
  112.  
  113.       }
  114.  
  115.       else
  116.  
  117.       {
  118.  
  119.         # Otherwise we have a good copy of the book, just
  120.         # let the user know we're skipping it.
  121.         $msg = "Book $currentBook of $bookCount ($title) already exists, skipping it"
  122.         $msg
  123.         if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
  124.  
  125.       }
  126.  
  127.     }
  128.  
  129.   } # end try
  130.  
  131.   catch
  132.  
  133.   {
  134.  
  135.     $msg = "There was an error downloading $title. You may wish to try to download this book manually."
  136.     Write-Host $msg -ForegroundColor Red
  137.     if ($log -eq $true) { "`n$($msg)" | Add-Content $logFile }
  138.  
  139.   } # end catch
  140.  
  141. } # end foreach 
  142.  
  143.  
  144. # Let user know we're done, and give a happy little beep
  145. # in case they aren't looking at the screen.
  146. "Done downloading all books"
  147. [Console]::Beep(500,300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement