# # Pending Reboot.ps1 # # If debugging is not working open PowerShell.exe and paste the command "Set-ExecutionPolicy Bypass -Scope Process" # To remove execution policy: paste in PowerShell the command "Set-ExecutionPolicy Undefined -Scope Process" #Based on $InstallMateFileRenamePending = $false function Test-RebootRequired { $result = @{ CBSRebootPending =$false WindowsUpdateRebootRequired = $false FileRenamePending = $false UpdateExeVolatile = $false SCCMRebootPending = $false ComputerNameRenamedPending = $false } #Check CBS Registry $key = Get-ChildItem "HKLM:Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction Ignore if ($key -ne $null) { $result.CBSRebootPending = $true } #Check Windows Update $key = Get-Item "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction Ignore if($key -ne $null) { $result.WindowsUpdateRebootRequired = $true } #Check PendingFileRenameOperations $pendingFilesProp = Get-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction Ignore if($pendingFilesProp -ne $null) { #PendingFileRenameOperations is not *must* to reboot? #Display pending files $multiSzPrefix = '^\\\?\?\\' Write-Host "" Write-Host "Pending file operations: $($pendingFilesProp.PendingFileRenameOperations.Count / 2)" -foregroundcolor Yellow Write-Host "" $pendings = $pendingFilesProp.PendingFileRenameOperations | Select-String -Pattern "_TinDel.exe" if ($pendings.Count -eq 1) { $InstallMateFileRenamePending = $true } for( $i = 0; $i -lt $pendingFilesProp.PendingFileRenameOperations.Count; $i++ ) { # Get the source file $src = $pendingFilesProp.PendingFileRenameOperations[$i] -replace $multiSzPrefix if( $src ) { #$found = $src.Contains("_TinDel.exe") -or $src -match "Tsu.*dll" #if ($found -eq $false) #{ # $result.FileRenamePending = $true #} $result.FileRenamePending = $true Write-Host -NoNewline "Found source: ${src}" -foregroundcolor Yellow try { # Get the target path $dst = $pendingFilesProp.PendingFileRenameOperations[++$i] -replace $multiSzPrefix if( $dst ) { Write-Verbose -NoNewline "Found target: ${dst}" -foregroundcolor Yellow } # Determine whether it's a "move" or "delete" $operation = if( $dst ) { "move" } else { "delete" } } catch { } Write-Host " - reboot to ${operation}" -foregroundcolor Yellow } } Write-Host "" } #Check Exec file modification $key = Get-Item "HKLM:SOFTWARE\Microsoft\Updates\UpdateExeVolatile" -ErrorAction Ignore if($key -ne $null) { $result.UpdateExeVolatile = $true } #Check Computer name renaming $ActiveComputerNameProp = Get-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" -Name ComputerName -ErrorAction Ignore $ComputerNameProp = Get-ItemProperty "HKLM:SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" -Name ComputerName -ErrorAction Ignore if($ActiveComputerNameProp -ne $null -and $ComputerNameProp -ne $null) { if ($ActiveComputerNameProp.ComputerName -ne $ComputerNameProp.ComputerName) { $result.ComputerNameRenamedPending = $true } } #Check SCCM Client try { $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities" $status = $util.DetermineIfRebootPending() if(($status -ne $null) -and $status.RebootPending) { $result.SCCMRebootPending = $true } }catch{} if ($result.CBSRebootPending) { Write-Host "Reboot pending: $($result.CBSRebootPending)" -foregroundcolor Yellow } else { Write-Host "Reboot pending: $($result.CBSRebootPending)" -foregroundcolor Green } if ($result.WindowsUpdateRebootRequired) { Write-Host "Windows update reboot required: $($result.WindowsUpdateRebootRequired)" -foregroundcolor Yellow } else { Write-Host "Windows update reboot required: $($result.WindowsUpdateRebootRequired)" -foregroundcolor Green } if ($result.FileRenamePending) { Write-Host "Pending file rename operations: $($result.FileRenamePending)" -foregroundcolor Yellow } else { Write-Host "Pending file rename operations: $($result.FileRenamePending)" -foregroundcolor Green } if ($result.UpdateExeVolatile) { Write-Host "Windows exec file modification: $($result.UpdateExeVolatile)" -foregroundcolor Yellow } else { Write-Host "Windows exec file modification: $($result.UpdateExeVolatile)" -foregroundcolor Green } if ($result.ComputerNameRenamedPending) { Write-Host "Computer name renamed pending: $($result.ComputerNameRenamedPending)" -foregroundcolor Yellow } else { Write-Host "Computer name renamed pending: $($result.ComputerNameRenamedPending)" -foregroundcolor Green } if ($result.SCCMRebootPending) { Write-Host "SCCM Client (Microsoft's System Center Configuration Manager): $($result.SCCMRebootPending)" -foregroundcolor Yellow } else { Write-Host "SCCM Client (Microsoft's System Center Configuration Manager): $($result.SCCMRebootPending)" -foregroundcolor Green } #Return Reboot required return $result.ContainsValue($true) } try { Write-Host "" if(Test-RebootRequired) { Write-Host "" Write-Host "A reboot is required" -foregroundcolor Yellow } else { Write-Host "" Write-Host "A reboot is not required" -foregroundcolor Green } Write-Host "" Read-Host -Prompt "Press any key to exit" } catch { Write-Error $_.Exception.ToString() Read-Host -Prompt "Error. Press Enter to exit" }