-
Notifications
You must be signed in to change notification settings - Fork 2
/
Add-CMSystemDiscoveryMethodContainer.ps1
259 lines (168 loc) · 8.39 KB
/
Add-CMSystemDiscoveryMethodContainer.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#Requires -Version 3.0
Function Add-CMSystemDiscoveryMethodContainer {
<#
.SYNOPSIS
Import Organizational Units (OU) defined by their distinguished name to an Active Directory System Discovery components in ConfigMgr
.DESCRIPTION
This cmdlet uses the distinguished name defined in -SearchBase to the System Discovery method on an SCCM server. Existing containers for the specified Discovery Method will be preserved. If a container is already present, it will not be added again.
.PARAMETER SiteServer
SCCM Site Server to connect too
.PARAMETER UseSSL
Creates CIM session to SCCM server using SSL (WinRM over HTTPS)
.PARAMETER SkipCACheck
Skip Certificate authority trust check on WinRM connnection
.PARAMETER SkipCNCheck
Skip verifying the CN/subject name on the WinRM certificate
.PARAMETER SkipRevocationCheck
Skip checking certificate revocation for WinRM connections
.PARAMETER SearchBase
Define the LDAP container OU path to new domains being added to System Discovery
.PARAMETER RestartService
Tells the SCCM server to restart the System Discovery service after adding new containers to search for
.PARAMETER Credential
Enter credentials to authenticate to the SCCM server
.EXAMPLE
Add-CMSystemDiscoveryMethodContainer -SiteServer sccm-server.domain.com -SearchBase "DC=domain,DC=com"
# This example adds the domain.com LDAP search base filter to the domain System Discovery method on the sccm-server.domain.com SCCM server
.EXAMPLE
Add-CMSystemDiscoveryMethodContainer -SiteServer sccm-server.domain.com -UseSSL -SkipCACheck -SkipCNCheck -SkipRevocationCheck -SearchBase "LDAP:\\DC=domain,DC=com"
# This example adds the domain.com LDAP search base filter to the domain System Discovery method on the sccm-server.domain.com SCCM server
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.INPUTS
None
.OUTPUTS
None
#>
[CmdletBinding(
SupportsShouldProcess=$True,
ConfirmImpact="Medium"
)] # End CmdletBinding
param(
[Parameter(
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="Site server where the SMS Provider is installed.")] # End Parameter
[ValidateNotNullOrEmpty()]
[String]$SiteServer,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$UseSSL,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$SkipCACheck,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$SkipCNCheck,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$SkipRevocationCheck,
[Parameter(
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$False,
HelpMessage="Specify the Active Directory Search Base `nEXAMPLE: DC=domain,DC=com : ")] # End Parameter
[ValidateScript({$_ -like "*DC=*,DC=*"})]
[String]$SearchBase,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$RestartService
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
) # End param
BEGIN {
$ComponentName = "SMS_AD_SYSTEM_DISCOVERY_AGENT"
Try {
Write-Verbose -Message "Determining Site Code for Site server: '$($SiteServer)'"
$CIMSession = New-CimSession -Credential $Credential -ComputerName $SiteServer -SessionOption (New-CimSessionOption -UseSSL:$UseSSL.IsPresent -SkipCACheck:$SkipCACheck.IsPresent -SkipCNCheck:$SkipCNCheck.IsPresent -SkipRevocationCheck:$SkipReovcationCheck.IsPresent -Verbose:$False) -Verbose:$False
$SiteCodeObjects = Get-CimInstance -CimSession $CIMSession -Namespace "Root\SMS" -ClassName SMS_ProviderLocation -ErrorAction Stop
ForEach ($SiteCodeObject in $SiteCodeObjects) {
If ($SiteCodeObject.ProviderForLocalSite -eq $True) {
$SiteCode = $SiteCodeObject.SiteCode
Write-Verbose -Message "Site Code: $($SiteCode)"
Break
} # End If
} # End ForEach
} Catch [System.UnauthorizedAccessException] {
Throw "[x] Access denied"
} Catch [System.Exception] {
Throw "[x] Unable to determine Site Code"
} # End Try Catch Catch
If ($SearchBase -notlike "LDAP://*") {
$SearchBase = "LDAP://$($SearchBase)"
} # End If
Try {
$ContainerData = New-Object -TypeName PSCustomObject -Property @{
DistinguishedName="$($SearchBase)";
Recursive="Yes";
Group="Excluded";
} # End New-Object -Property
} Catch [System.Exception] {
Write-Warning -Message "$($_.Exception.Message). Line: $($_.InvocationInfo.ScriptLineNumber)"
Break
} # End Try Catch
$OptionTable = @{
Yes = 0
No = 1
Included = 0
Excluded = 1
} # End $OptionTable
} PROCESS {
# Determine existing containers for selected Discovery Method
Try {
$DiscoveryContainerList = New-Object -TypeName System.Collections.ArrayList
$DiscoveryComponent = Get-WmiObject -Class SMS_SCI_Component -Namespace "Root\SMS\Site_$($SiteCode)" -ComputerName $SiteServer -Filter "ComponentName like '$($ComponentName)'" -Credential $Credential -ErrorAction Stop -Verbose:$False
$DiscoveryPropListADContainer = $DiscoveryComponent.PropLists | Where-Object -FilterScript { $_.PropertyListName -like "AD Containers" }
If ($DiscoveryPropListADContainer.PropertyListName -eq "AD Containers") {
$DiscoveryContainerList.AddRange(@($DiscoveryPropListADContainer.Values)) | Out-Null
} # End If
} Catch [System.Exception] {
Throw "[x] Unable to determine existing discovery method component properties"
} # End Catch
ForEach ($ContainerItem in $ContainerData) {
If ($ContainerItem.DistinguishedName -notlike "LDAP://*") {
$ContainerItem.DistinguishedName = "LDAP://$($ContainerItem.DistinguishedName)"
} # End If
If ($ContainerItem.DistinguishedName -notin $DiscoveryContainerList) {
Write-Verbose -Message "Adding new container item $($ContainerItem.DistinguishedName)"
$DiscoveryContainerList.AddRange(@($ContainerItem.DistinguishedName, $OptionTable[$ContainerItem.Recursive], $OptionTable[$ContainerItem.Group])) | Out-Null
} Else {
Write-Verbose -Message "Detected duplicate container object: $($ContainerItem.DistinguishedName)"
} # End If Else
} # End ForEach
Write-Verbose -Message "Attempting to save changes made to the $($ComponentName) component PropList"
Try {
$DiscoveryPropListADContainer.Values = $DiscoveryContainerList
$DiscoveryComponent.PropLists = $DiscoveryPropListADContainer
If ($PSCmdlet.ShouldProcess($DiscoveryComponent, '$DiscoveryComponent.Put()')) {
$DiscoveryComponent.Put() | Out-Null
} # End If
} Catch [System.Exception] {
Throw "[x] Unable to save changes made to $($ComponentName) component"
} # End Try Catch
If ($RestartService.IsPresent) {
Write-Verbose -Message "Restarting the SMS_SITE_COMPONENT_MANAGER service"
Try {
Write-Output -InputObject "[*] Restarting the SMS_SITE_COMPONENT_MANAGER service on $SiteServer"
Get-CimInstance -CimSession $CIMSession -ClassName Win32_Service -Filter 'Name = "SMS_SITE_COMPONENT_MANAGER"' -Verbose:$False | Invoke-CimMethod -MethodName StopService -WhatIf:$False -Verbose:$False | Out-Null
Get-CimInstance -CimSession $CIMSession -ClassName Win32_Service -Filter 'Name = "SMS_SITE_COMPONENT_MANAGER"' -Verbose:$False | Invoke-CimMethod -MethodName StartService -WhatIf:$False -Verbose:$False | Out-Null
} Catch [System.Exception] {
Write-Warning -Message "$($_.Exception.Message). Line: $($_.InvocationInfo.ScriptLineNumber)"
Break
} # End Try Catch
} # End If
} END {
If ($CIMSession) {
Remove-CimSession -CimSession $CIMSession -Confirm:$False -WhatIf:$False -Verbose:$False
} # End If
} # End END
} # End Function Add-CMSystemDiscoveryMethodContainer