Commit 8ddfdc8f authored by Stefan Scherer's avatar Stefan Scherer
Browse files

write log file C:\Windows\Temp\win-updates.log

parent 12e03aab
......@@ -3,6 +3,15 @@ param($global:RestartRequired=0,
$global:MaxCycles=5,
$MaxUpdatesPerCycle=500)
$Logfile = "C:\Windows\Temp\win-updates.log"
function LogWrite {
Param ([string]$logstring)
$now = Get-Date -format s
Add-Content $Logfile -value "$now $logstring"
Write-Host $logstring
}
function Check-ContinueRestartOrEnd() {
$RegistryKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
$RegistryEntry = "InstallWindowsUpdates"
......@@ -10,37 +19,37 @@ function Check-ContinueRestartOrEnd() {
0 {
$prop = (Get-ItemProperty $RegistryKey).$RegistryEntry
if ($prop) {
Write-Host "Restart Registry Entry Exists - Removing It"
LogWrite "Restart Registry Entry Exists - Removing It"
Remove-ItemProperty -Path $RegistryKey -Name $RegistryEntry -ErrorAction SilentlyContinue
}
Write-Host "No Restart Required"
LogWrite "No Restart Required"
Check-WindowsUpdates
if (($global:MoreUpdates -eq 1) -and ($script:Cycles -le $global:MaxCycles)) {
Install-WindowsUpdates
} elseif ($script:Cycles -gt $global:MaxCycles) {
Write-Host "Exceeded Cycle Count - Stopping"
LogWrite "Exceeded Cycle Count - Stopping"
Invoke-Expression "a:\openssh.ps1 -AutoStart"
} else {
Write-Host "Done Installing Windows Updates"
LogWrite "Done Installing Windows Updates"
Invoke-Expression "a:\openssh.ps1 -AutoStart"
}
}
1 {
$prop = (Get-ItemProperty $RegistryKey).$RegistryEntry
if (-not $prop) {
Write-Host "Restart Registry Entry Does Not Exist - Creating It"
LogWrite "Restart Registry Entry Does Not Exist - Creating It"
Set-ItemProperty -Path $RegistryKey -Name $RegistryEntry -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File $($script:ScriptPath) -MaxUpdatesPerCycle $($MaxUpdatesPerCycle)"
} else {
Write-Host "Restart Registry Entry Exists Already"
LogWrite "Restart Registry Entry Exists Already"
}
Write-Host "Restart Required - Restarting..."
LogWrite "Restart Required - Restarting..."
Restart-Computer
}
default {
Write-Host "Unsure If A Restart Is Required"
LogWrite "Unsure If A Restart Is Required"
break
}
}
......@@ -48,7 +57,7 @@ function Check-ContinueRestartOrEnd() {
function Install-WindowsUpdates() {
$script:Cycles++
Write-Host "Evaluating Available Updates with limit of $($MaxUpdatesPerCycle):"
LogWrite "Evaluating Available Updates with limit of $($MaxUpdatesPerCycle):"
$UpdatesToDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
$script:i = 0;
$CurrentUpdates = $SearchResult.Updates | Select-Object
......@@ -57,10 +66,10 @@ function Install-WindowsUpdates() {
if (($Update -ne $null) -and (!$Update.IsDownloaded)) {
[bool]$addThisUpdate = $false
if ($Update.InstallationBehavior.CanRequestUserInput) {
Write-Host "> Skipping: $($Update.Title) because it requires user input"
LogWrite "> Skipping: $($Update.Title) because it requires user input"
} else {
if (!($Update.EulaAccepted)) {
Write-Host "> Note: $($Update.Title) has a license agreement that must be accepted. Accepting the license."
LogWrite "> Note: $($Update.Title) has a license agreement that must be accepted. Accepting the license."
$Update.AcceptEula()
[bool]$addThisUpdate = $true
$script:CycleUpdateCount++
......@@ -71,7 +80,7 @@ function Install-WindowsUpdates() {
}
if ([bool]$addThisUpdate) {
Write-Host "Adding: $($Update.Title)"
LogWrite "Adding: $($Update.Title)"
$UpdatesToDownload.Add($Update) |Out-Null
}
}
......@@ -79,9 +88,9 @@ function Install-WindowsUpdates() {
}
if ($UpdatesToDownload.Count -eq 0) {
Write-Host "No Updates To Download..."
LogWrite "No Updates To Download..."
} else {
Write-Host 'Downloading Updates...'
LogWrite 'Downloading Updates...'
$ok = 0;
while (! $ok) {
try {
......@@ -90,8 +99,8 @@ function Install-WindowsUpdates() {
$Downloader.Download()
$ok = 1;
} catch {
Write-Host $_.Exception | Format-List -force
Write-Host "Error downloading updates. Retrying in 30s."
LogWrite $_.Exception | Format-List -force
LogWrite "Error downloading updates. Retrying in 30s."
$script:attempts = $script:attempts + 1
Start-Sleep -s 30
}
......@@ -100,10 +109,10 @@ function Install-WindowsUpdates() {
$UpdatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'
[bool]$rebootMayBeRequired = $false
Write-Host 'The following updates are downloaded and ready to be installed:'
LogWrite 'The following updates are downloaded and ready to be installed:'
foreach ($Update in $SearchResult.Updates) {
if (($Update.IsDownloaded)) {
Write-Host "> $($Update.Title)"
LogWrite "> $($Update.Title)"
$UpdatesToInstall.Add($Update) |Out-Null
if ($Update.InstallationBehavior.RebootBehavior -gt 0){
......@@ -113,7 +122,7 @@ function Install-WindowsUpdates() {
}
if ($UpdatesToInstall.Count -eq 0) {
Write-Host 'No updates available to install...'
LogWrite 'No updates available to install...'
$global:MoreUpdates=0
$global:RestartRequired=0
Invoke-Expression "a:\openssh.ps1 -AutoStart"
......@@ -121,19 +130,19 @@ function Install-WindowsUpdates() {
}
if ($rebootMayBeRequired) {
Write-Host 'These updates may require a reboot'
LogWrite 'These updates may require a reboot'
$global:RestartRequired=1
}
Write-Host 'Installing updates...'
LogWrite 'Installing updates...'
$Installer = $script:UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
Write-Host "Installation Result: $($InstallationResult.ResultCode)"
Write-Host "Reboot Required: $($InstallationResult.RebootRequired)"
Write-Host 'Listing of updates installed and individual installation results:'
LogWrite "Installation Result: $($InstallationResult.ResultCode)"
LogWrite "Reboot Required: $($InstallationResult.RebootRequired)"
LogWrite 'Listing of updates installed and individual installation results:'
if ($InstallationResult.RebootRequired) {
$global:RestartRequired=1
} else {
......@@ -151,7 +160,7 @@ function Install-WindowsUpdates() {
}
function Check-WindowsUpdates() {
Write-Host "Checking For Windows Updates"
LogWrite "Checking For Windows Updates"
$Username = $env:USERDOMAIN + "\" + $env:USERNAME
New-EventLog -Source $ScriptName -LogName 'Windows Powershell' -ErrorAction SilentlyContinue
......@@ -159,7 +168,7 @@ function Check-WindowsUpdates() {
$Message = "Script: " + $ScriptPath + "`nScript User: " + $Username + "`nStarted: " + (Get-Date).toString()
Write-EventLog -LogName 'Windows Powershell' -Source $ScriptName -EventID "104" -EntryType "Information" -Message $Message
Write-Host $Message
LogWrite $Message
$script:UpdateSearcher = $script:UpdateSession.CreateUpdateSearcher()
$script:successful = $FALSE
......@@ -170,8 +179,8 @@ function Check-WindowsUpdates() {
$script:SearchResult = $script:UpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
$script:successful = $TRUE
} catch {
Write-Host $_.Exception | Format-List -force
Write-Host "Search call to UpdateSearcher was unsuccessful. Retrying in 10s."
LogWrite $_.Exception | Format-List -force
LogWrite "Search call to UpdateSearcher was unsuccessful. Retrying in 10s."
$script:attempts = $script:attempts + 1
Start-Sleep -s 10
}
......@@ -179,20 +188,21 @@ function Check-WindowsUpdates() {
if ($SearchResult.Updates.Count -ne 0) {
$Message = "There are " + $SearchResult.Updates.Count + " more updates."
Write-Host $Message
LogWrite $Message
try {
$script:SearchResult.Updates |Select-Object -Property Title, Description, SupportUrl, UninstallationNotes, RebootRequired, EulaAccepted |Format-List
$global:MoreUpdates=1
} catch {
Write-Host $_.Exception | Format-List -force
Write-Host "Showing SearchResult was unsuccessful. Rebooting."
LogWrite $_.Exception | Format-List -force
LogWrite "Showing SearchResult was unsuccessful. Rebooting."
$global:RestartRequired=1
$global:MoreUpdates=0
Check-ContinueRestartOrEnd
LogWrite "Show never happen to see this text!"
Restart-Computer
}
} else {
Write-Host 'There are no applicable updates'
LogWrite 'There are no applicable updates'
$global:RestartRequired=0
$global:MoreUpdates=0
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment