Coder Social home page Coder Social logo

Comments (5)

That-Annoying-Guy avatar That-Annoying-Guy commented on July 29, 2024

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.

mmashwani avatar mmashwani commented on July 29, 2024

Sweet, thanks, that's perfect. I will work on integrating this into the toolkit.

from psappdeploytoolkit.

That-Annoying-Guy avatar That-Annoying-Guy commented on July 29, 2024

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.

mmashwani avatar mmashwani commented on July 29, 2024

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.

mmashwani avatar mmashwani commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.