Monday, January 4, 2016

Remove packages from DP from SCCM 2007 using Powershell

Here's a script to remove all packages from a DP in 2007 scheduled to be decommissioned.  I'm aware of the cmd-lets that are available for this but unfortunately Windows 2003 R2 can only have PS v2 and internal firewall rules prevents me from running this from a remote 2008 R2 servers, this is what I have come up with a variation of what I found of adding a package to a DP but instead to delete.

$ErrorActionPreference = "SilentlyContinue"
$SCCMServer = ''YourSCCMSERVER"
$SMSSitecode = "YourSiteCode"
$DP = "YourDP"

$pkgs = Get-WMIObject -ComputerName $SCCMServer -Namespace "root\sms\site_$SMSSitecode"  -Query "Select * From SMS_DistributionPoint WHERE ServerNalPath like '%$DP%' and SourceSite = '$smssidecode'"
foreach ($pkg in $pkgs){write-host $pkg.PackageID;$ID.Delete()}

If you want to find out the ServerNalPath of the servers replace the code with the below.  If you have PXE shares, you may end up accidentally deleting something you don't want but those shares shouldn't be holding software packages.  Enter a packageID to limit the results or it will pull the ServerNALPath from all packageIDs.

$pkgs = Get-WMIObject -ComputerName $SCCMServer -Namespace "root\sms\site_$SMSSitecode"  -Query "Select * From SMS_DistributionPOint WHERE PackageID = '$packageid' and SourceSite = 'XXX'"

foreach ($pkg in $pkgs){write-host $pkg.ServerNalPath}

Replace XXX with your site code above.

Good resources for scripting in general and SCCM related.

https://technet.microsoft.com/en-us/scriptcenter/bb410849.aspx

https://msdn.microsoft.com/en-us/library/cc145334.aspx

No comments: