-
Notifications
You must be signed in to change notification settings - Fork 3
/
Remove-Deployments.ps1
55 lines (40 loc) · 1.63 KB
/
Remove-Deployments.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[CmdletBinding()]
param (
[Parameter(Mandatory,HelpMessage='Enter the Collection name. WildCards Accepted (*)')]
[string]
$CollectionName,
[Parameter(Mandatory,HelpMessage='ConfigMgr Site Server address')]
[string]
$SiteServer,
[Parameter(Mandatory,HelpMessage='ConfigMgr Site Code')]
[string]
$SiteCode
)
begin {
if (-not (Get-Module -Name 'ConfigurationManager')) {
Write-Progress 'Importing Configuration Manager PSModule'
Import-Module (Join-Path (Split-Path $env:SMS_ADMIN_UI_PATH -Parent) 'ConfigurationManager.psd1')
}
if (-not (Get-PSDrive -Name $SiteCode -ErrorAction SilentlyContinue)) {
Write-Progress 'Creating PS Site Drive'
New-PSDrive -Name $SiteCode -PSProvider 'CMSite' -Root $SiteServer -Description 'ConfigMgr Site'
}
Push-Location -Path ('{0}:' -f $SiteCode)
Write-Progress 'Getting Collection Information.'
$Collections = Get-CMCollection -Name $CollectionName
}
process {
foreach ($Collection in $Collections) {
Write-Progress -Id 0 -Activity ('Checking: {0}' -f $Collection.Name)
$Deployments = Get-CMApplicationDeployment -CollectionId $Collection.CollectionID
if ($Deployments.Count -le 5) { continue }
$Deployments = $Deployments | Sort-Object -Property CreationTime | Select-Object -First $($Deployments.Count - 5)
$Deployments | ForEach-Object {
Write-Progress -Id 1 -Parent 0 -Activity ('Removing: {0}' -f $_.AssignmentName)
Remove-CMApplicationDeployment -InputObject $_ -Confirm:$false -Force:$true
}
}
}
end {
Pop-Location
}