Guys,
This is unfinished, but here is a script to download MS patches to specified folders. It also creates a config file to use in a patching script. SOme of it does not work - so if you want to modify and 'fix' - feel free.
# Retrieve RSS feed from Microsoft
# Output list of hotfix titles and link to information page function GetInfoUrl { param ($text, $os)
switch ($os){
2k {$matchtext = $2kmatchtext}
2k3 {$matchtext = $2k3matchtext}
dotnet {$matchtext = $dotnetmatchtext}
default {throw "Incorrect OS passed to GetInfoUrl"}
}
if ($text -match $matchtext){ # get the first 36 characters of the string
$guid = $matches.values -as [string];
$guid = $guid.substring(0,36);
#create the download page url
$dlurl = $dlurlbase + $guid + "&displaylang=en";
}
else {
$guid = "Not found";
$dlurl = "none";
}
$matches = "String Not Found";
$dlurl
} # end of GetInfoUrl function
$rssUrl = "http://www.microsoft.com/technet/security/bulletin/secrss.aspx"
# $rssUrl = "http://www.microsoft.com/technet/security/current.aspx"
$feed = [xml](new-object System.Net.WebClient).DownloadString($rssUrl) $results = $feed.rss.channel.item
$dlurlbase = "http://www.microsoft.com/downloads/details.aspx?familyid="
$2kfolder = "c:\temp\Powershell\Downloads\W2K\" # folder to hold 2k hotfixes
$2k3folder = "c:\temp\Powershell\Downloads\W2K3\" # folder to hold 2k3 hotfixes
$configout = "c:\temp\Powershell\Downloads\config.txt" # file to hold db.txt lines
$2kmatchtext = "[A-Z0-9]{8,8}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{12,12}.\>Microsoft Windows 2000 Service Pack 4"
$2k3matchtext = "[A-Z0-9]{8,8}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{12,12}.\>Windows Server 2003 Service Pack 2"
$dotnetmatchtext = "[A-Z0-9]{8,8}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{4,4}\-[A-Z0-9]{12,12}.\>ASP.NET" # For each description page, pull back all the content of the download links
$results | foreach-object {$_.title; $desctext = (New-Object Net.Webclient).DownloadString($_.link); # zero some variables so we start fresh for each hotfix
$matches = "String Not Found"
$affsys = 0
$cat = "XXX" # Get the Info Page URL for 2k and 2k3 and assign them to variables using function in this script
$2kdlurl = GetInfoUrl -text $desctext -os 2k;
$2k3dlurl = GetInfoUrl -text $desctext -os 2k3;
$dotnetdurl = GetInfoUrl -text $desctext dotnet;
# at this point, we now have the URL to the download page(s) $2kdlurl and $2k3dlurl
# we need to do more regex stuff on the download page to find the actual link to the exe
if ($2kdlurl -ne "none")
{
"Ok, I'll will get the download link for 2k from " + $2kdlurl; $downloadtext = (New-Object Net.Webclient).DownloadString($2kdlurl);
if ($downloadtext -match "http://download.microsoft.com/dow.*.exe")
{
# get the actual download link by chopping the ends off the string
$exe2k = $matches.values -as [string];
$exe2k = $exe2k.substring(13,($exe2k.length - 19));
"Download Link is " + $exe2k;
$exename = $exe2k -as [string];
$exename = $exename.split("/");
$pieces = $exename.count;
$exename = $exename[ $pieces - 1 ];
"Exe name is " + $exename;
$kbnum = $exename.split("-");
$kbnum = $kbnum[1]
"KBnumber is " + $kbnum
# Lets download the file and drop it in the relevant folder
$clnt = new-object System.Net.WebClient;
$clnt.DownloadFile( $exe2k,$2kfolder + $exename );
# Add a variable showing that we downloaded a patch for 2k
$affsys += 1 # add 1 to affsys for 2k, will add 2 for 2k3 later
}
else {
Write-Host - fore RED "uh-oh, I couldn't find the download link on the page"
}
}
if ($dotnetdlurl -ne "none")
{
"Ok, I'll will get the download link for DotNET from " + $dotnetdlurl; $downloadtext = (New-Object Net.Webclient).DownloadString($dotnetdlurl);
if ($downloadtext -match "http://download.microsoft.com/dow.*.exe")
{
# get the actual download link by chopping the ends off the string
$exedotnet = $matches.values -as [string];
$exedotnet = $exedotnet.substring(0,($exedotnet.length - 0));
"Download Link is " + $exedotnet;
$exename = $exedotnet -as [string];
$exename = $exename.split("/");
$pieces = $exename.count;
$exename = $exename[ $pieces - 1 ];
"Exe name is " + $exename;
$kbnum = $exename.split("-");
$kbnum = $kbnum[1]
"KBnumber is " + $kbnum
# Lets download the file and drop it in the relevant folder
$clnt = new-object System.Net.WebClient;
$clnt.DownloadFile( $exedotnet,$2kfolder + $exename );
# Add a variable showing that we downloaded a patch for 2k
$affsys += 1 # add 1 to affsys for 2k, will add 2 for 2k3 later
}
else {
Write-Host - fore RED "uh-oh, I couldn't find the download link on the page"
}
}
if ($2k3dlurl -ne "none")
{
"Ok, I'll will get the download link for 2k3 from " + $2k3dlurl;
$downloadtext = (New-Object Net.Webclient).DownloadString($2k3dlurl);
if ($downloadtext -match "http://download.microsoft.com/dow.*.exe")
{
# get the actual download link by chopping the ends off the string
$exe2k3 = $matches.values -as [string];
$exe2k3 = $exe2k3.substring(0,($exe2k3.length - 0));
"Download Link is " + $exe2k3;
$exename = $exe2k3 -as [string];
$exename = $exename.split("/");
$pieces = $exename.count;
$exename = $exename[ $pieces - 1 ];
"Exe name is " + $exename;
$kbnum = $exename.split("-");
$kbnum = $kbnum[1]
"KBnumber is " + $kbnum
# Lets download the file and drop it in the relevant folder
$clnt = new-object System.Net.WebClient;
$clnt.DownloadFile( $exe2k3,$2k3folder + $exename );
# Add a variable showing that we downloaded a patch for 2k3
$affsys += 2 # add 2 to affsys for 2k3, we added 1 for 2k earlier
}
else {
Write-Host -fore RED "uh-oh, I couldn't find the download link on the page"
}
# One, both or no patches have been downloaded. Create line to put in db.txt
switch ($affsys){
0 {"Affsys = 0, no patches downloaded"}
1 {"Affsys = 1, just 2k patch downloaded"; $affos = "2000SP4"}
2 {"Affsys = 2, just 2k3 patch downloaded"; $affos ="2003SP2"}
3 {"Affsys = 3, 2k and 2k3 patch downloaded"; $affos = "2000SP4,2003SP2"}
default {Write-host -fore RED "uh-oh!, something went wrong with the download!"; $affos = "ERROR DOWNLOADING!"}
}
# prompt user for categorisation of patch
$cat = Read-Host "What is the categorisation of" $kbnum "?"
$line = $cat + $kbnum + "-" + $affos + "`t`t-" + $_.title
Write-host -fore GREEN $line
# Write to the file
add-content -value $line -path $configout
}
}