-
Notifications
You must be signed in to change notification settings - Fork 0
/
ITG.DomainUtils.Printers.Configuration.ps1
339 lines (325 loc) · 10.6 KB
/
ITG.DomainUtils.Printers.Configuration.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
$ConfigCache = @{};
$ConfigContainerName = 'ITG DomainUtils';
$ConfigObjectName = 'ITG DomainUtils Printers';
$ConfigContainerParentDN = 'CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration';
$ConfigContainerDN = ( ( "CN=$ConfigContainerName", $ConfigContainerParentDN | ? { $_ } ) -join ',' );
$ConfigObjectDN = ( ( "CN=$ConfigObjectName", $ConfigContainerDN | ? { $_ } ) -join ',' );
Function Initialize-DomainUtilsPrintersConfiguration {
<#
.Synopsis
Èíèöèàëèçàöèÿ êîíôèãóðàöèè ìîäóëÿ.
.Description
Èíèöèàëèçèðóåò êîíôèãóðàöèþ ìîäóëÿ. Êîíôèãóðàöèÿ ìîäóëÿ ñîõðàíÿåòñÿ â Active Directory, ïîëíûé ïóòü
äëÿ äîìåíà csm.nov.ru áóäåò
`CN=ITG DomainUtils,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=csm,DC=nov,DC=ru`.
Êîíôèãóðàöèÿ ñîõðàíÿåòñÿ â AD (è çàãðóæàåòñÿ èç AD) ïî ðÿäó ïðè÷èí.  ÷àñòíîñòè, ìîäóëü ïîääåðæèâàåò
ëîêàëèçàöèþ è ïîääåðæèâàåò ðàçëè÷íûå êëàññû êîíòåéíåðîâ. Íî ñ ìîìåíòà íà÷àëà åãî ïðèìåíåíèÿ âñåìè
àäìèíèñòðàòîðàìè äîìåíà äîëæíà èñïîëüçîâàòüñÿ îäíà è òà æå êîíôèãóðàöèÿ ìîäóëÿ, â ÷àñòíîñòè - òèïû
êîíòåéíåðîâ, êîðíåâîé êîíòåéíåð äëÿ ðàçìåùåíèÿ ñîçäàâàåìûõ âñïîìîãàòåëüíûõ ãðóïï, è òàê äàëåå.
 ñëó÷àå íàëè÷èÿ êîíôèãóðàöèè â AD å¸ ìîæíî ïðèíóäèòåëüíî ïåðåçàïèñàòü, èñïîëüçóÿ êëþ÷ `-Force`,
îäíàêî ýòîò øàã íå ïðèâåä¸ò ê ôàêòè÷åñêîìó èçìåíåíèþ òèïîâ êîíòåéíåðîâ, èõ ìåñòîïîëîæåíèÿ,
àòòðèáóòîâ ñîçäàííûõ ãðóïï. Ïîýòîìó ïîëüçîâàòüñÿ ýòîé âîçìîæíîñòüþ ñëåäóåò ñ îñîáîé îñòîðîæíîñòüþ.
.Link
https://github.com/IT-Service/ITG.DomainUtils.Printers#Initialize-DomainUtilsPrintersConfiguration
.Example
Initialize-DomainUtilsPrintersConfiguration
Èíèöèàëèçèðóåì êîíôèãóðàöèþ ìîäóëÿ äëÿ äîìåíà ïîëüçîâàòåëÿ, îò èìåíè êîòîðîãî âûïîëíåí êîìàíäëåò.
#>
[CmdletBinding(
SupportsShouldProcess = $true
, ConfirmImpact = 'High'
, HelpUri = 'https://github.com/IT-Service/ITG.DomainUtils.Printers#Initialize-DomainUtilsPrintersConfiguration'
)]
param (
# Äîìåí, äëÿ êîòîðîãî èíèöèàëèçèðóåì êîíôèãóðàöèþ ìîäóëÿ. Åñëè íå óêàçàí - äîìåí ïîëüçîâàòåëÿ, îò èìåíè êîòîðîãî çàïóùåí ñöåíàðèé.
[Parameter(
Mandatory = $false
)]
[String]
$Domain = ( ( Get-ADDomain ).DNSRoot )
,
# Ïóòü (DN) ê êîíòåéíåðó AD, â êîòîðîì ðàñïîëîæåíû âñå êîíòåéíåðû, èñïîëüçóåìûå óòèëèòàìè äàííîãî ìîäóëÿ.
# Óêàçûâàåòñÿ áåç DN äîìåíà.
# Íàïðèìåð, äëÿ `CN=ITG,DC=csm,DC=nov,DC=ru` ñëåäóåò óêàçàòü `CN=ITG`.
#
# Ïî óìîë÷àíèþ â êà÷åñòâå êîðíåâîãî êîíòåéíåðà èñïîëüçóåòñÿ êîðíåâîé êîíòåéíåð äîìåíà.
[Parameter(
Mandatory = $false
)]
[String]
$DomainUtilsBase
,
# Êëàññ êîíòåéíåðîâ, èñïîëüçóåìûõ äàííûì ìîäóëåì
[Parameter(
Mandatory = $false
)]
[String]
[ValidateSet(
'container'
, 'organizationalUnit'
)]
$ContainerClass = 'container'
,
# Êîíòðîëëåð äîìåíà Active Directory. Åñëè íå óêàçàí ÿâíî - èñïîëüçóåòñÿ ýìóëÿòîð ðîëè PDC â óêàçàííîì äîìåíå.
[Parameter(
Mandatory = $false
)]
[String]
$Server
,
# Ïåðåçàïèñûâàòü ëè êîíôèãóðàöèþ â ñëó÷àå å¸ íàëè÷èÿ
[Switch]
$Force
)
try {
if ( -not $Server ) {
$Server = ( Get-ADDomainController `
-Discover `
-DomainName $Domain `
-Writable `
-Service PrimaryDC `
).HostName;
};
$ADDomain = Get-ADDomain `
-Identity $Domain `
-Server $Server `
;
if (
(
Test-DomainUtilsPrintersConfiguration `
-Domain $Domain `
-Server $Server `
) `
-and -not $Force
) {
Write-Error `
-Message ( [String]::Format( $loc.ConfigExistsMessage, $ADDomain.DNSRoot ) ) `
-Category ResourceExists `
-CategoryActivity ( [String]::Format( $loc.ConfigInitialization, $ADDomain.DNSRoot ) ) `
-CategoryReason ( [String]::Format( $loc.ConfigExistsMessage, $ADDomain.DNSRoot ) ) `
-RecommendedAction ( [String]::Format( $loc.ConfigExistsRA, $ADDomain.DNSRoot ) ) `
;
} else {
Write-Verbose `
-Message ( [String]::Format( $loc.ConfigInitialization, $ADDomain.DNSRoot ) ) `
;
if ( -not ( Test-Path `
-LiteralPath "AD:$( ( $ConfigContainerDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' )" `
) ) {
$null = New-ADObject `
-Type 'container' `
-Path ( ( $ConfigContainerParentDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' ) `
-Name $ConfigContainerName `
-ProtectedFromAccidentalDeletion $false `
-PassThru `
-Verbose:$VerbosePreference `
-Server $Server `
;
};
if (
Test-DomainUtilsPrintersConfiguration `
-Domain $Domain `
-Server $Server `
) {
$ConfigADObject = Get-ADObject `
-Identity ( ( $ConfigObjectDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' ) `
-Server $Server `
-Properties 'msDS-ObjectReference', 'msDS-Settings' `
;
};
if ( -not $ConfigADObject ) {
$ConfigADObject = New-ADObject `
-Type 'msDS-App-Configuration' `
-Path ( ( $ConfigContainerDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' ) `
-Name $ConfigObjectName `
-ProtectedFromAccidentalDeletion $false `
-PassThru `
-Verbose:$VerbosePreference `
-Server $Server `
;
};
if ( $ConfigADObject ) {
$ConfigADObject.'msDS-ObjectReference' = `
( ( $DomainUtilsBase, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' ) `
;
$ConfigADObject.'msDS-Settings' = `
"ContainerClass=$ContainerClass" `
, "PrintQueuesContainerName=$( $loc.PrintQueuesContainerName )" `
, "PrintQueueContainerName=$( $loc.PrintQueueContainerName )" `
, "PrintQueueUsersGroup=$( $loc.PrintQueueUsersGroup )" `
, "PrintQueueUsersGroupAccountName=$( $loc.PrintQueueUsersGroupAccountName )" `
, "PrintQueueAdministratorsGroup=$( $loc.PrintQueueAdministratorsGroup )" `
, "PrintQueueAdministratorsGroupAccountName=$( $loc.PrintQueueAdministratorsGroupAccountName )" `
, "PrintQueueGPOName=$( $loc.PrintQueueGPOName )" `
;
Set-ADObject `
-Instance $ConfigADObject `
-Verbose:$VerbosePreference `
-Server $Server `
;
$null = Get-DomainUtilsPrintersConfiguration `
-Domain $Domain `
-Server $Server `
;
};
};
} catch {
Write-Error `
-ErrorRecord $_ `
;
};
}
Function Test-DomainUtilsPrintersConfiguration {
<#
.Synopsis
Ïðîâåðÿåì íàëè÷èå êîíôèãóðàöèè ìîäóëÿ äëÿ óêàçàííîãî äîìåíà.
.Description
Ïðîâåðÿåì íàëè÷èå êîíôèãóðàöèè ìîäóëÿ äëÿ óêàçàííîãî äîìåíà.
.Link
https://github.com/IT-Service/ITG.DomainUtils.Printers#Test-DomainUtilsPrintersConfiguration
.Example
Test-DomainUtilsPrintersConfiguration -Domain 'csm.nov.ru'
Ïðîâåðÿåì ñóùåñòâîâàíèå êîíôèãóðàöèè äëÿ äîìåíà csm.nov.ru.
#>
[CmdletBinding(
HelpUri = 'https://github.com/IT-Service/ITG.DomainUtils.Printers#Test-DomainUtilsPrintersConfiguration'
)]
param (
# äîìåí, äëÿ êîòîðîãî ïðîâåðÿåì íàëè÷èå êîíôèãóðàöèè ìîäóëÿ
[Parameter(
Mandatory = $false
)]
[String]
$Domain = ( ( Get-ADDomain ).DNSRoot )
,
# Êîíòðîëëåð äîìåíà Active Directory
[Parameter(
Mandatory = $false
)]
[String]
$Server
)
try {
if ( -not $Server ) {
$Server = ( Get-ADDomainController `
-Discover `
-DomainName $Domain `
).HostName;
};
$ADDomain = Get-ADDomain `
-Identity $Domain `
-Server $Server `
;
if ( $ConfigCache.ContainsKey( $ADDomain.DNSRoot ) ) { return $true; };
return ( Test-Path -Path "AD:$( ( $ConfigObjectDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' )" );
} catch {
Write-Error `
-ErrorRecord $_ `
;
};
}
New-Alias -Name Test-Config -Value Test-DomainUtilsPrintersConfiguration -Force;
Function Get-DomainUtilsPrintersConfiguration {
<#
.Synopsis
Ïîëó÷àåì îáúåêò, ñîäåðæàùèé êîíôèãóðàöèþ ìîäóëÿ äëÿ óêàçàííîãî äîìåíà.
.Description
Ïîëó÷àåì îáúåêò, ñîäåðæàùèé êîíôèãóðàöèþ ìîäóëÿ äëÿ óêàçàííîãî äîìåíà.
.Link
https://github.com/IT-Service/ITG.DomainUtils.Printers#Get-DomainUtilsPrintersConfiguration
.Example
( Get-DomainUtilsPrintersConfiguration -Domain 'csm.nov.ru' ).ContainerClass
Îïðåäåëÿåì êëàññ êîíòåéíåðîâ, èñïîëüçóåìûõ ìîäóëåì äëÿ äîìåíà csm.nov.ru.
#>
[CmdletBinding(
HelpUri = 'https://github.com/IT-Service/ITG.DomainUtils.Printers#Get-DomainUtilsPrintersConfiguration'
)]
param (
# äîìåí, äëÿ êîòîðîãî çàïðàøèâàåì êîíôèãóðàöèþ ìîäóëÿ
[Parameter(
Mandatory = $false
)]
[String]
$Domain = ( ( Get-ADDomain ).DNSRoot )
,
# Êîíòðîëëåð äîìåíà Active Directory
[Parameter(
Mandatory = $false
)]
[String]
$Server
,
# Èãíîðèðîâàòü êåø è ïðèíóäèòåëüíî ïåðå÷èòàòü êîíôèãóðàöèþ èç AD
[Switch]
$NoCache
)
try {
if ( -not $Server ) {
$Server = ( Get-ADDomainController `
-Discover `
-DomainName $Domain `
-Writable `
-Service PrimaryDC `
).HostName;
};
$ADDomain = Get-ADDomain `
-Identity $Domain `
-Server $Server `
;
if (
( -not $NoCache ) `
-and ( $ConfigCache.ContainsKey( $ADDomain.DNSRoot ) )
) {
return $ConfigCache.Item( $ADDomain.DNSRoot );
} else {
if (
-not (
Test-DomainUtilsPrintersConfiguration `
-Domain $Domain `
-Server $Server `
) `
) {
Write-Error `
-Message ( [String]::Format( $loc.ConfigDoesntExistsMessage, $ADDomain.DNSRoot ) ) `
-Category NotInstalled `
-CategoryActivity ( [String]::Format( $loc.ConfigRetriving, $ADDomain.DNSRoot ) ) `
-CategoryReason ( [String]::Format( $loc.ConfigDoesntExistsMessage, $ADDomain.DNSRoot ) ) `
-RecommendedAction ( [String]::Format( $loc.ConfigDoesntExistsRA, $ADDomain.DNSRoot ) ) `
;
} else {
$ConfigADObject = Get-ADObject `
-Identity ( ( $ConfigObjectDN, $ADDomain.DistinguishedName | ? { $_ } ) -join ',' ) `
-Server $Server `
-Properties 'msDS-ObjectReference', 'msDS-Settings' `
;
$ConfigCache.Item( $ADDomain.DNSRoot ) = @{
DomainUtilsBase = $ConfigADObject.'msDS-ObjectReference'[0];
};
$ConfigADObject.'msDS-Settings' `
| % {
if ( $_ -match '^(?<Param>\w+)=(?<Value>.*)$' ) {
$ConfigCache.Item( $ADDomain.DNSRoot ).( $Matches[ 'Param' ] ) = $Matches[ 'Value' ];
};
};
if ( $ConfigCache.Item( $ADDomain.DNSRoot ).ContainerClass -eq 'organizationalUnit' ) {
$ConfigCache.Item( $ADDomain.DNSRoot ).ContainerPathTemplate = 'OU={0}';
} else {
$ConfigCache.Item( $ADDomain.DNSRoot ).ContainerPathTemplate = 'CN={0}';
};
$ConfigCache.Item( $ADDomain.DNSRoot ).PrintQueuesContainer = "$(
[String]::Format(
$ConfigCache.Item( $ADDomain.DNSRoot ).ContainerPathTemplate
, $ConfigCache.Item( $ADDomain.DNSRoot ).PrintQueuesContainerName
)
),$( $ConfigCache.Item( $ADDomain.DNSRoot ).DomainUtilsBase )"
;
return $ConfigCache.Item( $ADDomain.DNSRoot );
};
};
} catch {
Write-Error `
-ErrorRecord $_ `
;
};
}
New-Alias -Name Get-Config -Value Get-DomainUtilsPrintersConfiguration -Force;