Param($PackageDir, $LogDir) if ($PSBoundParameters.Count -ne 2) { Write-host "Error: -PackageDir -LogDir " ;exit 1} If ( -not (Test-Path $packagedir)) { Write-host "Error: -PackageDir does not exist." ;exit 1} If ( -not (Test-Path $LogDir)) { Write-host "Error: -LogDir does not exist." ;exit 1} function Write-Log { param ( [string]$Message, [string]$LogPath = "$env:TEMP\onlinepatch.log" ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $entry = "[$timestamp] $Message" Add-Content -Path $LogPath -Value $entry } $LogPath = "$LogDir\$(hostname)_onlinepatch.log" if (Test-Path $logPath) { $logTime = (get-item $logPath).LastWriteTime } else { $logTime = get-Date("1900/1/1") } Write-Log -Message "Start onlinepatch.ps1" -LogPath $LogPath $packages = (Get-ChildItem -Path $packagedir| where {$_.extension -eq ".msu"} | Sort Name | %{$_.FullName}) if ($packages.Count -gt 0){ if ((get-item $packages[0]).LastWriteTime -lt $logTime) { $outmsg = "It was already installed," Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath exit 0 } } # run MSRT, if exist # https://support.microsoft.com/kb/891716 if (Test-Path "$PackageDir\Windows-KB890830-*.exe") { Write-Log -Message "Run MSRT (result: $(hostname)_mrt.log)" -LogPath $LogPath Copy-Item "$PackageDir\Windows-KB890830-*.exe" "$env:Temp\mymsrt.exe" -Force Start-Process "$env:TEMP\mymsrt.exe" -Wait -ArgumentList "/q" #Remove-Item "$env:Temp\mymsrt.exe" Copy-Item "$env:WinDir\debug\mrt.log" "$LogDir\$(hostname)_mrt.log" -Force } # #$packages = (Get-ChildItem -Path $packagedir| where {$_.extension -eq ".msu"} | Sort Name | %{$_.FullName}) $success = $true if ($packages.Count -gt 0){ $outmsg = "Installation of updates is starting... " Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath foreach ($package in $packages){ Start-Process wusa.exe -Wait -ArgumentList "$package /quiet /norestart" if (!($LASTEXITCODE -eq 0)) { $success = $false $package = "+ " + (Get-Item $package).Name + ": Failed" } else { $package = "+ " + (Get-Item $package).Name + ": Success" } Write-Output $package Write-Log -Message $package -LogPath $LogPath } $outmsg = "Finished installing updates." Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath if ($success) { $outmsg = "Info: All updates were successfully installed." } else { $outmsg = "Info: There is a failed update." } Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath } else { $outmsg = "There are no updates (msu)." Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath #Pause exit 0 } if (Test-Path "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") { $outmsg = "Pending reboot." Write-Output $outmsg + "Press Enter to initiate reboot." Write-Log -Message $outmsg -LogPath $LogPath #Pause $outmsg = "Reboot now!" Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath Restart-Computer -Force } else { $outmsg = "No reboot is required." Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath #Pause } exit 0