-
-
Save JimmyJames404/b444b6ce6da9c98094cf7b50d3638aad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
-------------------------------------- | |
MR. UPDATER | |
-------------------------------------- | |
1. Call to Check_OS | |
2. Sets variables for log file path & current timestamp. | |
3. Retrieves operating system version using `Get-CimInstance` cmdlet & stores in `$osVersion` variable. | |
4. Extracts numeric version from `$osVersion` variable & stores in `$osVersionNum`. | |
5. Script retrieves list of updates using `Get-WmiObject` cmdlet & assigns to `$updates` variable. | |
6. Checks if script is running with administrative privileges using ` [Secury.Principal. WindowsPrincipal] ` & `[Secury.Principal.WindowsIdenty]` classes. | |
7. If script is not running with administrative privileges, displays an error message using ` [System.Windows.Forms.MessageBox]::Show` & breaks execution. | |
8. If script is running with administrative privileges, checks if operating system version is Windows 10. | |
9. If operating system version is Windows 10, calls `Update_10` function. | |
9.01. Writes a start message to log file. | |
9.02. Script enters a loop to process each update in `$updates` list. | |
9.03. For each update, retrieves update ID & description. | |
9.04. Writes update installation information to console & log file. | |
9.05. Script checks if update is already installed by using `Get-HotFix` cmdlet with `-Id` parameter set to update’s `HotFixID`. | |
9.06. If update is already installed, writes a message to console & log file. | |
9.07. If update is not installed, attempts to install update using `wusa.exe` comm&-line tool with `/quiet`, `/norestart`, & `/kb:` parameters. | |
9.08. After attempting to install update, script checks result for success or failure. | |
9.09. If installation is successful, writes a success message to console & log file. | |
9.10. If installation fails, writes an error message to console & log file. | |
9.11. Script adds a blank line to console & log file for better readability. | |
9.12. Once all updates are processed, script writes a section header to log file for update details. | |
10. If operating system version is not Windows 10, displays an error message using ` [System.Windows.MessageBox]::Show` & breaks execution. | |
#> | |
function Check_OS() | |
{ | |
$logFile = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath 'updates.log' | |
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' | |
$osVersion = (Get-CimInstance -class Win32_operatingSystem).Caption | |
$osVersionNum = $osVersion -replace "[^0-9]" | |
# Get updates | |
$updates = Get-WmiObject -Class Win32_QuickFixEngineering | |
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) | |
{ | |
[System.Windows.Forms.Messagebox]::Show("This script needs to be run As Admin!!!", "Error") | |
Break | |
} | |
else | |
{ | |
if($osVersionNum -eq "10") | |
{ | |
Write-Host('Passed system Check ...') -Fore Green | |
Apply-HotfixUpdates | |
Install-WinGetPackages | |
UpdateDrivers | |
reboot | |
} | |
else | |
{ | |
[System.Windows.MessageBox]::show("This Script is only for Windows 10!!!!!!!!", "Error") | |
Break | |
} | |
} | |
} | |
function Apply-HotfixUpdates() | |
{ | |
Write-Host('Applying Hotfixupdates ...') -Fore Green | |
LogMessage "$timestamp - ............................................................... STARTING SCRIPT HOTFIXUPDATES..............................................................." | |
foreach ($update in $updates) | |
{ | |
$updateID = $update.HotFixID | |
$updateTitle = $update.Description | |
Write-Host "Installing update: $updateTitle ($updateID)" | |
LogMessage "$timestamp - Installing update: $updateTitle ($updateID)" | |
# Check if update is already installed | |
$isInstalled = Get-HotFix -Id $updateID -ErrorAction SilentlyContinue | |
if ($isInstalled) | |
{ | |
Write-Host "The update is already installed." | |
LogMessage "$timestamp - The update is already installed." | |
Write-Host('Updates already applied ...') -Fore Yellow | |
} | |
else | |
{ | |
$result = wusa.exe /quiet /norestart /install /kb:$updateID 2>&1 | |
if ($result -match 'successfully') | |
{ | |
Write-Host "The update was installed successfully." | |
LogMessage "$timestamp - An error occurred while installing the update:`r`n$result" | |
} | |
else | |
{ | |
Write-Host "An error occurred while installing the update:" | |
Write-Host $result | |
LogMessage "$timestamp - An error occurred while installing the update:`r`n$result" | |
} | |
} | |
} | |
} | |
function Install-WinGetPackages | |
{ | |
Write-Host('Applying WingetPackages ...') -Fore Green | |
LogMessage "$timestamp - ............................................................... STARTING SCRIPT WINGETPACKAGES..............................................................." | |
try { | |
$wingetFile = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath 'wingets_ids.json' | |
winget export -o $wingetFile | |
Write-Host('Searching Updates...') -Fore Green | |
$myJson = Get-Content -Raw $wingetFile | ConvertFrom-Json | |
$packages = $myJson.Sources[0].Packages | |
foreach ($package in $packages) { | |
$packageIdentifier = $package.PackageIdentifier | |
$updateOutput = winget upgrade --id $packageIdentifier --exact | |
if ($updateOutput -match "No applicable upgrade found.") { | |
LogMessage "No applicable upgrade found for Package: $packageIdentifier" | |
} | |
else | |
{ | |
try | |
{ | |
winget update $packageIdentifier --accept-source-agreements --force #--accept-package-agreements | |
LogMessage "Applied upgrade for Package: $packageIdentifier" | |
} | |
catch | |
{ | |
$errorMessage = "An error occurred: $_" | |
LogMessage $errorMessage | |
} | |
} | |
} | |
} | |
catch { | |
$errorMessage = "An error occurred: $_" | |
LogMessage $errorMessage | |
} | |
} | |
function UpdateDrivers() | |
{ | |
#search and list all missing Drivers | |
$Session = New-Object -ComObject Microsoft.Update.Session | |
$Searcher = $Session.CreateUpdateSearcher() | |
$Searcher.ServiceID = '7971f918-a847-4430-9279-4a52d1efe18d' | |
$Searcher.SearchScope = 1 # MachineOnly | |
$Searcher.ServerSelection = 3 # Third Party | |
$Criteria = "IsInstalled=0 and Type='Driver' and ISHidden=0" | |
Write-Host('Searching Driver-Updates...') -Fore Green | |
$SearchResult = $Searcher.Search($Criteria) | |
$Updates = $SearchResult.Updates | |
#Show available Drivers | |
$Updates | select Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer | fl | |
#Download the Drivers from Microsoft | |
$UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl | |
$updates | % { $UpdatesToDownload.Add($_) | out-null } | |
Write-Host('Downloading Drivers...') -Fore Green | |
$UpdateSession = New-Object -Com Microsoft.Update.Session | |
$Downloader = $UpdateSession.CreateUpdateDownloader() | |
$Downloader.Updates = $UpdatesToDownload | |
$Downloader.Download() | |
#Check if the Drivers are all downloaded and trigger the Installation | |
$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl | |
$updates | % { if($_.IsDownloaded) { $UpdatesToInstall.Add($_) | out-null } } | |
Write-Host('Installing Drivers...') -Fore Green | |
$Installer = $UpdateSession.CreateUpdateInstaller() | |
$Installer.Updates = $UpdatesToInstall | |
$InstallationResult = $Installer.Install() | |
if($InstallationResult.RebootRequired) | |
{ | |
Write-Host('Reboot required! please reboot now..') -Fore Red | |
} | |
else | |
{ | |
Write-Host('Done..') -Fore Green | |
} | |
} | |
function reboot() | |
{ | |
#Sometimes reboot is necessary.... | |
restart-computer -Confirm | |
} | |
# Function to log messages | |
function LogMessage($message) | |
{ | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$logEntry = "[{0}] {1}" -f $timestamp, $message | |
$logEntry | Out-File -FilePath $logFile -Append | |
} | |
$host.ui.RawUI.WindowTitle = "MR.UPDATERS (-(-_-(-_(-_(-_-)_-)-_-)_-)_-)-)" | |
Check_OS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment