如何使用Powershell下载Windows补丁?

时间:2018-06-22 00:48:56

标签: powershell powershell-v3.0 windows-update

在Windows 2012和WSUS API上使用PowerShell,我设法遍历了经过严格批准的补丁程序列表,但是我不知道如何提取OrginUri路径以将每个补丁程序下载到唯一的下载文件夹中(不要不要问这是政策)。

我应该能够使用UpdateFile对每个更新使用OrginUri值: https://msdn.microsoft.com/en-us/library/microsoft.updateservices.administration.updatefile

我试图破译https://github.com/proxb/PoshWSUS/blob/master/Scripts/Get-PSWSUSUpdateFile.ps1上的代码,但我不明白:-)

1 个答案:

答案 0 :(得分:0)

取得了一些进展,秘密是OriginUri是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
            }
        }
     }