Forked from SMSAgentSoftware/Get-CMClientExecutionHistory.ps1
Created
January 27, 2020 17:33
-
-
Save KurtDeGreeff/58879f5131f77a76bed3f7fa771b5f7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)] | |
[string[]]$ComputerName = $env:COMPUTERNAME | |
) | |
Begin | |
{ | |
$Code = { | |
# Get Execution History from registry, and package details from WMI | |
$ExecutionHistoryKey = "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History" | |
$ContextKeys = Get-ChildItem $ExecutionHistoryKey | Select -ExpandProperty PSChildName | |
foreach ($ContextKey in $ContextKeys) | |
{ | |
If ($ContextKey -eq "System") | |
{ | |
$ContextKey = "Machine" | |
} | |
Else | |
{ | |
$ContextKey = $ContextKey.Replace('-','_') | |
} | |
[array]$SoftwareDistribution += Get-CimInstance -Namespace ROOT\ccm\Policy\$ContextKey -ClassName CCM_SoftwareDistribution | |
} | |
# Create a datatable to hold the results | |
$DataTable = New-Object System.Data.DataTable | |
[void]$DataTable.Columns.Add("ComputerName") | |
[void]$DataTable.Columns.Add("PackageName") | |
[void]$DataTable.Columns.Add("PackageID") | |
[void]$DataTable.Columns.Add("ProgramName") | |
[void]$DataTable.Columns.Add("DeploymentStatus") | |
[void]$DataTable.Columns.Add("Context") | |
[void]$DataTable.Columns.Add("State") | |
[void]$DataTable.Columns.Add("RunStartTime") | |
[void]$DataTable.Columns.Add("SuccessOrFailureCode") | |
[void]$DataTable.Columns.Add("SuccessOrFailureReason") | |
foreach ($ContextKey in $ContextKeys) | |
{ | |
If ($ContextKey -ne "System") | |
{ | |
# Get user context if applicable | |
$SID = New-Object Security.Principal.SecurityIdentifier -ArgumentList $ContextKey | |
$Context = $SID.Translate([System.Security.Principal.NTAccount]) | |
} | |
Else | |
{ | |
$Context = "Machine" | |
} | |
$SubKeys = Get-ChildItem "$ExecutionHistoryKey\$ContextKey" | |
Foreach ($SubKey in $SubKeys) | |
{ | |
$Items = Get-ChildItem $SubKey.PSPath | |
Foreach ($Item in $Items) | |
{ | |
$PackageInfo = $SoftwareDistribution | Where {$_.PKG_PackageID -eq $SubKey.PSChildName -and $_.PRG_ProgramName -eq $Item.GetValue("_ProgramID")} | Select -First 1 | |
If ($PackageInfo) | |
{ | |
$PackageName = $PackageInfo.PKG_Name | |
$DeploymentStatus = "Active" | |
} | |
Else | |
{ | |
$PackageName = "-Unknown-" | |
$DeploymentStatus = "No longer targeted" | |
} | |
[void]$DataTable.Rows.Add($using:Computer,$PackageName,$SubKey.PSChildName,$Item.GetValue("_ProgramID"),$DeploymentStatus,$Context,$Item.GetValue("_State"),$Item.GetValue("_RunStartTime"),$Item.GetValue("SuccessOrFailureCode"),$Item.GetValue("SuccessOrFailureReason")) | |
} | |
} | |
} | |
$DataTable.DefaultView.Sort = "RunStartTime DESC" | |
$DataTable = $DataTable.DefaultView.ToTable() | |
Return $DataTable | |
} | |
} | |
Process | |
{ | |
foreach ($Computer in $ComputerName) | |
{ | |
If ($Computer -eq $env:COMPUTERNAME) | |
{ | |
$Result = Invoke-Command -ScriptBlock $Code | |
} | |
Else | |
{ | |
$Result = Invoke-Command -ComputerName $Computer -HideComputerName -ScriptBlock $Code -ErrorAction Continue | |
} | |
$Result | Select ComputerName,PackageName,PackageID,ProgramName,DeploymentStatus,Context,State,RunStartTime,SuccessOrFailureCode,SuccessOrFailureReason | |
} | |
} | |
End | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment