<---name change
dism lets you inject it without any reboots...
dism lets you inject it without any reboots...
Function Get-PendingReboot {
<#
.SYNOPSIS
Gets the pending reboot status on a local computer. Return
.DESCRIPTION
Queries the registry and WMI to determine if the system waiting for a reboot, from:
CBServicing = Component Based Servicing (Windows 2008)
WindowsUpdate = Windows Update / Auto Update (Windows 2003 / 2008)
CCMClientSDK = SCCM 2012 Clients only (DetermineIfRebootPending method) otherwise $null value
PendFileRename = PendingFileRenameOperations (Windows 2003 / 2008)
Returns hash table similar to this:
Computer : MYCOMPUTERNAME
LastBootUpTime : 01/12/2014 11:53:04 AM
CBServicing : False
WindowsUpdate : False
CCMClientSDK : False
PendFileRename : False
PendFileRenVal :
RebootPending : False
ErrorMsg :
NOTES:
ErrorMsg only contains something if an error occurred
.EXAMPLE
Get-PendingReboot
.EXAMPLE
$PRB=Get-PendingReboot
$PRB.RebootPending
.EXAMPLE
#CAVEAT: Not fully tested but should work
If ( ${Get-PendingReboot}.RebootPending ) { echo "need to reboot" } Else { echo "no reboots pending" }
.NOTES
Based On: http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
#>
[CmdletBinding()]
Begin {
## Get the name of this function and write header
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header
[String]$ComputerName="$envComputerName"
$CBSRebootPend=$null
$WUAURebootReq=$null
$SCCM=$null
$PendFileRename=$null
$RegValuePFRO=$null
$Pending=$null
$LastBootUpTime=$null
$PendRebootErrorMsg=$null
}
Process {
Try {
# Setting pending values to false to cut down on the number of else statements
$PendFileRename,$Pending,$SCCM = $false,$false,$false
# Setting CBSRebootPend to null since not all versions of Windows has this value
$CBSRebootPend = $null
Try {
# Querying WMI for build version
$WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
$LastBootUpTime=$WMI_OS.ConvertToDateTime($WMI_OS.LastBootUpTime)
} catch {
[datetime]$LastBootUpTime=0 # returns January-01-01 12:00:00 AM
$PendRebootErrorMsg="WMI: $($_.Exception.Message)"
}
# If Vista/2008 & Above query the CBS Reg Key
If ($WMI_OS.BuildNumber -ge 6001) {
If (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
$CBSRebootPend=$true } else { $CBSRebootPend=$false }
}
# Query WUAU from the registry
If (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
$WUAURebootReq=$true } else { $WUAURebootReq=$false }
# Query PendingFileRenameOperations from the registry
$key="HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\"
$Value="PendingFileRenameOperations"
If (Test-Path "$key\$Value" -ErrorAction SilentlyContinue) {
$RegValuePFRO=Get-ItemProperty -Path $key | Select $Value -ExpandProperty $Value -ErrorAction SilentlyContinue
} else { $RegValuePFRO=$null }
# If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true
If ($RegValuePFRO) {
$PendFileRename = $true
} else {
# $RegValuePFRO="<empty>"
$PendFileRename = $false
}
# Determine SCCM 2012 Client Reboot Pending Status
# To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
$CCMClientSDK = $null
$CCMSplat = @{
NameSpace='ROOT\ccm\ClientSDK'
Class='CCM_ClientUtilities'
Name='DetermineIfRebootPending'
ComputerName=$ComputerName
ErrorAction='SilentlyContinue'
}
$CCMClientSDK = Invoke-WmiMethod @CCMSplat
If ($CCMClientSDK) {
If ($CCMClientSDK.ReturnValue -ne 0) {
Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
}
If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending) {
$SCCM = $true
}
}
} Catch {
Write-Warning "$ComputerName`: $_"
$PendRebootErrorMsg=$_
}
# If any of the variables are true, set $Pending variable to $true
# If the variables are a mixture of false and Null, $Pending variable becomes $false
If ($CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename) {
$Pending = $true
}
# Creating Custom PSObject and Select-Object Splat
$SelectSplat = @{
Property=('Computer','LastBootUpTime','CBServicing','WindowsUpdate','CCMClientSDK','PendFileRename','PendFileRenVal','RebootPending','ErrorMsg')
}
New-Object -TypeName PSObject -Property @{
Computer=$WMI_OS.CSName
CBServicing=$CBSRebootPend
WindowsUpdate=$WUAURebootReq
CCMClientSDK=$SCCM
PendFileRename=$PendFileRename
PendFileRenVal=$RegValuePFRO
RebootPending=$Pending
LastBootUpTime=$LastBootUpTime
ErrorMsg=$PendRebootErrorMsg
} | Select-Object @SelectSplat
}
End {
Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer
}
} #Get-PendingReboot Function
$UserNotificationStateSource = @'
using System;
using System.Runtime.InteropServices;
namespace QueryUser
{
public class NotificationState
{
// http://msdn.microsoft.com/en-us/library/bb762533(v=vs.85).aspx
public enum UserNotificationState
{
ScreenSaverOrLockedOrFastUserSwitching=1, FullScreenOrPresentationMode=2, RunningDirect3dFullScreen=3, PresentationMode=4, AcceptsNotifications=5, QuietTime=6, WindowsStoreAppRunning=7
}
// Only for Vista or above
[DllImport("shell32.dll")]
static extern int SHQueryUserNotificationState(out UserNotificationState pquns);
public static UserNotificationState GetState()
{
UserNotificationState state;
int returnVal = SHQueryUserNotificationState(out state);
return state;
}
}
}
'@
If (-not ([System.Management.Automation.PSTypeName]'QueryUser.NotificationState').Type) {
Add-Type -TypeDefinition $UserNotificationStateSource -Language CSharp
}
Start-Sleep -Seconds 10
[QueryUser.NotificationState]::GetState()