Repository navigation
Expand file tree
/
Copy pathdeleteDuplicateComputerRecord.ps1
More file actions
92 lines (63 loc) · 3.81 KB
/
Copy pathdeleteDuplicateComputerRecord.ps1
File metadata and controls
92 lines (63 loc) · 3.81 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
############################################################################################################################
### Automated deletion of ConfigMgr duplicate computer records
### Fields requiring modification are contained in [BRACKETS] below
### olearym
### Changelog: 2019-02-28: Added logging and email notification
### 2019-03-01: Added logging notation if script runs without duplicate computers present in the SCCM database
### 2019-10-30: Sanitized for public release
############################################################################################################################
$LogFile = "[YOURLOGFILEHERE].log"
$SiteCode = "[CM1]"
$SiteServerAddress = "[SITE SERVER ADDRESS]"
Import-Module $(Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
$null = New-PSDrive -Name "$SiteCode" -PSProvider "AdminUI.PS.Provider\CMSite" -Root "$SiteServerAddress" -Description "SCCM Site" -ErrorAction SilentlyContinue
cd "$($SiteCode):"
# Set actionTaken flag for logging purposes
$actionTaken = $FALSE
# Logwriting Function
Function LogWrite ([string]$logstring) {
c:
Add-content -path "$Logfile" -value "$logstring"
}
# email function
function sendmail($subject, $body) {
$EmailFrom = "[EMAIL ORIGIN]"
$EmailTo = "[EMAIL DESTINATION]"
$EmailBody = $body
$EmailSubject = $subject
$SMTPServer = "[SMTP SERVER ADDRESS]"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $subject -body $EmailBody -SmtpServer $SMTPServer
}
# Generate alpha sorted array of unique duplicate computer names from SCCM "Duplicate Computer Names" query
$duplicateComputersUnique = invoke-cmquery -name "duplicate computer names" | sort-object -property name -unique | select-object -expandproperty name
# Loop through duplicate computer name list & recursively loop through all SCCM device objects associated with that name. Take action on records where Client Presence = FALSE
foreach ($computer in $duplicateComputersUnique) {
cd "$($SiteCode):"
Get-CMDevice -name $computer | % {
if ($_.IsClient -eq $False) {
# Remove bunk SCCM device record
Remove-CMResource -ResourceID $_.ResourceID -force
# email log data
$subjectString = "A duplicate computer record for device $($_.name) has been deleted from the SCCM database."
$body = "A duplicate computer record for device $($_.name) was deleted from the SCCM database at $(get-date).`n`nThis action was performed by a script located at [PATH TO SCRIPT]\deleteDuplicateComputerRecord.ps1.`n`nThis information has also been logged to $LogFile."
sendmail $subjectString $body
# Write action to log
logwrite $(get-date), $subjectString
# Modify and revert changes to associated AD object in order to trigger an AD Delta Discovery update
$ADdescriptionMod = "This field was modified by a script written by [YOUR NAME HERE] to address duplicate computer records in SCCM. If you are reading this, please contact him as soon as possible. $(get-date -format o)"
$ComputerDn = ([ADSISEARCHER]"sAMAccountName=$($_.name)$").FindOne().Path
$ADComputer = [ADSI]$ComputerDn
$ADComputer.description = $ADdescriptionMod
$ADComputer.SetInfo()
$ADdescriptionMod = $null
$ADComputer = [ADSI]$ComputerDn
$ADComputer.description.Remove("$($ADComputer.description)")
$ADComputer.SetInfo()
# Flip actionTaken flag to TRUE
$actionTaken = $TRUE
}
}
}
# Record that no action was taken if script has run without duplicate computers present in the SCCM database
if ($actionTaken -eq $FALSE) {logwrite $(get-date), "A task sequence has completed without duplicate computers present in the SCCM database."}
remove-PSDrive -Name $SiteCode