Created
January 5, 2015 21:09
-
-
Save nicholasdille/bf76ad4e9e31c1794a79 to your computer and use it in GitHub Desktop.
Download Script for all #PSDSC Resources on TechNet
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
param( | |
[string]$ResourceUrlCacheFile = (Join-Path -Path $PSScriptRoot -ChildPath 'PSDSC-ResourceDownloader.clixml') | |
, | |
[switch]$IgnoreCachedUrls = $false | |
, | |
[switch]$OverwriteExistingModules = $false | |
) | |
if (-Not (Test-Path -Path $ResourceUrlCacheFile) -Or $IgnoreCachedUrls) { | |
$ModuleList = New-Object System.Collections.ArrayList | |
$PageList = New-Object System.Collections.Stack | |
$PageList.Push('https://gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=Tag&f%5B0%5D.Value=Windows%20PowerShell%20Desired%20State%20Configuration&f%5B0%5D.Text=Windows%20PowerShell%20Desired%20State%20Configuration&pageIndex=1') | |
$PageBeenThere = New-Object System.Collections.ArrayList | |
while ($PageList.Count -gt 0) { | |
$url = $PageList.Pop() | |
if (-Not $PageBeenThere.Contains($url)) { | |
#'processing {0}' -f $url | |
$PageBeenThere.Add($url) | Out-Null | |
$page = Invoke-WebRequest $url | |
$page.Links | where {$_.href -match 'pageIndex' -and $_.innerText -match '\d+'} | foreach { | |
$url = $_.href | |
$url = $url.Replace('about:', 'https://gallery.technet.microsoft.com') | |
$url = $url.Replace('&', '&') | |
if (-Not $PageBeenThere.Contains($url)) { | |
$PageList.Push($url) | |
} | |
} | |
$page.Links | where {$_.href -match '^about:/scriptcenter/(.+)-[a-z0-9]{8}$'} | foreach { | |
$url = $_.href | |
$url = $url.Replace('about:', 'https://gallery.technet.microsoft.com') | |
$url = $url.Replace('&', '&') | |
$ModuleList.Push($url) | |
} | |
Start-Sleep -Seconds 5 | |
} | |
} | |
$ModuleList | Export-Clixml -Path $ResourceUrlCacheFile | |
} else { | |
$ModuleList = Import-Clixml -Path $ResourceUrlCacheFile | |
} | |
Foreach ($ModuleUrl in $ModuleList) { | |
$page = Invoke-WebRequest $ModuleUrl | |
$page.Links | where {$_.href -match '^about:/scriptcenter/(.+-[a-z0-9]{8})/file/'} | select -First 1 | foreach { | |
$ItemName = $Matches[1] | |
$url = $_.href | |
$url = $url.Replace('about:', 'https://gallery.technet.microsoft.com') | |
$url = $url.Replace('&', '&') | |
$url -match '/([^/]+.zip$)' | Out-Null | |
$FileName = $Matches[1] | |
$FileName = (Join-Path -Path $PSScriptRoot -ChildPath ('\DSC-Modules\' + $FileName)) | |
if (-Not (Test-Path -Path $FileName) -Or $OverwriteExistingModules) { | |
Invoke-WebRequest $url -OutFile $FileName | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist has been moved to my function library in repository psdsc(https://github.com/nicholasdille/psdsc)