-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-Device.ps1
139 lines (125 loc) · 3.66 KB
/
Remove-Device.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
<#PSScriptInfo
.VERSION 1.0
.AUTHOR Ian Meigh
.COMPANYNAME UOD
.EXTERNALSCRIPTDEPENDENCIES
Join-Object.ps1
Start-SCCMRuntimeChecks.ps1
.RELEASEDATE 10/04/19
.RELEASENOTES
.TODO
- Remove from Azure
#>
<#
.SYNOPSIS
This function will remove an array of hostnames from SCCM.
.DESCRIPTION
This function will remove a array of hostname from SCCM. It will report on devices that cannot be found.
.EXAMPLE
Remove-SCCM $array
Command will remove the supplied array of hostnames from SCCM.
#>
Function Remove-SCCM ($computers) {
$results = foreach ($computer in $computers) {
if ($null -eq (Get-CMDevice -name $computer)) {
$SCCMResult = "Not found"
}
else
{
Remove-CMDevice -DeviceName $computer -Force
$SCCMResult = "Success"
}
[pscustomobject]@{
ComputerName = $computer
SCCMResult = $SCCMResult
}
}
return $results
}
<#
.SYNOPSIS
This function will remove an array of hostnames from AD.
.DESCRIPTION
This function will remove a array of hostname from AD. It will report on devices that cannot be found.
.EXAMPLE
Remove-AD $array
Command will remove the supplied array of hostnames from AD.
#>
Function Remove-AD ($computers) {
$results = foreach ($computer in $computers) {
try {
Get-ADComputer -Identity $computer | Remove-ADComputer -Confirm:$false -ErrorAction SilentlyContinue
$ADResult = "Success"
} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
$ADResult = "Not found"
} catch [Microsoft.ActiveDirectory.Management.Commands.RemoveADComputer] {
try {
Get-ADComputer -Identity $computer | Remove-ADObject -Recursive -Confirm:$false
$ADResult = "Success - W10"
} catch {
$ADResult = $error[0].message
}
}
[pscustomobject]@{
ComputerName = $computer
ADResult = $ADResult
}
}
return $results
}
<#
.SYNOPSIS
This function will remove a host from either AD, SCCM or Both.
.DESCRIPTION
This function will remove a given hostname (either specified explicilty or from the pipeline) from either AD, SCCM or
both (if no switch parameters are specified). It will report on devices that cannot be found.
.INPUTS
System.Array - An array of hostnames.
.OUTPUTS
System.Object
.PARAMETER ComputerName
Mandatory parameter - Hostname to be removed.
.PARAMETER AD
Switch parameter which when specified will only remove the object from AD.
.PARAMETER SCCM
Switch parameter which when specified will only remove the object from SCCM.
.EXAMPLE
Remove-Device HOST1
Command will remove the specified Host from AD and SCCM.
.EXAMPLE
Get-Content "C:\hostnames.txt" | Remove-Device -AD
Command will remove the piped hostnames from AD only.
#>
Function Remove-Device {
param
(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true)]
$ComputerName,
[Switch]$AD,
[Switch]$SCCM
)
Begin {
Start-SCCMRuntimeChecks
$array = @()
}
Process { $array += $ComputerName }
End {
if ($SCCM)
{
write-host "SCCM ONLY"
Remove-SCCM $array
}
if ($AD) {
write-host "AD"
Remove-AD $array
}
if (!($SCCM -or $AD)) {
$SCCMResults = Remove-SCCM $array
$ADResults = Remove-AD $array
#merge objects
$results = $SCCMResults | LeftJoin $ADResults -On ComputerName
return $results
}
}
}