PoshCode Archive  Artifact Content

Artifact 8ad85e19c08621345ba980cb6a3ffc635a005b5d756036ad87f91cd9f49d12f5:

  • File Remove-SCCMDPContent.ps1 — part of check-in [91b62a620a] at 2018-06-10 13:49:15 on branch trunk — The Function Remove-SCCMDPContent will remove a list of PackageIDs from a Distribution Point. (user: DexterPOSH size: 4481)

# encoding: ascii
# api: powershell
# title: Remove-SCCMDPContent
# description: The Function Remove-SCCMDPContent will remove a list of PackageIDs from a Distribution Point.
# version: 3.0
# type: function
# author: DexterPOSH
# license: CC0
# function: Remove-SCCMDPContent
# x-poshcode-id: 5048
# x-archived: 2014-04-06T17:34:23
# x-published: 2014-04-03T05:13:00
# Have to specify the SCCM Server with SMS namespace installed.
# The big advantage is you don’t need any Module for this, standalone function and works for both ConfigMgr 07 & 12 (tested).
# It uses WQL filters to first get all the Packages distributed to a DP and the iterates over the list specified and finally removes the WMI Instances of the SMS_Distribution Class matching the PackageIDs.
function Remove-SCCMDPContent
    Function to Remove Packages from the DP
    THis Function will connect to the SCCM Server SMS namespace and then remove the Package IDs
    passed to the Function from the specified DP name.
    PS> Remove-SCCMDPContent -PackageID DEX123AB,DEX145CD -DPname DexDP -Computername DexSCCMServer  

    This will remove the Packages with Package IDs [ DEX123AB,DEX145CD] from the Distribution Point "DexDP".
    Author - DexterPOSH (Deepak Singh Dhami)
    BlogLink - http://dexterposh.blogspot.com/2014/02/use-powershell-to-remove-packages-from.html

#requires -version 3.0 
#requires -RunAsAdministrator
        # Specify the Package IDs which need to be removed from the DP

        # Pass the DP name where cleanup is to be done

        #Supply the SCCM Site Server hosting SMS Namespace Provider

        Write-Verbose -Message "[BEGIN]"
            $sccmProvider = Get-WmiObject -query "select * from SMS_ProviderLocation where ProviderForLocalSite = true" -Namespace "root\sms" -computername $ComputerName -errorAction Stop
            # Split up the namespace path
            $Splits = $sccmProvider.NamespacePath -split "\\", 4
            Write-Verbose "Provider is located on $($sccmProvider.Machine) in namespace $($splits[3])"
            # Create a new hash to be passed on later
            $hash= @{"ComputerName"=$ComputerName;"NameSpace"=$Splits[3];"Class"="SMS_DistributionPoint";"ErrorAction"="Stop"}
            #add the filter to get the packages there in the DP only
            $hash.Add("Filter","ServerNALPath LIKE '%$DPname%'")

            Write-Warning "Something went wrong while getting the SMS ProviderLocation"
            throw $Error[0].Exception
            Write-Verbose -Message "[PROCESS] Working to remove packages from DP --> $DPName  "
            #get all the packages in the Distribution Point
            $PackagesINDP = Get-WmiObject @hash
            #Filter the packages based on the PackageID
            $RemovePackages = $PackagesINDP | where {$PackageID -contains $_.packageid   }
            $Removepackages | ForEach-Object -Process { 
                    Remove-WmiObject  -InputObject $_  -ErrorAction Stop -ErrorVariable WMIRemoveError 
                    Write-Verbose -Message "Removed $($_.PackageID) from $DPname"
                    Write-Warning "[PROCESS] Something went wrong while removing the Package  from $DPname"
            }#End Foreach-Object
        }#End Process
        Write-Verbose "[END] Ending the Function"