Created
July 18, 2017 17:10
-
-
Save vongrippen/d36f0110b0b8a986ad54226853493d33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$destinationDir = "C:\Users\$env:Username\eBooks\" | |
$URI = "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/" | |
$HTML = Invoke-WebRequest -Uri $URI | |
$Elements = $HTML.AllElements | |
$Parsed = $HTML.ParsedHtml | |
$table = $Parsed.getElementsByTagName("tbody")[0] | |
$tr = $table.getElementsByTagName("tr") | |
Function Remove-InvalidFileNameChars { | |
param( | |
[Parameter(Mandatory=$true, | |
Position=0, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[String]$Name | |
) | |
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join '' | |
$re = "[{0}]" -f [RegEx]::Escape($invalidChars) | |
return ($Name -replace $re) | |
} | |
ForEach ($row in $tr) { | |
# Loop through the rows | |
$cells = $row.getElementsByTagName("td") | |
if ($cells[0].innerText -ne "Category") { | |
$category = $cells[0].innerText.Replace("\", " ").Replace("/"," ") | Remove-InvalidFileNameChars | |
$name = $cells[1].innerText.Replace("\", " ").Replace("/"," ") | Remove-InvalidFileNameChars | |
$links = $cells[2].getElementsByTagName("a") | |
$destination = $destinationDir + $category + "\" + $name + "\" | |
New-Item $destination -ItemType Directory | |
ForEach ($link in $links) { | |
$href = $link.href | |
$hdr = Invoke-WebRequest $href -Method Head -TimeoutSec 60 | |
$title = $hdr.BaseResponse.ResponseUri.Segments[-1] | |
$title = [uri]::UnescapeDataString($title) | Remove-InvalidFileNameChars | |
($href + " -- " + $title) | Write-Host | |
Invoke-WebRequest $href -Outfile ($destination + "\" + $title) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment