User Tools

Site Tools


userspace:windows_updates_-_powershell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
userspace:windows_updates_-_powershell [2017/03/02 09:39]
trabs-ol
userspace:windows_updates_-_powershell [2021/08/23 08:37] (current)
Line 1: Line 1:
-Mit diesem Script werden so lange Updates intstalliert bis keine mehr vorliegen. Die Updates werden nur nach 19 Uhr und vor 7 Uhr installiert um den User nicht an der Anmeldung zu hindern. Aussnahme: Der Rechner wurde neu installiert, dann werden unabhängig von der Uhrzeit alle Updates installiert. Mit dem ONCE Script werden immer alle Updates installiert, unabhänig von der Tageszeit.+====== WSUS Updates Powershell ======
  
-Voraussetzungen: Powershell Version >= 4+Mit diesem Script werden so lange Updates intstalliert bis keine mehr vorliegen. Die Updates werden nur nach 19 Uhr und vor 7 Uhr installiert um den User nicht an der Anmeldung zu hindern. Aussnahme: Der Rechner wurde neu installiert, dann werden unabhängig von der Uhrzeit alle Updates installiert. Mit dem ONCE Script werden immer alle Updates installiert, unabhängig von der Tageszeit. 
 + 
 +Voraussetzungen: Powershell Version >= 4.0 
 + 
 +Getestet unter Windows 7 x64 SP1 
 + 
 +Logging des Scriptes erfolgt in C:\tmp\windows-update-opsi.log 
 + 
 +Ordnerstruktur: 
 + 
 +CLIENT_DATA 
 +  * install-updates.ps1 
 +  * install-updates-once.ps1 
 +  * once64.ins 
 +  * setup64.ins 
 +  * windows_update_icon.png (nur fürs gute Aussehen) 
 + 
 +OPSI 
 +  * control
  
 setup64.ins setup64.ins
Line 21: Line 39:
 DefVar $MinimumSpace$ DefVar $MinimumSpace$
 DefVar $ExitCode$ DefVar $ExitCode$
- 
-DefStringList $Version_List$ 
-DefVar $Version$ 
  
 Set $LogDir$ = "%SystemDrive%\tmp" Set $LogDir$ = "%SystemDrive%\tmp"
Line 45: Line 60:
  ShowBitmap "%ScriptPath%\windows_update_icon.png" "Windows Update"  ShowBitmap "%ScriptPath%\windows_update_icon.png" "Windows Update"
  
- Message "Prüfe, ob Powershell Version >= 4 ist..." +        Files_Install /Sysnative
- set $Version_List$ = getOutStreamFromSection ("DosInAnIcon_PSVersion_check"+
- set $Version$ = takeString(2, $Version_List$) +
-  +
- if ($Version$ < "4"+
- Message "Powershell Version veraltet. Produkt Powershell auf setup setzen..." +
- opsiServiceCall_Powershell_Setup +
- Message "Neustart..." +
- ExitWindows /ImmediateReboot +
- else +
- Message "Powershell Version ist >= 4..." +
- endif +
-  +
-    Files_Install /Sysnative+
  Message "Installiere Windows Updates. Dies kann mehrere Stunden dauern."  Message "Installiere Windows Updates. Dies kann mehrere Stunden dauern."
  DosInAnIcon_WindowsUpate  DosInAnIcon_WindowsUpate
Line 64: Line 66:
  
 endif endif
- 
-[DosInAnIcon_PSVersion_check] 
-powershell.exe $PSVersionTable.PSVersion.Major 
  
 [Files_Install] [Files_Install]
 CheckTargetPath = "%Systemdrive%\tmp" CheckTargetPath = "%Systemdrive%\tmp"
-copy -u "%ScriptPath%\install-updates.ps1" "%Systemdrive%\temp\"+copy -u "%ScriptPath%\install-updates.ps1" "%Systemdrive%\tmp\"
  
 [DosInAnIcon_WindowsUpate] [DosInAnIcon_WindowsUpate]
-"%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy ByPass -command "& { "%Systemdrive%\temp\install-updates.ps1"; exit $lastexitcode }" +"%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy ByPass -command "& { "%Systemdrive%\tmp\install-updates.ps1"; exit $lastexitcode }"
- +
-[opsiServiceCall_Powershell_Setup] +
-"method": "setProductActionRequestWithDependencies" +
-"params":+
-           "powershell", +
-           "%HostID%", +
-           "setup" +
-          ]+
  
 [Sub_check_exitcode] [Sub_check_exitcode]
Line 131: Line 122:
 DefVar $MinimumSpace$ DefVar $MinimumSpace$
 DefVar $ExitCode$ DefVar $ExitCode$
- 
-DefStringList $Version_List$ 
-DefVar $Version$ 
  
 Set $LogDir$ = "%SystemDrive%\tmp" Set $LogDir$ = "%SystemDrive%\tmp"
Line 155: Line 143:
  ShowBitmap "%ScriptPath%\windows_update_icon.png" "Windows Update"  ShowBitmap "%ScriptPath%\windows_update_icon.png" "Windows Update"
  
- Message "Prüfe, ob Powershell Version >= 4 ist..." +        Files_Install /Sysnative
- set $Version_List$ = getOutStreamFromSection ("DosInAnIcon_PSVersion_check"+
- set $Version$ = takeString(2, $Version_List$) +
-  +
- if ($Version$ < "4"+
- Message "Powershell Version veraltet. Produkt Powershell auf setup setzen..." +
- opsiServiceCall_Powershell_Setup +
- Message "Neustart..." +
- ExitWindows /ImmediateReboot +
- else +
- Message "Powershell Version ist >= 4..." +
- endif +
-  +
-    Files_Install /Sysnative+
  Message "Installiere Windows Updates. Dies kann mehrere Stunden dauern."  Message "Installiere Windows Updates. Dies kann mehrere Stunden dauern."
  DosInAnIcon_WindowsUpate  DosInAnIcon_WindowsUpate
Line 174: Line 149:
  
 endif endif
- 
-[DosInAnIcon_PSVersion_check] 
-powershell.exe $PSVersionTable.PSVersion.Major 
  
 [Files_Install] [Files_Install]
 CheckTargetPath = "%Systemdrive%\tmp" CheckTargetPath = "%Systemdrive%\tmp"
-copy -u "%ScriptPath%\install-updates-once.ps1" "%Systemdrive%\temp\"+copy -u "%ScriptPath%\install-updates-once.ps1" "%Systemdrive%\tmp\"
  
 [DosInAnIcon_WindowsUpate] [DosInAnIcon_WindowsUpate]
-"%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -command "& { "%Systemdrive%\temp\install-updates-once.ps1"; exit $lastexitcode }" +"%SystemRoot%\Sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -command "& { "%Systemdrive%\tmp\install-updates-once.ps1"; exit $lastexitcode }"
- +
-[opsiServiceCall_Powershell_Setup] +
-"method": "setProductActionRequestWithDependencies" +
-"params":+
-           "powershell", +
-           "%HostID%", +
-           "setup" +
-          ]+
  
 [Sub_check_exitcode] [Sub_check_exitcode]
Line 249: Line 213:
 customScript:  customScript: 
 userLoginScript:  userLoginScript: 
 +</code>
  
-[ProductDependency+install-updates.ps1 
-actionsetup + 
-requiredProductpowershell +<code> 
-requiredStatus: installed +function Write-Log 
-requirementTypebefore+
 +    [CmdletBinding()
 +    param( 
 +        [Parameter(Mandatory=$True,ValueFromPipeline=$True)] 
 +        [Array[]]$logstring 
 +    ) 
 +    foreach ($string in $logstring) { 
 +        $nowDate = Get-Date -Format dd.MM.yyyy 
 +        $nowTime = Get-Date -Format HH:mm:ss 
 +        Write-Host $nowDate $nowTime $string 
 +        Add-Content -LiteralPath $LogPath -Value "$nowDate $nowTime $string" 
 +    } 
 +
 + 
 +function Get-Timewindow 
 +
 +    $Now = Get-Date 
 +    if (($Now.Hour -ge 7) -and ($Now.Hour -le 19)) 
 +    { 
 +        Write-Log 'It is daytime. Check if System was just getting installed.' 
 +        $InstallDate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate) 
 +        $OneDay = New-TimeSpan -Days 1 
 + 
 +        if ((($Now) -$InstallDate) -lt $OneDay) 
 +        { 
 +            Write-Log 'OS installation time is not older than 1 day. Windows Updates must be installed. Continue...' 
 +        } 
 +        else 
 +        { 
 +            Write-Log 'OS installation time is older than 1 day. Doing nothing because its day.' 
 +            Write-Log '***** END *****' 
 +            exit 0 
 +        } 
 +    } 
 +    else 
 +    { 
 +        Write-Log 'It is night. Continue...' 
 +    } 
 +
 + 
 +function Get-Rebootrequired 
 +
 +    $objSystemInfo= New-Object -ComObject 'Microsoft.Update.SystemInfo' 
 +    $RebootRequired = $objSystemInfo.RebootRequired 
 +    if ($RebootRequired -eq $true) 
 +    { 
 +        Write-Log 'Need to reboot, rebooting...' 
 +        exit 3010 
 +    } 
 +    else 
 +    { 
 +        Write-Log 'No need to reboot.' 
 +    }  
 +
 + 
 +function Get-InstallerStatus { 
 +    $Busy = $true 
 +    $lastWriteTimeCBSLos = (Get-Item C:\Windows\Logs\CBS\CBS.log).LastWriteTime 
 +    $TimespanOneMinute = New-TimeSpan -Minutes 1 
 +    while ($Busy -eq $true) 
 +    { 
 +        if (((Get-Date) -$lastWriteTimeCBSLos) -gt $TimespanOneMinute) { 
 +            $Busy = $false 
 +        } 
 +        else { 
 +            Write-Log 'Waiting for Trusted Installer...' 
 +            Start-Sleep -Seconds 10 
 +        } 
 +    } 
 +     
 +
 + 
 + 
 +$LogPath = "$env:SystemDrive\tmp\windows-update-opsi.log" 
 +$FirstRun = Test-Path -Path $LogPath 
 + 
 +Write-Log '***** START *****' 
 + 
 +Get-Timewindow 
 +Get-Rebootrequired 
 +Get-InstallerStatus 
 + 
 +Write-Log 'Searching for new Updates...' 
 + 
 +#GUI bauen 
 +[System.Windows.Forms.Application]::EnableVisualStyles() 
 +Add-Type -AssemblyName System.Windows.Forms 
 +$Form = New-Object system.Windows.Forms.Form 
 +$Form.Text = 'Windows-Update-Status' 
 +$Form.Width = 430 
 +$Form.Height = 100 
 +$Form.TopMost = $True 
 +$Form.AutoSizeMode = 'GrowAndShrink' 
 +$Form.MinimizeBox = $False 
 +$Form.MaximizeBox = $False 
 +$Form.WindowState = 'Normal' 
 +$Form.SizeGripStyle = 'Hide' 
 +$Form.ShowInTaskbar = $False 
 +$Form.StartPosition = 'CenterScreen' 
 +$Font = New-Object System.Drawing.Font('Arial',12) 
 +$Form.Font = $Font 
 +$Label = New-Object System.Windows.Forms.Label 
 +$Label.Text = 'Suche nach Windows Updates...' 
 +$Label.AutoSize = $True 
 +$Form.Controls.Add($Label) 
 +$Form.Show() 
 +$Form.Focus() 
 + 
 +$Session= New-Object -ComObject Microsoft.Update.Session 
 +$Searcher= $Session.CreateUpdateSearcher() 
 + 
 +$SearchResults = $Searcher.Search("IsInstalled=0 and Type='Software'").Updates 
 +if ($SearchResults.Count -eq 0 -and $FirstRun -eq $true) 
 +
 +    Write-Log 'No Updates found.' 
 +    Write-Log '***** END *****' 
 +    $Label.Text = 'Keine neuen Updates gefunden.' 
 +    $Form.Refresh() 
 +    Start-Sleep -Seconds 5 
 +    $Form.Close() 
 +    exit 0 
 +
 + 
 +if ($SearchResults.Count -eq 0 -and $FirstRun -eq $false) 
 +
 +    Write-Log 'No Updates found, but it is the first time to search for updates. Reboot!' 
 +    Write-Log '***** END *****' 
 +    $Label.Text = 'Keine Updates gefunden, neuer Versuch...' 
 +    $Form.Refresh() 
 +    Start-Sleep -Seconds 5 
 +    $form.Close() 
 +    exit 3010 
 +
 + 
 +foreach ($Update in $SearchResults) { 
 +    $TotalUpdateSize = $Update.MaxDownloadSize + $TotalUpdateSize 
 +    Write-Log "Found KB$($update.KBArticleIDs), Size: $([math]::Round($($update.MaxDownloadSize/1MB),2).ToString('0.00').PadLeft(6)) MB, Title: $($update.Title)" 
 +
 + 
 +Write-Log "Summary: $($SearchResults.Count) new Update(s), $($($TotalUpdateSize/1MB).ToString('0.00')) MB to download." 
 +$Label.Text = "$($SearchResults.Count) neue Update(s) gefunden." 
 +$Form.Refresh() 
 +Start-Sleep -Seconds 5 
 +Write-Log 'Starting Download...' 
 +$Label.Text = 'Starte Download.' 
 +$Form.Refresh() 
 +Start-Sleep -Seconds 5 
 + 
 +# ProgressBar bauen 
 +$ProgressBarSize = New-Object System.Drawing.Size 
 +$ProgressBarSize.Width = 400 
 +$ProgressBarSize.Height = 20 
 +$ProgressBar = New-Object System.Windows.Forms.ProgressBar 
 +$ProgressBar.Left = 5 
 +$ProgressBar.Top = 35 
 +$ProgressBar.Style = 'Continuous' 
 +$ProgressBar.Value = 0 
 +$ProgressBar.Size = $ProgressBarSize 
 +$Form.Controls.Add($ProgressBar) 
 + 
 +$ProgressInPercent = 0 
 +$DownloadCount = 0 
 +$DownloadSuccessCounter = 0 
 +$DownloadFailedCounter = 0 
 + 
 +foreach ($DownloadItem in $SearchResults) 
 +
 +    $DownloadCount++ 
 +    $Downloader = $Session.CreateUpdateDownloader() 
 +    $DownloadItems = New-Object -ComObject Microsoft.Update.UpdateColl 
 +    $DownloadItems.Add($DownloadItem) 
 +    $Downloader.Updates = $DownloadItems 
 +    $DownloadResult = $Downloader.Download() 
 +    if ($DownloadResult.ResultCode -eq 2) { 
 +        $DownloadSuccessCounter++ 
 +        Write-Log "Successfully downloaded Update $DownloadCount of $($SearchResults.Count), KB$($DownloadItem.KBArticleIDs), Size: $(($DownloadItem.MaxDownloadSize/1MB).ToString('0.00')) MB, Title: $($DownloadItem.Title)" 
 +        $ProgressInPercent = ($DownloadCount / $($SearchResults.Count))*100 
 +        $ProgressBar.Value = $ProgressInPercent 
 +        $Label.Text = "Download $DownloadCount von $($SearchResults.Count) erfolgreich." 
 +        $Form.Refresh() 
 +    } 
 +    else { 
 +        $DownloadFailedCounter++ 
 +        Write-Log "Failed downloading Update $DownloadCount of $($SearchResults.Count), KB$($DownloadItem.KBArticleIDs), Title: $($DownloadItem.Title)" 
 +        $ProgressInPercent = ($DownloadCount / $($SearchResults.Count))*100 
 +        $ProgressBar.Value = $ProgressInPercent 
 +        $Label.Text = "Download $DownloadCount von $($SearchResults.Count) fehlgeschlagen." 
 +        $Form.Refresh() 
 + 
 +    } 
 +
 +Write-Log "Summary: Successfully downloaded $DownloadSuccessCounter Updates, failed to download $DownloadFailedCounter Updates." 
 + 
 +Write-Log 'Starting Install...' 
 +$ProgressBar.Value = 0 
 +$Label.Text = 'Starte Installation...' 
 +$Form.Refresh() 
 + 
 +$InstallCount = 0 
 +$InstallSuccessCounter = 0 
 +$InstallFailedCounter = 0 
 +foreach ($InstallItem in $SearchResults) 
 +
 +    $InstallCount++ 
 +    $Installer = $Session.CreateUpdateInstaller() 
 +    $InstallerItems = New-Object -ComObject Microsoft.Update.UpdateColl 
 +    $InstallerItems.Add($InstallItem) 
 +    $Installer.Updates = $InstallerItems 
 +    $InstallResult = $Installer.Install() 
 +    if ($InstallResult.ResultCode -eq 2) { 
 +        $InstallSuccessCounter++ 
 +        Write-Log "Successfully installed Update $InstallCount of $($SearchResults.Count), KB$($InstallItem.KBArticleIDs), Title: $($InstallItem.Title)" 
 +        $ProgressInPercent = ($InstallCount / $($SearchResults.Count))*100 
 +        $ProgressBar.Value = $ProgressInPercent 
 +        $Label.Text = "Update $InstallCount von $($SearchResults.Count) erfolgreich installiert." 
 +        $Form.Refresh() 
 +    } 
 +    else { 
 +        $InstallFailedCounter++ 
 +        Write-Log "Failed installing Update $InstallCount of $($SearchResults.Count), KB$($InstallItem.KBArticleIDs), Title$($InstallItem.Title)" 
 +        $ProgressInPercent = ($InstallCount / $($SearchResults.Count))*100 
 +        $ProgressBar.Value = $ProgressInPercent 
 +        $Label.Text = "Update $InstallCount von $($SearchResults.Count) fehlgeschlagen." 
 +        $Form.Refresh() 
 +    } 
 +
 +Write-Log "Summary: Successfully installed $InstallSuccessCounter Updates, failed to install $InstallFailedCounter Updates." 
 +Write-Log 'Rebooting...' 
 +Write-Log '***** END *****' 
 +$Label.Text = "$InstallSuccessCounter Updates erfolgreich, $InstallFailedCounter Updates fehlgeschlagen." 
 +$Form.Refresh() 
 +Start-Sleep -Seconds 5 
 +$Label.Text = 'Neustart des Systems' 
 +$Form.Refresh() 
 +Start-Sleep -Seconds 5 
 +$Form.Close() 
 +exit 3010
 </code> </code>
 +
 +install-updates-once.ps1
 +
 +<code>
 +function Write-Log
 +{
 +    [CmdletBinding()]
 +    param(
 +        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
 +        [Array[]]$logstring
 +    )
 +    foreach ($string in $logstring) {
 +        $nowDate = Get-Date -Format dd.MM.yyyy
 +        $nowTime = Get-Date -Format HH:mm:ss
 +        Write-Host $nowDate $nowTime $string
 +        Add-Content -LiteralPath $LogPath -Value "$nowDate $nowTime $string"
 +    }
 +}
 +
 +function Get-Timewindow
 +{
 +    $Now = Get-Date
 +    if (($Now.Hour -ge 7) -and ($Now.Hour -le 19))
 +    {
 +        Write-Log 'It is daytime. Check if System was just getting installed.'
 +        $InstallDate = ([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)
 +        $OneDay = New-TimeSpan -Days 1
 +
 +        if ((($Now) -$InstallDate) -lt $OneDay)
 +        {
 +            Write-Log 'OS installation time is not older than 1 day. Windows Updates must be installed. Continue...'
 +        }
 +        else
 +        {
 +            Write-Log 'OS installation time is older than 1 day. Doing nothing because its day.'
 +            Write-Log '***** END *****'
 +            exit 0
 +        }
 +    }
 +    else
 +    {
 +        Write-Log 'It is night. Continue...'
 +    }
 +}
 +
 +function Get-Rebootrequired
 +{
 +    $objSystemInfo= New-Object -ComObject 'Microsoft.Update.SystemInfo'
 +    $RebootRequired = $objSystemInfo.RebootRequired
 +    if ($RebootRequired -eq $true)
 +    {
 +        Write-Log 'Need to reboot, rebooting...'
 +        exit 3010
 +    }
 +    else
 +    {
 +        Write-Log 'No need to reboot.'
 +    }
 +}
 +
 +function Get-InstallerStatus {
 +    $Busy = $true
 +    $lastWriteTimeCBSLos = (Get-Item C:\Windows\Logs\CBS\CBS.log).LastWriteTime
 +    $TimespanOneMinute = New-TimeSpan -Minutes 1
 +    while ($Busy -eq $true)
 +    {
 +        if (((Get-Date) -$lastWriteTimeCBSLos) -gt $TimespanOneMinute) {
 +            $Busy = $false
 +        }
 +        else {
 +            Write-Log 'Waiting for Trusted Installer...'
 +            Start-Sleep -Seconds 10
 +        }
 +    }
 +    
 +}
 +
 +
 +$LogPath = "$env:SystemDrive\tmp\windows-update-opsi.log"
 +$FirstRun = Test-Path -Path $LogPath
 +
 +Write-Log '***** START *****'
 +
 +# Auskommetniert für OPSI-Once-Script 
 +# Get-Timewindow
 +Get-Rebootrequired
 +Get-InstallerStatus
 +
 +Write-Log 'Searching for new Updates...'
 +
 +#GUI bauen
 +[System.Windows.Forms.Application]::EnableVisualStyles()
 +Add-Type -AssemblyName System.Windows.Forms
 +$Form = New-Object system.Windows.Forms.Form
 +$Form.Text = 'Windows-Update-Status'
 +$Form.Width = 430
 +$Form.Height = 100
 +$Form.TopMost = $True
 +$Form.AutoSizeMode = 'GrowAndShrink'
 +$Form.MinimizeBox = $False
 +$Form.MaximizeBox = $False
 +$Form.WindowState = 'Normal'
 +$Form.SizeGripStyle = 'Hide'
 +$Form.ShowInTaskbar = $False
 +$Form.StartPosition = 'CenterScreen'
 +$Font = New-Object System.Drawing.Font('Arial',12)
 +$Form.Font = $Font
 +$Label = New-Object System.Windows.Forms.Label
 +$Label.Text = 'Suche nach Windows Updates...'
 +$Label.AutoSize = $True
 +$Form.Controls.Add($Label)
 +$Form.Show()
 +$Form.Focus()
 +
 +$Session= New-Object -ComObject Microsoft.Update.Session
 +$Searcher= $Session.CreateUpdateSearcher()
 +
 +$SearchResults = $Searcher.Search("IsInstalled=0 and Type='Software'").Updates
 +if ($SearchResults.Count -eq 0 -and $FirstRun -eq $true)
 +{
 +    Write-Log 'No Updates found.'
 +    Write-Log '***** END *****'
 +    $Label.Text = 'Keine neuen Updates gefunden.'
 +    $Form.Refresh()
 +    Start-Sleep -Seconds 5
 +    $Form.Close()
 +    exit 0
 +}
 +
 +if ($SearchResults.Count -eq 0 -and $FirstRun -eq $false)
 +{
 +    Write-Log 'No Updates found, but it is the first time to search for updates. Reboot!'
 +    Write-Log '***** END *****'
 +    $Label.Text = 'Keine Updates gefunden, neuer Versuch...'
 +    $Form.Refresh()
 +    Start-Sleep -Seconds 5
 +    $form.Close()
 +    exit 3010
 +}
 +
 +foreach ($Update in $SearchResults) {
 +    $TotalUpdateSize = $Update.MaxDownloadSize + $TotalUpdateSize
 +    Write-Log "Found KB$($update.KBArticleIDs), Size: $([math]::Round($($update.MaxDownloadSize/1MB),2).ToString('0.00').PadLeft(6)) MB, Title: $($update.Title)"
 +}
 +
 +Write-Log "Summary: $($SearchResults.Count) new Update(s), $($($TotalUpdateSize/1MB).ToString('0.00')) MB to download."
 +$Label.Text = "$($SearchResults.Count) neue Update(s) gefunden."
 +$Form.Refresh()
 +Start-Sleep -Seconds 5
 +Write-Log 'Starting Download...'
 +$Label.Text = 'Starte Download.'
 +$Form.Refresh()
 +Start-Sleep -Seconds 5
 +
 +# ProgressBar bauen
 +$ProgressBarSize = New-Object System.Drawing.Size
 +$ProgressBarSize.Width = 400
 +$ProgressBarSize.Height = 20
 +$ProgressBar = New-Object System.Windows.Forms.ProgressBar
 +$ProgressBar.Left = 5
 +$ProgressBar.Top = 35
 +$ProgressBar.Style = 'Continuous'
 +$ProgressBar.Value = 0
 +$ProgressBar.Size = $ProgressBarSize
 +$Form.Controls.Add($ProgressBar)
 +
 +$ProgressInPercent = 0
 +$DownloadCount = 0
 +$DownloadSuccessCounter = 0
 +$DownloadFailedCounter = 0
 +
 +foreach ($DownloadItem in $SearchResults)
 +{
 +    $DownloadCount++
 +    $Downloader = $Session.CreateUpdateDownloader()
 +    $DownloadItems = New-Object -ComObject Microsoft.Update.UpdateColl
 +    $DownloadItems.Add($DownloadItem)
 +    $Downloader.Updates = $DownloadItems
 +    $DownloadResult = $Downloader.Download()
 +    if ($DownloadResult.ResultCode -eq 2) {
 +        $DownloadSuccessCounter++
 +        Write-Log "Successfully downloaded Update $DownloadCount of $($SearchResults.Count), KB$($DownloadItem.KBArticleIDs), Size: $(($DownloadItem.MaxDownloadSize/1MB).ToString('0.00')) MB, Title: $($DownloadItem.Title)"
 +        $ProgressInPercent = ($DownloadCount / $($SearchResults.Count))*100
 +        $ProgressBar.Value = $ProgressInPercent
 +        $Label.Text = "Download $DownloadCount von $($SearchResults.Count) erfolgreich."
 +        $Form.Refresh()
 +    }
 +    else {
 +        $DownloadFailedCounter++
 +        Write-Log "Failed downloading Update $DownloadCount of $($SearchResults.Count), KB$($DownloadItem.KBArticleIDs), Title: $($DownloadItem.Title)"
 +        $ProgressInPercent = ($DownloadCount / $($SearchResults.Count))*100
 +        $ProgressBar.Value = $ProgressInPercent
 +        $Label.Text = "Download $DownloadCount von $($SearchResults.Count) fehlgeschlagen."
 +        $Form.Refresh()
 +
 +    }
 +}
 +Write-Log "Summary: Successfully downloaded $DownloadSuccessCounter Updates, failed to download $DownloadFailedCounter Updates."
 +
 +Write-Log 'Starting Install...'
 +$ProgressBar.Value = 0
 +$Label.Text = 'Starte Installation...'
 +$Form.Refresh()
 +
 +$InstallCount = 0
 +$InstallSuccessCounter = 0
 +$InstallFailedCounter = 0
 +foreach ($InstallItem in $SearchResults)
 +{
 +    $InstallCount++
 +    $Installer = $Session.CreateUpdateInstaller()
 +    $InstallerItems = New-Object -ComObject Microsoft.Update.UpdateColl
 +    $InstallerItems.Add($InstallItem)
 +    $Installer.Updates = $InstallerItems
 +    $InstallResult = $Installer.Install()
 +    if ($InstallResult.ResultCode -eq 2) {
 +        $InstallSuccessCounter++
 +        Write-Log "Successfully installed Update $InstallCount of $($SearchResults.Count), KB$($InstallItem.KBArticleIDs), Title: $($InstallItem.Title)"
 +        $ProgressInPercent = ($InstallCount / $($SearchResults.Count))*100
 +        $ProgressBar.Value = $ProgressInPercent
 +        $Label.Text = "Update $InstallCount von $($SearchResults.Count) erfolgreich installiert."
 +        $Form.Refresh()
 +    }
 +    else {
 +        $InstallFailedCounter++
 +        Write-Log "Failed installing Update $InstallCount of $($SearchResults.Count), KB$($InstallItem.KBArticleIDs), Title: $($InstallItem.Title)"
 +        $ProgressInPercent = ($InstallCount / $($SearchResults.Count))*100
 +        $ProgressBar.Value = $ProgressInPercent
 +        $Label.Text = "Update $InstallCount von $($SearchResults.Count) fehlgeschlagen."
 +        $Form.Refresh()
 +    }
 +}
 +Write-Log "Summary: Successfully installed $InstallSuccessCounter Updates, failed to install $InstallFailedCounter Updates."
 +Write-Log 'Rebooting...'
 +Write-Log '***** END *****'
 +$Label.Text = "$InstallSuccessCounter Updates erfolgreich, $InstallFailedCounter Updates fehlgeschlagen."
 +$Form.Refresh()
 +Start-Sleep -Seconds 5
 +$Label.Text = 'Neustart des Systems'
 +$Form.Refresh()
 +Start-Sleep -Seconds 5
 +$Form.Close()
 +exit 3010
 +</code>
 +
userspace/windows_updates_-_powershell.1488447555.txt.gz · Last modified: 2021/08/23 08:37 (external edit)