-
Notifications
You must be signed in to change notification settings - Fork 127
/
MECMADCleanup.ps1
128 lines (112 loc) · 3.33 KB
/
MECMADCleanup.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
<#
.SYNOPSIS
ConfigMgr Cleanup
.DESCRIPTION
This script will compare the All Systems list in ConfigMgr to systems in AD and delete systems from ConfigMgr that are disabled in AD. It will also report a list of systems that are greater than 30 days old since the last activity in AD.
.PARAMETER SQLServer
A description of the SQLServer parameter.
.PARAMETER SQLDatabase
A description of the SQLDatabase parameter.
.PARAMETER PSHCfgMgrModule
Path to ConfigurationManager.psd1 module
.PARAMETER Sitecode
Three character ConfigMgr site code
.PARAMETER SiteServer
FQDN of the Configuration Manager server
.PARAMETER DeleteSystems
Select to automatically delete systems from Configuration Manager
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2022 v5.8.208
Created on: 7/26/2022 8:00 AM
Created by: Mick Pletcher
Filename: MECMADCleanup.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$SQLServer,
[ValidateNotNullOrEmpty()]
[string]$SQLDatabase,
[string]$PSHCfgMgrModule,
[string]$SiteCode,
[string]$SiteServer,
[switch]$DeleteSystems
)
function Get-PSHModule {
<#
.SYNOPSIS
Import Module
.DESCRIPTION
Import specified module
.PARAMETER Module
Name of PowerShell Module
.PARAMETER NoInstall
Import only. Typically used for modules that are not in the PowerShell Gallery
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$Module,
[switch]$NoInstall
)
If ($NoInstall.IsPresent) {
Import-Module -Name $Module
}
else {
Try {
Import-Module -Name $Module
}
Catch {
Find-Module -Name $Module | Install-Module -Force
Import-Module -Name $Module
}
}
}
#Import SQL Server PowerShell Module
Get-PSHModule -Module "SqlServer"
#Import AD PowerShell module
Get-PSHModule -Module "ActiveDirectory"
$Systems = @()
#Get All Systems list from ConfigMgr
$List = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDatabase -Query "SELECT NAME FROM dbo._RES_COLL_SMS00001 ORDER BY Name"
foreach ($System in $List) {
#Filter out built-in accounts
If (($System.Name -notlike '*Unknown*') -and ($System.Name -notlike '*Provisioning*')) {
#Return a list of all systems either not in AD or that have been disabled
Try {
$AD = Get-ADComputer $System.Name
If ($AD.Enabled -eq $false) {
$Systems += $AD.Name
}
} catch {
$Systems += $System.Name
}
}
}
$Systems
$Systems.Count
If ($Systems.Count -ne 0) {
If ($DeleteSystems.IsPresent) {
#Import ConfigMgr Module
Get-PSHModule -Module $PSHCfgMgrModule -NoInstall
If ((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $SiteServer
}
Set-Location "$($SiteCode):\"
$Systems | ForEach-Object {
Write-Host ('Deleting ' + $_ + '.....') -NoNewline
Remove-CMDevice -Name $_ -Force
If ((Get-CMDevice -Name $_) -eq $null) {
Write-Host 'Success' -ForegroundColor Yellow
} else {
Write-Host 'Failed' -ForegroundColor Red
}
}
}
}