SysJam Powershell RightClick Tool – Part 6 – Getting Running ConfigMgr Jobs with Powershell

One of the key functionality improvements I wanted to include in the Sysjam Powershell RightClick tool was realtime running job monitoring. There are few tools that don’t require you to click some type of refresh button to see the running jobs.

Part 1 of providing this functionality is the using powershell jobs combined with a timer object to polling any data on a refresh cycle and updating the form. I have covered this previously here

Part 2 of this is querying WMI for the status of each running jobs…and translating this to english.

This post will concentrate on Part 2

To start of, lets look at currently running programs. We can do this by looking at the “CCM_ExecutionRequestex” class which is available in the “root\ccm\softmgmtagent” namespace. Thankfully, most of this is in readable form within WMI, so we don’t need to translate it.

#Get Running Programs from WMI
    $wmiObject = Get-WmiObject -class CCM_ExecutionRequestex -NameSpace "root\ccm\softmgmtagent" -ComputerName $strComputer

    Foreach ($job in $wmiObject)
    {
	    $AdvID = $Job.AdvertID
	    $HashTableJob = $null
	    $HashTableJob = @{
            "AdvID" = $job.AdvertID
	        "PrgID" = $job.ProgramID
	        "State" = $job.State
	        "ContentID" = $job.ContentID
	        "RequestID" = $job.RequestID
        }
        $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)
    }

Next, lets look at currently enforcing applications. We can do this by looking at the “CCM_Application” class in the “root\ccm\ClientSDK” namespace. The evaluation state however is stored as an integer within WMI. We’ll need to translate this for the user….I use Switch() for this.

 #Get Running Applications from WMI
    $wmiObject = Get-WmiObject -class CCM_Application -NameSpace "root\ccm\ClientSDK" -ComputerName $strComputer | where { $_.EvaluationState -gt 5 }
    Foreach ($job in $wmiObject)
    {
	    $Evalstate = $job.EvaluationState
	    Switch ($Evalstate)
	    {
		    5 { $Evalstate = 'Application is currently waiting for content download to complete.' }
		    6 { $Evalstate = 'Application is currently waiting for content download to complete.' }
		    7 { $Evalstate = 'Application is currently waiting for its dependencies to download.' }
		    8 { $Evalstate = 'Application is currently waiting for a service (maintenance) window.' }
		    9 { $Evalstate = 'Application is currently waiting for a previously pending reboot.' }
		    10 { $Evalstate = 'Application is currently waiting for serialized enforcement.' }
		    11 { $Evalstate = 'Application is currently enforcing dependencies.' }
		    12 { $Evalstate = 'Application is currently enforcing.' }
		    13 { $Evalstate = 'Application install/uninstall enforced and soft reboot is pending.' }
		    14 { $Evalstate = 'Application installed/uninstalled and hard reboot is pending' }
		    15 { $Evalstate = 'Update is available but pending installation.' }
		    16 { $Evalstate = 'Application failed to evaluate.' }
		    17 { $Evalstate = 'Application is currently waiting for an active user session to enforce.' }
		    18 { $Evalstate = 'Application is currently waiting for all users to logoff.' }
		    19 { $Evalstate = 'Application is currently waiting for a user logon.' }
		    20 { $Evalstate = 'Application in progress, waiting for retry.' }
		    21 { $Evalstate = 'Application is waiting for presentation mode to be switched off.' }
		    22 { $Evalstate = 'Application is pre-downloading content' }
		    23 { $Evalstate = 'Application is pre-downloading dependent content' }
		    24 { $Evalstate = 'Application download failed' }
		    25 { $Evalstate = 'Application pre-downloading failed' }
		    26 { $Evalstate = 'Download success' }
		    27 { $Evalstate = 'Post-enforce evaluation.' }
		    28 { $Evalstate = 'Waiting for network connectivity.' }
		    Default { 'No Information Available' }
	    }
		
	
	    $HashTableJob = $null
	    $HashTableJob = @{ 
	        "AdvID" = $job.Name
	        "PrgID" = $job.FullName
	        "State" = $EvalState
	        "ContentID" = $null
	        "RequestID" = $job.ID
        } #Single job
        $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)				
    }

