/
SCCMADCleanup.ps1
229 lines (196 loc) · 7.13 KB
/
SCCMADCleanup.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<#
.SYNOPSIS
SCCM AD Cleanup
.DESCRIPTION
This script removes systems from SCCM that are populated via active directory, but have been disabled in AD.
.PARAMETER SCCMServer
Name of the SCCM server
.PARAMETER SCCMDrive
SCCM Drive
.PARAMETER SCCMCollection
SCCM collection to query for cleanup
.PARAMETER ReportOnly
Produce a report only
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.143
Created on: 8/3/2017 9:07 AM
Created by: Mick Pletcher
Filename: SCCMADCleanup.ps1
.EXAMPLE
powershell.exe -file SCCMADCleanup.ps1 -SCCMServer AtlantaSCCM -SCCMDrive ATL
===========================================================================
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]$SCCMServer='BNASCCM',
[ValidateNotNullOrEmpty()][string]$SCCMDrive='BNA',
[ValidateNotNullOrEmpty()][string]$SCCMCollection = 'All Systems',
[switch]$ReportOnly
)
function Get-RelativePath {
<#
.SYNOPSIS
Get the relative path
.DESCRIPTION
Returns the location of the currently running PowerShell script
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([string])]
param ()
$Path = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"
Return $Path
}
function Import-SCCMModule {
<#
.SYNOPSIS
Import SCCM Module
.DESCRIPTION
Locate the ConfigurationManager.psd1 file and import it.
.PARAMETER SCCMServer
Name of the SCCM server to connect to.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]$SCCMServer
)
#Get the architecture of the specified SCCM server
$Architecture = (get-wmiobject win32_operatingsystem -computername $SCCMServer).OSArchitecture
#Get list of installed applications
$Uninstall = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Force -ErrorAction SilentlyContinue }
If ($Architecture -eq "64-bit") {
$Uninstall += Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Force -ErrorAction SilentlyContinue }
}
#Get the registry key that specifies the location of the SCCM installation drive and directory
$RegKey = ($Uninstall | Where-Object { $_ -like "*SMS Primary Site*" }) -replace 'HKEY_LOCAL_MACHINE', 'HKLM:'
$Reg = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ItemProperty -Path $args[0] } -ArgumentList $RegKey
#Parse the directory listing
$Directory = (($Reg.UninstallString).Split("\", 4) | Select-Object -Index 0, 1, 2) -join "\"
#Locate the location of the SCCM module
$Module = Invoke-Command -ComputerName $SCCMServer -ScriptBlock { Get-ChildItem -Path $args[0] -Filter "ConfigurationManager.psd1" -Recurse } -ArgumentList $Directory
#If more than one module is present, use the latest one
If ($Module.Length -gt 1) {
foreach ($Item in $Module) {
If (($NewModule -eq $null) -or ($Item.CreationTime -gt $NewModule.CreationTime)) {
$NewModule = $Item
}
}
$Module = $NewModule
}
#format the $Module unc path
[string]$Module = "\\" + $SCCMServer + "\" + ($Module.Fullname -replace ":", "$")
#Import the SCCM module
Import-Module -Name $Module
}
function Get-SCCMCollectionList {
<#
.SYNOPSIS
Retrieve Collection List
.DESCRIPTION
Query the specifies collection in SCCM for a list of all systems residing in that collection.
.PARAMETER CollectionName
Name of SCCM collection to query
.EXAMPLE
PS C:\> Get-SCCMCollectionList
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]$CollectionName
)
#FQDN of SCCM Server
$FQDN = ([System.Net.Dns]::GetHostByName($SCCMServer)).HostName
#Create new SCCM drive
New-PSDrive -Name $SCCMDrive -PSProvider "AdminUI.PS.Provider\CMSite" -Root $FQDN -Description $SCCMDrive"Primary Site" | Out-Null
#Add colon at end of SCCMDrive if it does not exist
If ($SCCMDrive[$SCCMDrive.Length - 1] -ne ":") {
$SCCMDrive = $SCCMDrive + ":"
}
#Change to SCCM drive
Set-Location $SCCMDrive
#Get the collection ID for retrieving the list of systems
$CollectionID = (Get-CMDeviceCollection | Where-Object { $_.Name -eq $SCCMCollection }).CollectionID
#Get list of systems from the specified collection
$CollectionSystems = (Get-CMDevice -CollectionId $CollectionID).Name | Where-Object { $_ -notlike "*Unknown Computer*" } | Sort-Object
#Change location to the local drive
Set-Location $env:HOMEDRIVE
#Create Collection array
$Collection = @()
foreach ($System in $CollectionSystems) {
try {
$ADSystem = (Get-ADComputer $System).Enabled
} catch {
$ADSystem = $false
}
$objSystem = New-Object System.Object
$objSystem | Add-Member -MemberType NoteProperty -Name Name -Value $System
$objSystem | Add-Member -MemberType NoteProperty -Name Enabled -Value $ADSystem
$Collection += $objSystem
}
Return $Collection
}
function Remove-Systems {
<#
.SYNOPSIS
Remove Disabled Systems
.DESCRIPTION
Remove disabled active directory systems from SCCM
.PARAMETER Collection
List of all machines in the $SCCMCollection
.EXAMPLE
PS C:\> Remove-Systems
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]$Collection
)
#FQDN of SCCM Server
$FQDN = ([System.Net.Dns]::GetHostByName($SCCMServer)).HostName
#Create new SCCM drive
New-PSDrive -Name $SCCMDrive -PSProvider "AdminUI.PS.Provider\CMSite" -Root $FQDN -Description $SCCMDrive"Primary Site" | Out-Null
#Add colon at end of SCCMDrive if it does not exist
If ($SCCMDrive[$SCCMDrive.Length - 1] -ne ":") {
$SCCMDrive = $SCCMDrive + ":"
}
#Change to SCCM drive
Set-Location $SCCMDrive
#Parse through list and delete systems from SCCM
foreach ($System in $Collection) {
If ($System.Enabled -eq $False) {
Remove-CMDevice -Name $System.Name -Force
}
}
#Change location to the local drive
Set-Location $env:HOMEDRIVE
}
Clear-Host
Import-Module ActiveDirectory
Import-SCCMModule -SCCMServer $SCCMServer
#Get execution path of this script
$RelativePath = Get-RelativePath
$Collection = Get-SCCMCollectionList -CollectionName "All Systems"
If (!($ReportOnly.IsPresent)) {
Remove-Systems -Collection $Collection
$Collection
$File = $RelativePath + "DeletedSystems.csv"
$Collection | Out-File -FilePath $File -Encoding UTF8 -NoClobber -force
} else {
#Location and name of .CSV to write the output to
$File = $RelativePath + "DisabledSystems.csv"
#Delete file if it exists
If ((Test-Path $File) -eq $true) {
Remove-Item -Path $File -Force
}
$Collection
$Collection | Out-File -FilePath $File -Encoding UTF8 -NoClobber -force
}