powershell

sethayes

·

step04

·

Powershell

·

Total Size: 9.94 KB

·

·

Created: 3 months ago

·

Edited: 3 months ago

Param ( $global:RestartRequired = 0, $global:MoreUpdates = 0, $global:MaxCycles = 10 ) $Log = "C:\Users\Public\Log.txt" filter timestamp {"$(Get-Date -Format G) | $_"} Function Check-ContinueRestartOrEnd() { $RegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" $RegistryEntry = "InstallWindowsUpdates" switch ($global:RestartRequired) { 0 { $prop = (Get-ItemProperty $RegistryKey).$RegistryEntry If ($prop) { Write-Output "Restart Registry Entry Exists - Removing It" | timestamp Write-Output "Restart Registry Entry Exists - Removing It" | timestamp | Out-File $Log -Append Remove-ItemProperty -Path $RegistryKey -Name $RegistryEntry -ErrorAction SilentlyContinue If (Test-Path C:\Users\Public\step05.ps1) { & "C:\Users\Public\step05.ps1" } } Check-WindowsUpdates If (($global:MoreUpdates -eq 1) -and ($script:Cycles -le $global:MaxCycles)) { Stop-Service $script:ServiceName -Force Set-Service -Name $script:ServiceName -StartupType Disabled -Status Stopped Install-WindowsUpdates } ElseIf ($script:Cycles -gt $global:MaxCycles) { Write-Output "Exceeded Cycle Count - Stopping" | timestamp Write-Output "Exceeded Cycle Count - Stopping" | timestamp | Out-File $Log -Append } Else { Write-Output "Done Installing Windows Updates" | timestamp Write-Output "Done Installing Windows Updates" | timestamp | Out-File $Log -Append Set-Service -Name $script:ServiceName -StartupType Automatic -Status Running } } 1 { $prop = (Get-ItemProperty $RegistryKey).$RegistryEntry If (-not $prop) { Write-Output "Restart Registry Entry Does Not Exist - Creating It" | timestamp Write-Output "Restart Registry Entry Does Not Exist - Creating It" | timestamp | Out-File $Log -Append Set-ItemProperty -Path $RegistryKey -Name $RegistryEntry -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File $($script:ScriptPath)" } Else { Write-Output "Restart Registry Entry Already Exists" | timestamp Write-Output "Restart Registry Entry Already Exists" | timestamp | Out-File $Log -Append } Write-Output "Restart Required - Restarting..." | timestamp Write-Output "Restart Required - Restarting..." | timestamp | Out-File $Log -Append Restart-Computer -Force -Confirm:$false } default { Write-Output "Unsure If A Restart is Required" | timestamp Write-Output "Unsure If A Restart is Required" | timestamp | Out-File $Log -Append break } } } Function Install-WindowsUpdates() { $script:Cycles++ Write-Output "Evaluating Available Updates:" | timestamp Write-Output "Evaluating Available Updates:" | timestamp | Out-File $Log -Append $UpdatesToDownload = New-Object -ComObject "Microsoft.Update.UpdateColl" ForEach ($Update in $SearchResult.Updats) { If (($null -ne $Update) -and (!$Update.IsDownloaded)) { [bool]$addThisUpdate = $false If ($Update.InstallationBehavior.CanRequestUserInput) { Write-Output "> Skipping: $($Update.Title) because it requires user input" | timestamp Write-Output "> Skipping: $($Update.Title) because it requires user input" | timestamp | Out-File $Log -Append } Else { If (!($Update.EulaAccepted)) { Write-Output "> Note: $($Update.Title) has a License Agreement that Must be Accepted. Accepting the License." | timestamp Write-Output "> Note: $($Update.Title) has a License Agreement that Must be Accepted. Accepting the License." | timestamp | Out-File $Log -Append $Update.AcceptEula() [bool]$addThisUpdate = $true } Else { [bool]$addThisUpdate = $true } } If ([bool]$addThisUpdate) { Write-Output "Adding: $($Update.Title)" | timestamp Write-Output "Adding: $($Update.Title)" | timestamp | Out-File $Log -Append $UpdatesToDownload.Add($Update) | Out-Null } } } If ($UpdatesToDownload.Count -eq 0) { Write-Output "No Updates to Download..." | timestamp Write-Output "No Updates to Download..." | timestamp | Out-File $Log -Append } Else { Write-Output "Downloading Updates..." | timestamp Write-Output "Downloading Updates..." | timestamp | Out-File $Log -Append $Downloader = $UpdateSession.CreateUpdateDownloader() $Downloader.Updates = $UpdatesToDownload $Downloader.Download() } $UpdatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl' [bool]$rebootMayBeRequired = $false Write-Output "The Following Updates are Downloaded and Ready to Be Installed:" | timestamp Write-Output "The Following Updates are Downloaded and Ready to Be Installed:" | timestamp | Out-File $Log -Append ForEach ($Update in $SearchResult.Updates) { If (($Update.IsDownloaded)) { Write-Output "> $($Update.Title)" | timestamp Write-Output "> $($Update.Title)" | timestamp | Out-File $Log -Append $UpdatesToInstall.Add($Update) | Out-Null If ($Update.InstallationBehavior.RebootBehavior -gt 0) { [bool]$rebootMayBeRequired = $true } } } If ($UpdatesToInstall.Count -eq 0) { Write-Output "No Updates Available to Install..." | timestamp Write-Output "No Updates Available to Install..." | timestamp | Out-File $Log -Append $global:MoreUpdates = 0 $global:RestartRequired = 0 If (Test-Path C:\Users\Public\step05.ps1) { & C:\Users\Public\step05.ps1 } break } If ($rebootMayBeRequired) { Write-Output "These Updates May Require a Reboot" | timestamp Write-Output "These Updates May Require a Reboot" | timestamp | Out-File $Log -Append $global:RestartRequired = 1 } Write-Output "Installing Updates..." | timestamp Write-Output "Installing Updates..." | timestamp | Out-File $Log -Append $Installer = $script:UpdateSession.CreateUpdateInstaller() $Installer.Updates = $UpdatesToInstall $InstallationResult = $Installer.Install() Write-Output "Installation Result: $($InstallationResult.ResultCode)" | timestamp Write-Output "Reboot Required: $($InstallationResult.RebootRequired)" | timestamp Write-Output "Listing of Updates Installed and Individual Installation Results:" | timestamp Write-Output "Installation Result: $($InstallationResult.ResultCode)" | timestamp | Out-File $Log -Append Write-Output "Reboot Required: $($InstallationResult.RebootRequired)" | timestamp | Out-File $Log -Append Write-Output "Listing of Updates Installed and Individual Installation Results:" | timestamp | Out-File $Log -Append If ($InstallationResult.RebootRequired) { $global:RestartRequired = 1 } Else { $global:RestartRequired = 0 } For ($i = 0; $i -lt $UpdatesToInstall.Count; $i++) { New-Object -TypeName PSObject -Property @{ Title = $UpdatesToInstall.Item($i).Title Result = $InstallationResult.GetUpdateResult($i).ResultCode } } Check-ContinueRestartOrEnd } Function Check-WindowsUpdates() { Write-Output "Checking for Windows Updates" | timestamp Write-Output "Checking for Windows Updates" | timestamp | Out-File $Log -Append $Username = $env:USERDOMAIN + "\" + $env:USERNAME New-EventLog -Source $ScriptName -LogName 'WindowsPowershell' -ErrorAction SilentlyContinue $Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nStarted: " + (Get-Date).toString() Write-EventLog -LogName 'WindowsPowershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $message Write-Output $message | timestamp Write-Output $message | timestamp | Out-File $Log -Append $script:UpdateSearcher = $script:UpdateSession.CreateUpdateSearcher() $script:SearchResult = $script:UpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0") If ($SearchResult.Updates.Count -ne 0) { $script:SearchResult.Updates | Select-Object -Property Title, Description, SupportUrl, UninstallationNotes, RebootRequired, EulaAccepted | Format-List $global:MoreUpdates = 1 } Else { Write-Output "There are No Applicable Entries" | timestamp Write-Output "There are No Applicable Entries" | timestamp | Out-File $Log -Append $global:RestartRequired = 0 $global:MoreUpdates = 0 } } $script:ScriptName = $MyInvocation.MyCommand.ToString() $script:ScriptPath = $MyInvocation.MyCommand.Path $script:UpdateSession = New-Object -ComObject 'Microsoft.Update.Session' $script:UpdateSession.ClientApplicationID = 'Packer Windows Update Installer' $script:UpdateSearcher = $script:UpdateSession.CreateUpdateSearcher() $script:SearchResult = New-Object -ComObject 'Microsoft.Update.UpdateColl' $script:Cycles = 0 $script:ServiceName = "OpenSSHd" Stop-Service $script:ServiceName -Force Set-Service -Name $script:ServiceName -StartupType Disabled -Status Stopped Check-WindowsUpdates If ($global:MoreUpdates -eq 1) { Install-WindowsUpdates } Else { Check-ContinueRestartOrEnd }

0 bits

61 views

Are you sure you want to delete?