Add task sequence dependencies to DP group

, , , , , ,
ConfigMgr

We’re working on adding new distribution point that are used exclusively for imaging.  This motivated me to create a script to make sure all dependent packages are assigned to the proper distribution point group.  (You are using distribution point groups right?)

Change the site code in the script and it will prompt you for the task sequence and then prompt for the distribution point group.  After that, magic!  🙂

I wasn’t planning on posting this one yet as it could use a lot of polish but I’m sure it’ll evolve as time goes on and I’ll do my best to keep this updated.

On to the code…

 

 

$siteCode = "CM1"

Import-Module ConfigurationManager
Push-Location
Set-Location "$($siteCode):"

$site = (Get-CMSite -SiteCode $siteCode)
$sequences = Get-CMTaskSequence | Select-Object -Property Name | Out-GridView -Title "Select task sequences for DG" -PassThru
$dg = Get-CMDistributionPointGroup | Out-GridView -Title "Select a distribution point group." -PassThru

if($ConfirmPreference -eq 'Low') {$conf = @{Confirm = $true}}

foreach ($tsname in $sequences)
{
    $ts = Get-CMTaskSequence -Name $tsname.Name
    Write-Host "References $($ts.References.Count)"
    foreach ($ref in $ts.References)
    {
        $pkgContentServer = $null
        $pkgId = $null

        if ($ref.Type -eq 0)
        {
            $pkgContentServer = Get-WmiObject -ComputerName $site.ServerName -Namespace "rootSMSsite_$($site.SiteCode)" -Class SMS_PackageContentServerInfo -Filter "PackageID = '$($ref.Package)' AND ContentServerID = '$($dg.GroupID)'"
            $pkgId = $ref.Package
        }
        elseif ($ref.Type -eq 1)
        {
            $app = Get-CMApplication -ModelName $ref.Package
            $pkgContentServer = Get-WmiObject -ComputerName $site.ServerName -Namespace "rootSMSsite_$($site.SiteCode)" -Class SMS_PackageContentServerInfo -Filter "PackageID = '$($app.PackageID)' AND ContentServerID = '$($dg.GroupID)'"
            $pkgId = $app.PackageID
        }

        if ($pkgContentServer -eq $null)
        {
            Write-Host "Adding distribution point group $($dg.Name) to package $($ref.Package)."
            if ($PSCmdlet.ShouldProcess("$($ref.Package)", "Distribute package"))
            {
                if ($ref.Type -eq 0)
                {
                    $baseObject = Get-WmiObject -ComputerName $site.ServerName -Namespace "rootSMSsite_$($site.SiteCode)" -Class SMS_PackageBaseClass -Filter "PackageID = '$pkgId'"
                    Start-CMContentDistribution -InputObject ($baseObject | ConvertTo-CMIResultObject) -DistributionPointGroupName $dg.Name #-WhatIf:([bool]$WhatIfPreference.IsPresent) #@conf
                }
                elseif ($ref.Type -eq 1)
                {
                    Start-CMContentDistribution -ApplicationId $app.CI_ID -DistributionPointGroupName $dg.Name
                }
            }
        }
    }

    $bootImage = Get-CMBootImage -Id $ts.BootImageID
    $pkgContentServer = Get-WmiObject -ComputerName $site.ServerName -Namespace "rootSMSsite_$($site.SiteCode)" -Class SMS_PackageContentServerInfo -Filter "PackageID = '$($bootImage.PackageID)' AND ContentServerID = '$($dg.GroupID)'"
    if ($pkgContentServer -eq $null)
    {
        Write-Host "Adding distribution point group $($dg.Name) to package $($bootImage.PackageID)."
        if ($PSCmdlet.ShouldProcess("$($bootImage.PackageID)", "Distribute package"))
        {
            $baseObject = Get-WmiObject -ComputerName $site.ServerName -Namespace "rootSMSsite_$($site.SiteCode)" -Class SMS_PackageBaseClass -Filter "PackageID = '$($bootImage.PackageID)'"
            Start-CMContentDistribution -InputObject ($baseObject | ConvertTo-CMIResultObject) -DistributionPointGroupName $dg.Name
        }
    }
}
Pop-Location


ALSO CHECK : Post OSD Scheduled Task
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen − four =

This site uses Akismet to reduce spam. Learn how your comment data is processed.