Windows Server

PowerShell İle WSUS’a Güncelleme Paketi Yükleme

Merhaba, Microsoft’un son yaptığı değişiklik sonrası WSUS üzerinde manuel update ekleme prosedürü değişti. Artık WSUS üzerine manuel bir güncelleme paketi eklemek için Microsoft’un yayınladığı ps script’i kullanmamız gerekiyor.

Özellikle Microsoft’un “out of band” dediğimiz normal güncelleme dağıtım peryotları dışında yayınladığı güncellemeleri bu method kullanarak WSUS veritabanına eklememiz gerekiyor. Bunun yanında SCCM gibi WSUS’a göbekten bağlı bir patch management tool’u kullanıyorsunuz yine aynı yöntemi kullanmanız gerekiyor.

Lütfen unutmayın, out of band güncellemeler WSUS gibi sistemlere otomatik eklenmediğinden manuel olarak eklenmelidir.”

Hadi başlayalım!

İlk olarak WSUS konsolunu açıyoruz ve “Import Updates“‘e tıklıyoruz.

Aşağıdaki web sitesi bizi karşılayacak. “Microsoft Update Catalog” linkine tıklıyoruz.

Açılan sayfada arama kutusuna bu ay Domain Controll’ların çökmesine neden olan ve Microsoft’un out of band olarak yayınladığı “KB5037425” güncellemesini yükleyeceğimiz için KB değerini aratıyoruz.

Güncelleme paketini bulduk ve “Download” butonuna tıkladık.

Gelen ekrandaki “UpdateID“‘i bir kenera not ediyoruz.

Şu ana kadar WSUS’a indireceğimiz güncelleme paketinin “UpdateID“‘i aldık. Şimdi sırada script’i oluşturmak var.

İlk olarak aşağıdaki script “ps1” uzantılı kaydediyoruz.

<#
.SYNOPSIS
Powershell script to import an update, or multiple updates into WSUS based on the UpdateID from the catalog.

.DESCRIPTION
This script takes user input and attempts to connect to the WSUS server.
Then it tries to import the update using the provided UpdateID from the catalog.

.INPUTS
The script takes WSUS server Name/IP, WSUS server port, SSL configuration option and UpdateID as input. UpdateID can be viewed and copied from the update details page for any update in the catalog, https://catalog.update.microsoft.com.

.OUTPUTS
Writes logging information to standard output.

.EXAMPLE
# Use with remote server IP, port and SSL
.\ImportUpdateToWSUS.ps1 -WsusServer 127.0.0.1 -PortNumber 8531 -UseSsl -UpdateId 12345678-90ab-cdef-1234-567890abcdef

.EXAMPLE
# Use with remote server Name, port and SSL
.\ImportUpdateToWSUS.ps1 -WsusServer WSUSServer1.us.contoso.com -PortNumber 8531 -UseSsl -UpdateId 12345678-90ab-cdef-1234-567890abcdef

.EXAMPLE
# Use with remote server IP, defaultport and no SSL
.\ImportUpdateToWSUS.ps1 -WsusServer 127.0.0.1  -UpdateId 12345678-90ab-cdef-1234-567890abcdef

.EXAMPLE
# Use with localhost default port
.\ImportUpdateToWSUS.ps1 -UpdateId 12345678-90ab-cdef-1234-567890abcdef

.EXAMPLE
# Use with localhost default port, file with updateID's
.\ImportUpdateToWSUS.ps1 -UpdateIdFilePath .\file.txt

.NOTES  
# On error, try enabling TLS: https://learn.microsoft.com/mem/configmgr/core/plan-design/security/enable-tls-1-2-client

# Sample registry add for the WSUS server from command line. Restarts the WSUSService and IIS after adding:
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 /V SchUseStrongCrypto /T REG_DWORD /D 1

## Sample registry add for the WSUS server from PowerShell. Restarts WSUSService and IIS after adding:
$registryPath = "HKLM:\Software\Microsoft\.NETFramework\v4.0.30319"
$Name = "SchUseStrongCrypto"
$value = "1" 
if (!(Test-Path $registryPath)) {
    New-Item -Path $registryPath -Force | Out-Null
}
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null
Restart-Service WsusService, w3svc

# Update import logs/errors are under %ProgramFiles%\Update Services\LogFiles\SoftwareDistribution.log

#>

param(
    [Parameter(Mandatory = $false, HelpMessage = "Specifies the name of a WSUS server, if not specified connects to localhost")]
    # Specifies the name of a WSUS server, if not specified connects to localhost.
    [string]$WsusServer,

[Parameter(Mandatory = $false, HelpMessage = "Specifies the port number to use to communicate with the upstream WSUS server, default is 8530")]
    # Specifies the port number to use to communicate with the upstream WSUS server, default is 8530.
    [ValidateSet("80", "443", "8530", "8531")]
    [int32]$PortNumber = 8530,

[Parameter(Mandatory = $false, HelpMessage = "Specifies that the WSUS server should use Secure Sockets Layer (SSL) via HTTPS to communicate with an upstream server")]
    # Specifies that the WSUS server should use Secure Sockets Layer (SSL) via HTTPS to communicate with an upstream server.  
    [Switch]$UseSsl,

[Parameter(Mandatory = $true, HelpMessage = "Specifies the update Id we should import to WSUS", ParameterSetName = "Single")]
    # Specifies the update Id we should import to WSUS
    [ValidateNotNullOrEmpty()]
    [String]$UpdateId,

[Parameter(Mandatory = $true, HelpMessage = "Specifies path to a text file containing a list of update ID's on each line", ParameterSetName = "Multiple")]
    # Specifies path to a text file containing a list of update ID's on each line.
    [ValidateNotNullOrEmpty()]
    [String]$UpdateIdFilePath
)

Set-StrictMode -Version Latest

# set server options
$serverOptions = "Get-WsusServer"
if ($psBoundParameters.containsKey('WsusServer')) { $serverOptions += " -Name $WsusServer -PortNumber $PortNumber" }
if ($UseSsl) { $serverOptions += " -UseSsl" }

# empty updateID list
$updateList = @()

# get update id's
if ($UpdateIdFilePath) {
    if (Test-Path $UpdateIdFilePath) {
        foreach ($id in (Get-Content $UpdateIdFilePath)) {
            $updateList += $id.Trim()
        }
    }
    else {
        Write-Error "[$UpdateIdFilePath]: File not found"
		return
    }
}
else {
    $updateList = @($UpdateId)
}

# get WSUS server
Try {
    Write-Host "Attempting WSUS Connection using $serverOptions... " -NoNewline
    $server = invoke-expression $serverOptions
    Write-Host "Connection Successful"
}
Catch {
    Write-Error $_
    return
}

# empty file list
$FileList = @()

# call ImportUpdateFromCatalogSite on WSUS
foreach ($uid in $updateList) {
    Try {
        Write-Host "Attempting WSUS update import for Update ID: $uid... " -NoNewline
        $server.ImportUpdateFromCatalogSite($uid, $FileList)
        Write-Host "Import Successful"
    }
    Catch {
        Write-Error "Failed. $_"
    }
}

Bu script’e yine buradan erişebilirsiniz.

İlk olarak “Set-ExecutionPolicy -ExecutionPolicy Bypass” komutu ile ps script’in çalışmasını engelleyen policy’i bypass ediyoruz.

İkinci olarak ps script’in bulunduğu konuma giderek aşağıdaki komutu çalıştırıyoruz.

.\ImportUpdateToWSUS.ps1 -UpdateId b40feb45-69fa-4ed7-aff2-608a93c9512f

Dikkat ederseniz “UpdateId” kısmına makalenin başlangıcında not ettiğimiz paketin ID numarasını girdik.

Yukardaki hatanın nedeni güçlü şifreleme algoritmalarının kullanılmaması. Aşağıdaki kod satırlarını sırayla çalıştırın ve WSUS sunucusunu reboot edin.

[Net.ServicePointManager]::SecurityProtocol
#Set strong cryptography on 64 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
#set strong cryptography on 32 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

[Net.ServicePointManager]::SecurityProtocol

Tekrar import komutu denendiğinde başarı bir şekilde işlem tamamlanmış olacak.

Sonuç olarak paketi kontrol ettiğimizde geldiğini görüyoruz. Bundan sonra paketin inidirilmesini sağlayıp deploy edebiliriz.

İşlemler bu kadar faydalı olması dileğiyle kolaylıklar.

İlgili Makaleler

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Başa dön tuşu