Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Created November 24, 2025 19:44
Show Gist options
  • Select an option

  • Save MyITGuy/1cc47b974f6ec552f2326f396cc040b6 to your computer and use it in GitHub Desktop.

Select an option

Save MyITGuy/1cc47b974f6ec552f2326f396cc040b6 to your computer and use it in GitHub Desktop.
function Get-CMSoftwareDistribution {
[CmdletBinding()]
PARAM(
[Alias('PKG_Name')]
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]
$PackageName
,
[Alias('PKG_PackageID')]
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]
$PackageId
,
[Alias('PRG_ProgramName')]
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]
$ProgramName
,
[Alias('PRG_ProgramID')]
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]
$ProgramId
,
[Alias('ADV_AdvertisementID')]
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string]
$AdvertisementId
)
begin {
Write-Verbose $MyInvocation.MyCommand
filter filterByPackageName {
$Object = $_
if ($PackageName) {
$Object | Where-Object PKG_Name -eq $PackageName
} else {
$Object
}
}
filter filterByPackageId {
$Object = $_
if ($PackageId) {
$Object | Where-Object PKG_PackageID -eq $PackageId
}
else {
$Object
}
}
filter filterByProgramName {
$Object = $_
if ($ProgramName) {
$Object | Where-Object PRG_ProgramName -eq $ProgramName
}
else {
$Object
}
}
filter filterByProgramId {
$Object = $_
if ($ProgramId) {
$Object | Where-Object PRG_ProgramID -eq $ProgramId
}
else {
$Object
}
}
filter filterByAdvertisementId {
$Object = $_
if ($AdvertisementId) {
$Object | Where-Object ADV_AdvertisementID -eq $AdvertisementId
}
else {
$Object
}
}
}
process {
try {
# Execute on any computer with CM client
# Get CCM_SoftwareDistribution for reading
Get-CimInstance -ClassName "CCM_SoftwareDistribution" -Namespace "ROOT\ccm\policy\machine\actualconfig" | filterByPackageName | filterByPackageId | filterByProgramName | filterByProgramId | filterByAdvertisementId | ForEach-Object {
$CCM_SoftwareDistribution = $_
$ADV_AdvertisementID = $CCM_SoftwareDistribution.ADV_AdvertisementID
$PKG_PackageID = $CCM_SoftwareDistribution.PKG_PackageID
$PRG_ProgramID = $CCM_SoftwareDistribution.PRG_ProgramID
# Get CCM_SoftwareDistribution
# The task delivered by CM does not allow for re-running. This will modify that behavior just before execution.
$Program = ([wmi]"ROOT\ccm\policy\machine\actualconfig:CCM_SoftwareDistribution.ADV_AdvertisementID='$($ADV_AdvertisementID)',PKG_PackageID='$($PKG_PackageID)',PRG_ProgramID='$($PRG_ProgramID)'")
$Program
}
}
catch {
Throw $_
}
}
end {
}
}
#endregion Get-CMSoftwareDistribution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment