Friday, July 29, 2016

Free eBooks from Microsoft

I just found out that Eric Ligman, Microsoft Director of Sales Excellence (whatever that is) is giving away a TON of free eBooks.  Head over to this site for the steaming poop: https://blogs.msdn.microsoft.com/mssmallbiz/2016/07/10/free-thats-right-im-giving-away-millions-of-free-microsoft-ebooks-again-including-windows-10-office-365-office-2016-power-bi-azure-windows-8-1-office-2013-sharepoint-2016-sha/

Now, one thing you will note is that you can either click on each eBook individually, or you can play leap-frog and click the "Download-All" button just to be hit with a "list" of the links.  A few folks posted complaints about that and so far no one has posted any real solution... except for me that is.

If you head over there, and it's been published, you'll see a little PowerShell script I whipped up and posted as a comment.  That script grabs the "list" and bulk downloads all the eBooks to a new folder on C:\ called "ebooks".  It's pretty straight forward. I'll repost it here since it could very well be usefull for many other things.

Clear-Host
#requires -version 3.0
$ErrorActionPreference = "SilentlyContinue"
If (!(Test-Path -Path "C:\eBooks")){New-Item -ItemType Directory -Force -Path "C:\eBooks" -Confirm:$false | Out-Null}
set-location "c:\eBooks"
$Source = "http://ligman.me/29zpthb"
$List = ((Invoke-WebRequest $Source) -split '[\r\n]') |? {$_}

Foreach ($URL in $List){
     Write-Host $URL -ForegroundColor Cyan
     $WebClientObject = New-Object System.Net.WebClient
     $WebRequest = [System.Net.WebRequest]::create($URL)
     $WebResponse = $WebRequest.GetResponse()
     $ActualDownloadURL = $WebResponse.ResponseUri.AbsoluteUri
     $ObjectProperties = @{'Shortened URL' = $URL;'Actual URL' = $ActualDownloadURL}
     $ResultsObject = New-Object -TypeName PSObject -Property $ObjectProperties
     $WebResponse.Close()
     $FullURL = $ResultsObject.'Actual URL'
     Write-Host 'Target URL : '$FullURL
     $Count = ($FullUrl.ToCharArray() | Where-Object {$_ -eq '/'} | Measure-Object).Count
     $FileName = ($FullURL.Split("/")[$Count]) -replace "%20"," "
     Write-Host 'Downloading: '$FileName
     Invoke-WebRequest $URL -OutFile "C:\eBooks\$FileName"
     Write-Host "--------------------------------"
}

Write-Host "--- COMPLETED ---" -ForegroundColor Red