narrow default width wide
colour style colour style colour style colour style

Patch Download script for Offline MDT Builds

This is a powershell script to help download patches if you need to build a patch image using MDT in an Isolated environment. 


To use it, deploy your current image to a system with Internet access. Then, run this script and it should download all needed patches to the C: of that computer. Then, import them into MDT and rebuild your image.


 
#set-executionpolicy remotesigned
 
$RootTargetFolder = "c:\patchDL1"
if ((Test-Path -path $RootTargetFolder\) -ne $True)
  {
  New-Item $RootTargetFolder\ -type directory
  }
 
$UpdateSession = New-Object -com Microsoft.Update.Session
$UpdateServiceManager = New-Object -Com Microsoft.Update.ServiceManager
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
#$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0") 
$UpdateSearcher.ServerSelection = 2
$SearchResult = $UpdateSearcher.Search("Type='Software'")
$searchResult.Updates.Count
$Updates = $searchResult.Updates
 
#Write-Host $UpdateSearcher.ClientApplicationID
#$UpdateSearcher | gm
 
#$proxy = New-Object System.Net.WebProxy("http://proxy2.ssmhc.com:8080")
#$proxy.UserDefaultCredentials = $True
$Webclient = New-Object Net.WebClient
#$Webclient.Proxy = $proxy
 
#$url = 'http://download.microsoft.com/download/4/7/1/47104ec6-410d-4492-890b-2a34900c9df2/Workshops-EN.zip'
#$local = "$home\powershellworkshop.zip"
#$object.DownloadFile($url, $local)
 
$UpdatesToProcess = New-Object -Com Microsoft.Update.UpdateColl
foreach($update in $Updates)
  {
  #If (($Filename.ToUpper().Contains("972813") -eq $False)  #Language Packs
  switch -wildcard ($update.Title)
    {
    "Windows Search 4.0*" {"SKIP - " + $update.Title; break}
    "Internet Explorer 8 for Windows XP" {"SKIP - " + $update.Title; break}
    "Windows Malicious Software Removal Tool*" {"SKIP - " + $update.Title; break}
    "Windows Media Player 11" {"SKIP - " + $update.Title; break}
    "*microsoftfixit*" {"SKIP - " + $update.Title; break}  #Weird .Net patch files      #need to be in other exclusion area??
    "*msipatchregfix*" {"SKIP - " + $update.Title; break}  #Weird .Net patch files      #need to be in other exclusion area??
    "*KB905474*" {"SKIP - " + $update.Title; break}  #Windows Genuine
    "*KB923789*" {"SKIP - " + $update.Title; break}  #Flash Security Update
    "*KB931125*" {"SKIP - " + $update.Title; break}  #Root Cert Update
    "*KB2483139*" {"SKIP - " + $update.Title; break}  #Language Packs
    default   {$UpdatesToProcess.Add($update) | Out-Null}  #Only add it if we haven't excluded it
    }
    #"*KB982524*" {"SKIP - " + $update.Title; break}  #UNKNOWN
    #"*KB982168*" {"SKIP - " + $update.Title; break}  #UNKNOWN
    #"*KB983583*" {"SKIP - " + $update.Title; break}  #UNKNOWN
    #"*KB2416473*" {"SKIP - " + $update.Title; break}  #UNKNOWN
    #"*KB2418241*" {"SKIP - " + $update.Title; break}  #UNKNOWN
    #"Microsoft .NET Framework version 1.1" {"SKIP - " + $update.Title; break}
    #"*KB936929*" {"SKIP - " + $update.Title; break}  #XP SP3
 
 
 
  #$UpdatesToProcess.Add($update) | Out-Null
 
  #foreach($Category in $update.Categories)
  #  {
  #  "`t" + $Category.Title + "`t" + $Category.Type
  #  }
 
  }
 
#Exit
$UpdatesToProcess.Count
 
foreach($update in $UpdatesToProcess)
  {
  #$update.Title
  #$update.BundledUpdates.DownloadContents.DownloadURL
 
  foreach($BundledUpdate in $update.BundledUpdates)
    {
    #"`t" + $BundledUpdate.Title
    #$BundledUpdate.DownloadURL
    Write-Host $BundledUpdate.IsInstalled  $update.Title
    if($BundledUpdate.IsInstalled -eq $False)
      {
      foreach($DCC in $BundledUpdate.DownloadContents)
        {
 
        #"`t`t" + $DCC.DownloadURL
        $Filename = $DCC.DownloadURL.Substring($DCC.DownloadURL.LastIndexOf("/") + 1)
        #"`t`t`t" + $filename
 
        #Skip Language Packs and "delta" versions of patches
        #If (($Filename.ToUpper().Contains("972813") -eq $False) -and ($Filename.ToUpper().Contains("EXPRESS") -eq $False) -and (($Filename.ToUpper().Substring($Filename.Length - 3, 3) -eq "PSF") -eq $False))
        If (($Filename.ToUpper().Contains("EXPRESS") -eq $False) -and (($Filename.ToUpper().Substring($Filename.Length - 3, 3) -eq "PSF") -eq $False))
        #If (1 -eq 1)
 
          {
          #Write-Host $update.Title.Length  $Filename.Length
          if(($RootTargetFolder.Length + $update.Title.Length + $Filename.Length + 1) -lt 250)
            {
            $PatchTargetFolder = $RootTargetFolder + "\" + ($update.Title -Replace(":", ""))
            #$a = $a -replace("x","a")
            }
          else
            {
            foreach($KB in $update.KBArticleIDs)
              {
              #"`t" + $KB
              #Will just use one of the KBs referenced by the patches
              $PatchTargetFolder = $RootTargetFolder + "\KB" + $KB
              }
            }
 
          #$PatchTargetFolder = $RootTargetFolder + "\" + $update.Title
 
          #Test-Path -path $PatchTargetFolder\
          #Create patch-specific folder
           if ((Test-Path -path $PatchTargetFolder\) -ne $True)
             {
  #           New-Item $PatchTargetFolder\ -type directory
             }
 
          $DestFilepath = $PatchTargetFolder + "\" + $filename
 
          "`t`t`t" + $DestFilepath
          #Download patch
          if((Test-Path -Path $DestFilepath) -ne $True)
            {
  #          $Webclient.DownloadFile($DCC.DownloadURL, $DestFilepath)
            }
          }
        }
      }
    }
  }