1: #Get list of all Management packs and their links from Technet Wiki
2: #Thanks to Stefan Stranger http://blogs.technet.com/b/stefan_stranger/archive/2013/03/13/finding-management-packs-from-microsoft-download-website-using-powershell.aspx
3: $allmpspage = Invoke-WebRequest -Uri "http://social.technet.microsoft.com/wiki/contents/articles/16174.microsoft-management-packs.aspx"
4: $mpslist = $allmpspage.Links | Where-Object {($_.href -like "*http://www.microsoft.com/*download*") -and ($_.outerText -notlike "*Link to download page*") -and ($_.InnerHTML -like "*This link*")} |
5: Select @{Label="Management Pack";Expression={$_.InnerText}}, @{Label="Download Link";Expression={$_.href}}
6:
7: #Directory to save the downloaded management packs. Make sure it is created first before running the script
8: $dirmp = "D:\MPs\"
9:
10: #go trough every MP
11: foreach ($mp in $mpslist)
12: {
13: #get MP link
14: $mplink = $mp.'Download Link'
15:
16: #get MP name
17: $mpname = $mp.'Management Pack'
18: Write-Host "MP Name:" $mpname
19: Write-Host "MP Link:" $mplink
20:
21: #Read MP page
22: $mppage = Invoke-WebRequest -Uri "$mplink"
23:
24: #Find all download links on the page (mp, guide and etc.). $_.href cannot be used beacuse some of the links require conformation before download
25: $dws = $mppage.Links | Where-Object {($_.'bi:fileurl' -like "*http://download.microsoft.com/download*") } | Select @{Label="Download Link";Expression={$_.'bi:fileurl'}}
26:
27: #Find the version number of the MP on its page
28: $version = $mppage.ParsedHtml.getElementsByTagName("td") | Where "classname" -contains "col2" | Select -ExpandProperty InnerText
29:
30: #Remove character ? in fron of MP version. For some reason some versions of mps start with ?
31: $version = $version.Replace("?","")
32:
33: #Remove / character from MP name if contains it beacuse can create unneeded directories
34: $mpname = $mpname.Replace("/","")
35: Write-Host "MP Version:" $version
36: Write-Host "Download Links:" $dws
37:
38: #Create directory with the Name of the MP and subdirecotory with the version of the MP
39: New-Item -ItemType directory -Path $dirmp\$mpname\$version
40:
41: #Get the array of found download links
42: $dws = $dws.'Download Link'
43:
44: #Get trough every download link
45: foreach ($dw in $dws)
46: {
47: #assign download link to $source variable
48: $source = $dw
49:
50: #Get the name of the file that will be downloaded
51: $Filename = [System.IO.Path]::GetFileName($source)
52:
53: #Set directory where the file to be downloaded
54: $dest = "$dirmp\$mpname\$version\$Filename"
55:
56: #initiate client for download
57: $wc = New-Object System.Net.WebClient
58:
59: #download the file and put it in the destination directory
60: $wc.DownloadFile($source, $dest)
61: }
62:
63: #empy line
64: Write-Host
65: }