Задача: удалить контент пакетов с SCCM Distribution point серверов, чтобы они не занимали место на них, при этом не удаляя самих пакетов.
Скрипт получает списков DP серверов, на которые указанный пакет распространен (distributed), а затем удаляет контент на каждом DP. Для каждого типа пакетов используется свой синтаксис командлета Remove-CMContentDistribution.
#Powershell script to remove SCCM content of the packages from distribution points without remove the package itself.
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
# Site configuration
$SiteCode = "CEN" # Site code
$ProviderMachineName = "your.central.sscm" # SMS Provider machine name
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
}
# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName
}
# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams
$date = Get-Date
$log = "c:\temp\remove_cmcontent_all_dp.txt"
"" | Out-File $log -Append
"==== $date ==== " | Out-File $log -Append
$pkg = Read-Host "Enter PackageID"
$query = Get-WmiObject -NameSpace Root\SMS\Site_CEN -Class SMS_DistributionDPStatus -ComputerName $ProviderMachineName -Filter "PackageID='$pkg'" | Select Name, ObjectTypeID
$ObjType = $query.ObjectTypeID
$dplist = $query.Name
if ($ObjType -eq "2") {
"Package Type is Regular package"
try
{
foreach ($DPname in $dplist)
{
Write-host "Working on package $pkg --> $DPname" -ForegroundColor Yellow
"Working on package $pkg --> $DPname">>$log
Remove-CMContentDistribution -DistributionPointName $DPname -Force -PackageId $pkg -ErrorAction Continue
Write-host "Completed the package $pkg Removal from $DPname" -ForegroundColor green
"Completed the package $pkg Removal from $DPname">>$log
}
}
catch
{
#Catch the exception here
Write-host "-Exeception error on $DPname. Check logFile more details" -ForegroundColor Red
#Write the server name to $log
"$pkg has thrown asn error $($_.exception)" >> $log
}
}
if ($ObjType -eq "18") {
"Package Type is OS Image"
try
{
foreach ($DPname in $dplist)
{
Write-host "Working on package $pkg --> $DPname" -ForegroundColor Yellow
"Working on package $pkg --> $DPname">>$log
Remove-CMContentDistribution -DistributionPointName $DPname -Force -OperatingSystemImageId $pkg -ErrorAction Continue
Write-host "Completed the package $pkg Removal from $DPname" -ForegroundColor green
"Completed the package $pkg Removal from $DPname">>$log
}
}
catch
{
#Catch the exception here
Write-host "-Exeception error on $DPname. Check logFile more details" -ForegroundColor Red
#Write the server name to $log
"$pkg has thrown asn error $($_.exception)" >> $log
}
}
if ($ObjType -eq "19") {
"Package Type is Boot Image"
try
{
foreach ($DPname in $dplist)
{
Write-host "Working on package $pkg --> $DPname" -ForegroundColor Yellow
"Working on package $pkg --> $DPname">>$log
Remove-CMContentDistribution -DistributionPointName $DPname -Force -BootImageId $pkg -ErrorAction Continue
Write-host "Completed the package $pkg Removal from $DPname" -ForegroundColor green
"Completed the package $pkg Removal from $DPname">>$log
}
}
catch
{
#Catch the exception here
Write-host "-Exeception error on $DPname. Check logFile more details" -ForegroundColor Red
#Write the server name to $log
"$pkg has thrown asn error $($_.exception)" >> $log
}
}
if ($ObjType -eq "14") {
"Package Type is OS Upgrade Package"
try
{
foreach ($DPname in $dplist)
{
Write-host "Working on package $pkg --> $DPname" -ForegroundColor Yellow
"Working on package $pkg --> $DPname">>$log
Remove-CMContentDistribution -DistributionPointName $DPname -Force -OperatingSystemInstallerId $pkg -ErrorAction Continue
Write-host "Completed the package $pkg Removal from $DPname" -ForegroundColor green
"Completed the package $pkg Removal from $DPname">>$log
}
}
catch
{
#Catch the exception here
Write-host "-Exeception error on $DPname. Check logFile more details" -ForegroundColor Red
#Write the server name to $log
"$pkg has thrown asn error $($_.exception)" >> $log
}
}
if ($ObjType -eq "23") {
"Package Type is Driver Package"
try
{
foreach ($DPname in $dplist)
{
Write-host "Working on package $pkg --> $DPname" -ForegroundColor Yellow
"Working on package $pkg --> $DPname">>$log
Remove-CMContentDistribution -DistributionPointName $DPname -Force -DriverPackageId $pkg -ErrorAction Continue
Write-host "Completed the package $pkg Removal from $DPname" -ForegroundColor green
"Completed the package $pkg Removal from $DPname">>$log
}
}
catch
{
#Catch the exception here
Write-host "-Exeception error on $DPname. Check logFile more details" -ForegroundColor Red
#Write the server name to $log
"$pkg has thrown an error $($_.exception)" >> $log
}
}