-1

Using PowerShell on Windows 2012 and the WSUS API, I've managed to loop thru the list of reciently approved patches but I can't figure out how to extract the OrginUri path to download each patch into a unique download folder (don't ask it's policy).

I should be able OrginUri values for each update using UpdateFile: https://msdn.microsoft.com/en-us/library/microsoft.updateservices.administration.updatefile

I've tried to decipher the code at https://github.com/proxb/PoshWSUS/blob/master/Scripts/Get-PSWSUSUpdateFile.ps1 but I don't get it :-)

vorear
  • 19
  • 4

1 Answers1

0

Made some progress, the secret was OriginUri was part of GetInstallableItems

[void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
$ErrorActionPreference = 'Stop'
$WSUS = Get-WsusServer
Write-Host "WSUS Name:  " $WSUS.Name
$ApprovedUpdates = $WSUS.GetUpdate
$UpdateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$UpdateScope.FromCreationDate = (Get-Date).AddDays(-30)
#TODO: Search for patches approved since last patch tuesday
$UpdateScope.ApprovedStates = [Microsoft.UpdateServices.Administration.ApprovedStates]::LatestRevisionApproved
$updates = $WSUS.GetUpdates($UpdateScope)
Write-Host "Last Update: "(($WSUS.GetSubscription()).LastSynchronizationTime).DateTime
Write-Host "UpdateCount: "$updates.count
Write-Host
Write-Host "Patches:"

foreach ($item in $updates) {

    Write-Host $item.KnowledgebaseArticles $item.title
    $Update = $wsus.GetUpdate([Guid]($item.Id).UpdateId)
    $DownloadFolder = Join-Path  -Path "D:\UpdateFiles" -ChildPath ($item.Title).ToString()
    $PathResult = New-Item -ItemType Directory -Force -Path $DownloadFolder

    Foreach ($file in $item) {
    $PatchNames = $Update.GetInstallableItems() | select -ExpandProperty files | select -Property Name
    $PatchUri = $UpdateUri.OriginUri.AbsoluteUri
    $PatchCount = $PatchNames.Name.Count
    $PatchCount

    For ($Count=0; $Count -le $PatchCount; $Count++) {

        Write-Host $Count, $PatchNames.name[$Count]
        $DownloadName = Join-Path -Path $DownloadFolder -ChildPath $PatchNames.Name[$Count]
        $Job = Start-BitsTransfer -Source $PatchUri[$Count] -Destination $DownloadName -ErrorAction Stop 

        While( ($Job.JobState.ToString() -eq 'Transferring') -or 
($Job.JobState.ToString() -eq 'Connecting') -or !($Job.JobState.ToString() -eq 'Error') )
                {
                $pct = [int](($Job.BytesTransferred*100) / $Job.BytesTotal)
                Write-Progress -Activity "Copying file..." -CurrentOperation "$pct% complete"
                }
                Complete-BitsTransfer -BitsJob $Job
            }
        }
     }
vorear
  • 19
  • 4
  • Close, but unable to handle single row arrays of patches. Some Updates have multiple files per update. Error: Cannot index into a null array. Line 27 – vorear Jun 29 '18 at 20:06
  • Why does OriginUri say the patch is multiple cab files while the Microsoft Update catalog lists one msu file? – vorear Jun 29 '18 at 20:37
  • Considering https://github.com/exchange12rocks/WU/tree/master/Get-WUFilebyID and using the update GUID to request each package from the Update Catalog. – vorear Jun 29 '18 at 23:04