-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathSCCMDuplicateCleanup.ps1
More file actions
74 lines (62 loc) · 2.59 KB
/
SCCMDuplicateCleanup.ps1
File metadata and controls
74 lines (62 loc) · 2.59 KB
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
<#
.SYNOPSIS
SCCM Duplicate Cleanup
.DESCRIPTION
This script will query for a list of machines with error 120 when trying to install the SCCM client. This error indicates the system is a duplicate.
.PARAMETER SCCMModule
UNC path including file name of the configuration manager module
.PARAMETER SCCMServer
FQDN of SCCM Server
.PARAMETER SCCMSiteDescription
Description of the SCCM Server
.PARAMETER SiteCode
Three letter SCCM Site Code
.PARAMETER Collection
Name of the collection to query
.PARAMETER SQLServer
Name of the SQL server
.PARAMETER SQLDatabase
A description of the SQLDatabase parameter
.PARAMETER SQLInstance
Name of the SQL Database
.PARAMETER SCCMFQDN
Fully Qualified Domain Name of the SCCM server
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.142
Created on: 10/3/2019 12:04 PM
Created by: Mick Pletcher
Filename: SCCMDuplicateCleanup.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]
[string]$SCCMModule,
[ValidateNotNullOrEmpty()]
[string]$SCCMServer,
[ValidateNotNullOrEmpty()]
[string]$SCCMSiteDescription,
[ValidateNotNullOrEmpty()]
[string]$SiteCode,
[ValidateNotNullOrEmpty()]
[string]$Collection,
[ValidateNotNullOrEmpty()]
[string]$SQLServer,
[ValidateNotNullOrEmpty()]
[string]$SQLDatabase
)
$List = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDatabase -Query ('SELECT Name, MachineID, CP_LastInstallationError FROM' + [char]32 + 'dbo.' + ((Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDatabase -Query ('Select ResultTableName FROM dbo.Collections WHERE CollectionName =' + [char]32 + [char]39 + $Collection + [char]39)).ResultTableName) + [char]32 + 'WHERE ClientVersion IS NULL AND CP_LastInstallationError = 120 Order By MachineID')
If ($List -ne '') {
Import-Module -Name $SCCMModule -Force
New-PSDrive -Name $SiteCode -PSProvider 'AdminUI.PS.Provider\CMSite' -Root $SCCMServer -Description $SCCMSiteDescription | Out-Null
Set-Location -Path ($SiteCode + ':')
#Test with output to screen before enabling the other line that also deletes each item
$List | ForEach-Object { (Get-CMDevice -ResourceId $_.MachineID -Fast).Name }
#$List | ForEach-Object { Get-CMDevice -ResourceId $_.MachineID -Fast | Remove-CMDevice -Confirm:$false -Force }
Remove-PSDrive -Name $SiteCode -Force
Write-Output ($List.Name | Sort-Object)
} else {
Exit 1
}