Comments (5)
Mo, My gift to you:
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 occured
.EXAMPLE
Get-PendingReboot
.EXAMPLE
$PRB=Get-PendingReboot
$PRB.RebootPending
.EXAMPLE
#CAVEAT: Not fully tested but should work
If (${Get-PendingReboot}.RebootPending) { echo "need2reboot" } 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
[string]$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
[string]$key = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\"
[string]$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
from psappdeploytoolkit.
Sweet, thanks, that's perfect. I will work on integrating this into the toolkit.
from psappdeploytoolkit.
I have a few of these "gifts" in-waiting.
I'd like to post them to the extension forum but WordPress is not kind to PowerShell code:
http://psappdeploytoolkit.com/forums/topic/check-if-prerequisite-is-present-before-install-application/
I'm currently working on Test-RegistryKey (b/c checking Reg values is not obvious or fun)
but I have Set-ScheduledTask (Creates a scheduled task on local computer or remote computer using COM object and XML text. ) ready to go.
Also have stuff for WakeTimers but I can't see anybody needing them.
from psappdeploytoolkit.
Use the "crayon" button to post properly formatted code on the WordPress forum. Also, a link to a GitHub page with the script would work as well.
from psappdeploytoolkit.
This function has been added to the latest beta version of the toolkit. Please test and let me know if it's working as expected.
from psappdeploytoolkit.
Related Issues (20)
- [Bug] Remove-File doesn't like single quotes. HOT 4
- Show-InstallationWelcome[Feature] HOT 1
- [Feature] Remove-EXEApplications HOT 1
- [Feature] Allow Get-InstalledApplication to also retrieve user context applications HOT 5
- [Bug] Show-InstallationPrompt via MCM/SCCM is not displayed for interactive installations HOT 42
- [Bug] Get-FileVersion -ProductVersion sometimes has trailing whitespace
- [Bug] RoboCopy Error 16 in CopyFile when started from UNC Path HOT 3
- Reference/documentation as pdf
- [Feature] Add and Remove Registry Key Values from Type MultiString without affecting existing ones
- [Bug] v3.10.1 Deploy-Application.exe triggers Defender ASR for vulnerable signed drivers HOT 1
- [Feature] Add AppDeployToolkitLogo.png in customization documentation HOT 1
- ClosePrompt_ButtonClose to small with german text HOT 5
- Is this a dead variable? HOT 3
- [Bug] Configure-EdgeExtension errors on line 16517 char:40 - Add-Member : Cannot bind argument to parameter 'InputObject' because it is null. HOT 1
- [tanslation] <BalloonText_FastRetry> German Translastion
- [Bug] HOT 1
- Incorrect information with 'Set-ItemPermission' reference in docs - 'https://psappdeploytoolkit.com/docs/reference/functions/Set-ItemPermission/' HOT 1
- [Documentation] Copy-File documentation needs more info HOT 1
- [Feature] Pauze after deffer period HOT 2
- [Bug] Activesetup not applying for current logged on user HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psappdeploytoolkit.