We also need to display any currently installing windows updates. This is very similar to the applications and we’ll need to make use of Switch() to translate the state for the user. This time however, we’ll make use of the “CCM_SoftwareUpdate” class in the “root\ccm\ClientSDK” namespace.

 #Get Running Software Updates from WMI
    $wmiObject = Get-WmiObject -class CCM_SoftwareUpdate -NameSpace "root\ccm\ClientSDK" -ComputerName $strComputer | where { $_.EvaluationState -gt 1 }
				
    Foreach ($job in $wmiObject)
    {
	    $Evalstate = $job.EvaluationState
	    Switch ($Evalstate)
	    {
		    1 { $Evalstate = 'Update is available' }
		    2 { $Evalstate = 'Update submitted' }
		    3 { $Evalstate = 'Update detecting' }
		    4 { $Evalstate = 'Update starting download' }
		    5 { $Evalstate = 'Update is currently waiting for content download to complete.' }
		    6 { $Evalstate = 'Update is currently waiting for install to start.' }
		    7 { $Evalstate = 'Update is currently waiting for install to complete.' }
		    8 { $Evalstate = 'Update is currently waiting for a soft reboot.' }
		    9 { $Evalstate = 'Update is currently waiting for a hard reboot.' }
		    10 { $Evalstate = 'Update is currently waiting for a reboot.' }
		    11 { $Evalstate = 'Update is currently verifying the installation' }
		    12 { $Evalstate = 'Update install complete.' }
		    13 { $Evalstate = 'Update install failed.' }
		    14 { $Evalstate = 'Update is waiting for an available maintenance window' }
		    15 { $Evalstate = 'Update is waiting for user logon.' }
		    16 { $Evalstate = 'Update is waiting for user log off' }
		    17 { $Evalstate = 'Update is waiting for user logon' }
		    18 { $Evalstate = 'Update is waiting for user to reconnect.' }
		    19 { $Evalstate = 'Update is waiting for user log off' }
		    20 { $Evalstate = 'Update pending' }
		    21 { $Evalstate = 'Update is waiting for retry' }
		    Default { 'Update in unknown state' }
	    }
						
	    $HashTableJob = @{ 
	        "AdvID" = $job.Name
	        "PrgID" = "Software Update"
	        "State" = $EvalState
	        "ContentID" = $null
	        "RequestID" = $job.UpdateID
        } 
	    $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)								
    }

We can now put this all together:

Function Get-RunningSCCMJobs{
     <#
        .SYNOPSIS
            Gets the current running jobs on a provided Configuration Manager 2012 client
    
        .DESCRIPTION
            Returns array of jobs (custom powershell object)
    
        .PARAMETER  strComputer
            The computer to query
    
        .EXAMPLE
           Get-RunningSCCMJobs -strComputer $strComputer
    #>
    Param(
        [Parameter(Mandatory=$true)]
        [string] $strComputer
    )
    #Define an array to hold all of the jobs.
    $arrJobs = @()

    #Get Running Programs from WMI
    $wmiObject = Get-WmiObject -class CCM_ExecutionRequestex -NameSpace "root\ccm\softmgmtagent" -ComputerName $strComputer

    Foreach ($job in $wmiObject)
    {
	    $AdvID = $Job.AdvertID
	    $HashTableJob = $null
	    $HashTableJob = @{
            "AdvID" = $job.AdvertID
	        "PrgID" = $job.ProgramID
	        "State" = $job.State
	        "ContentID" = $job.ContentID
	        "RequestID" = $job.RequestID
        }
        $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)
    }

				
    #Get Running Applications from WMI
    $wmiObject = Get-WmiObject -class CCM_Application -NameSpace "root\ccm\ClientSDK" -ComputerName $strComputer | where { $_.EvaluationState -gt 5 }
    Foreach ($job in $wmiObject)
    {
	    $Evalstate = $job.EvaluationState
	    Switch ($Evalstate)
	    {
		    5 { $Evalstate = 'Application is currently waiting for content download to complete.' }
		    6 { $Evalstate = 'Application is currently waiting for content download to complete.' }
		    7 { $Evalstate = 'Application is currently waiting for its dependencies to download.' }
		    8 { $Evalstate = 'Application is currently waiting for a service (maintenance) window.' }
		    9 { $Evalstate = 'Application is currently waiting for a previously pending reboot.' }
		    10 { $Evalstate = 'Application is currently waiting for serialized enforcement.' }
		    11 { $Evalstate = 'Application is currently enforcing dependencies.' }
		    12 { $Evalstate = 'Application is currently enforcing.' }
		    13 { $Evalstate = 'Application install/uninstall enforced and soft reboot is pending.' }
		    14 { $Evalstate = 'Application installed/uninstalled and hard reboot is pending' }
		    15 { $Evalstate = 'Update is available but pending installation.' }
		    16 { $Evalstate = 'Application failed to evaluate.' }
		    17 { $Evalstate = 'Application is currently waiting for an active user session to enforce.' }
		    18 { $Evalstate = 'Application is currently waiting for all users to logoff.' }
		    19 { $Evalstate = 'Application is currently waiting for a user logon.' }
		    20 { $Evalstate = 'Application in progress, waiting for retry.' }
		    21 { $Evalstate = 'Application is waiting for presentation mode to be switched off.' }
		    22 { $Evalstate = 'Application is pre-downloading content' }
		    23 { $Evalstate = 'Application is pre-downloading dependent content' }
		    24 { $Evalstate = 'Application download failed' }
		    25 { $Evalstate = 'Application pre-downloading failed' }
		    26 { $Evalstate = 'Download success' }
		    27 { $Evalstate = 'Post-enforce evaluation.' }
		    28 { $Evalstate = 'Waiting for network connectivity.' }
		    Default { 'No Information Available' }
	    }
		
	
	    $HashTableJob = $null
	    $HashTableJob = @{ 
	        "AdvID" = $job.Name
	        "PrgID" = $job.FullName
	        "State" = $EvalState
	        "ContentID" = $null
	        "RequestID" = $job.ID
        } #Single job
        $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)				
    }
				
				
    #Get Running Software Updates from WMI
    $wmiObject = Get-WmiObject -class CCM_SoftwareUpdate -NameSpace "root\ccm\ClientSDK" -ComputerName $strComputer | where { $_.EvaluationState -gt 1 }
				
    Foreach ($job in $wmiObject)
    {
	    $Evalstate = $job.EvaluationState
	    Switch ($Evalstate)
	    {
		    1 { $Evalstate = 'Update is available' }
		    2 { $Evalstate = 'Update submitted' }
		    3 { $Evalstate = 'Update detecting' }
		    4 { $Evalstate = 'Update starting download' }
		    5 { $Evalstate = 'Update is currently waiting for content download to complete.' }
		    6 { $Evalstate = 'Update is currently waiting for install to start.' }
		    7 { $Evalstate = 'Update is currently waiting for install to complete.' }
		    8 { $Evalstate = 'Update is currently waiting for a soft reboot.' }
		    9 { $Evalstate = 'Update is currently waiting for a hard reboot.' }
		    10 { $Evalstate = 'Update is currently waiting for a reboot.' }
		    11 { $Evalstate = 'Update is currently verifying the installation' }
		    12 { $Evalstate = 'Update install complete.' }
		    13 { $Evalstate = 'Update install failed.' }
		    14 { $Evalstate = 'Update is waiting for an available maintenance window' }
		    15 { $Evalstate = 'Update is waiting for user logon.' }
		    16 { $Evalstate = 'Update is waiting for user log off' }
		    17 { $Evalstate = 'Update is waiting for user logon' }
		    18 { $Evalstate = 'Update is waiting for user to reconnect.' }
		    19 { $Evalstate = 'Update is waiting for user log off' }
		    20 { $Evalstate = 'Update pending' }
		    21 { $Evalstate = 'Update is waiting for retry' }
		    Default { 'Update in unknown state' }
	    }
						
	    $HashTableJob = @{ 
	        "AdvID" = $job.Name
	        "PrgID" = "Software Update"
	        "State" = $EvalState
	        "ContentID" = $null
	        "RequestID" = $job.UpdateID
        } 
	    $arrJobs += (New-Object -TypeName PSCustomObject -Property $HashTableJob)								
    }
    Return $arrJobs
}

We can then call this function and display it to the user in the form. For the sake of this example, we can output it to a datagridview with the Out-GridView command

Get-RunningSCCMJobs -strComputer "SomeComputerName" | Out-gridview
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s