-
Notifications
You must be signed in to change notification settings - Fork 141
/
MSFT_ADUser.psm1
2197 lines (1822 loc) · 67.1 KB
/
MSFT_ADUser.psm1
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingUserNameAndPassWordParams', '')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "PasswordAuthentication")]
param()
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'
$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'ActiveDirectoryDsc.Common'
Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath 'ActiveDirectoryDsc.Common.psm1')
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_ADUser'
# Create a property map that maps the DSC resource parameters to the
# Active Directory user attributes.
$adPropertyMap = @(
@{
Parameter = 'CommonName'
ADProperty = 'cn'
}
@{
Parameter = 'UserPrincipalName'
}
@{
Parameter = 'DisplayName'
}
@{
Parameter = 'Path'
ADProperty = 'distinguishedName'
}
@{
Parameter = 'GivenName'
}
@{
Parameter = 'Initials'
}
@{
Parameter = 'Surname'
ADProperty = 'sn'
}
@{
Parameter = 'Description'
}
@{
Parameter = 'StreetAddress'
}
@{
Parameter = 'POBox'
}
@{
Parameter = 'City'
ADProperty = 'l'
}
@{
Parameter = 'State'
ADProperty = 'st'
}
@{
Parameter = 'PostalCode'
}
@{
Parameter = 'Country'
ADProperty = 'c'
}
@{
Parameter = 'Department'
}
@{
Parameter = 'Division'
}
@{
Parameter = 'Company'
}
@{
Parameter = 'Office'
ADProperty = 'physicalDeliveryOfficeName'
}
@{
Parameter = 'JobTitle'
ADProperty = 'title'
}
@{
Parameter = 'EmailAddress'
ADProperty = 'mail'
}
@{
Parameter = 'EmployeeID'
}
@{
Parameter = 'EmployeeNumber'
}
@{
Parameter = 'HomeDirectory'
}
@{
Parameter = 'HomeDrive'
}
@{
Parameter = 'HomePage'
ADProperty = 'wWWHomePage'
}
@{
Parameter = 'ProfilePath'
}
@{
Parameter = 'LogonScript'
ADProperty = 'scriptPath'
}
@{
Parameter = 'Notes'
ADProperty = 'info'
}
@{
Parameter = 'OfficePhone'
ADProperty = 'telephoneNumber'
}
@{
Parameter = 'MobilePhone'
ADProperty = 'mobile'
}
@{
Parameter = 'Fax'
ADProperty = 'facsimileTelephoneNumber'
}
@{
Parameter = 'Pager'
}
@{
Parameter = 'IPPhone'
}
@{
Parameter = 'HomePhone'
}
@{
Parameter = 'Enabled'
}
@{
Parameter = 'Manager'
}
@{
Parameter = 'Organization'
}
@{
Parameter = 'OtherName'
}
@{
Parameter = 'ThumbnailPhoto'
ADProperty = 'thumbnailPhoto'
}
@{
Parameter = 'PasswordNeverExpires'
UseCmdletParameter = $true
}
@{
Parameter = 'CannotChangePassword'
UseCmdletParameter = $true
}
@{
Parameter = 'ChangePasswordAtLogon'
UseCmdletParameter = $true
ADProperty = 'pwdLastSet'
}
@{
Parameter = 'TrustedForDelegation'
UseCmdletParameter = $true
}
@{
Parameter = 'AccountNotDelegated'
UseCmdletParameter = $true
}
@{
Parameter = 'AllowReversiblePasswordEncryption'
UseCmdletParameter = $true
}
@{
Parameter = 'CompoundIdentitySupported'
UseCmdletParameter = $true
}
@{
Parameter = 'PasswordNotRequired'
UseCmdletParameter = $true
}
@{
Parameter = 'SmartcardLogonRequired'
UseCmdletParameter = $true
}
@{
Parameter = 'ServicePrincipalNames'
ADProperty = 'ServicePrincipalName'
Type = 'Array'
}
@{
Parameter = 'ProxyAddresses'
Type = 'Array'
}
)
<#
.SYNOPSIS
Returns the current state of the Active Directory User
.PARAMETER DomainName
Name of the domain where the user account is located (only used if
password is managed).
.PARAMETER UserName
Specifies the Security Account Manager (SAM) account name of the user
(ldapDisplayName 'sAMAccountName').
.PARAMETER DomainController
Specifies the Active Directory Domain Services instance to use to
perform the task.
.PARAMETER Credential
Specifies the user account credentials to use to perform this task.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.String]
$UserName,
[Parameter()]
[ValidateNotNull()]
[System.String]
$DomainController,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
Assert-Module -ModuleName 'ActiveDirectory'
try
{
$adCommonParameters = Get-ADCommonParameters @PSBoundParameters
$adProperties = @()
# Create an array of the AD propertie names to retrieve from the property map
foreach ($property in $adPropertyMap)
{
if ($property.ADProperty)
{
$adProperties += $property.ADProperty
}
else
{
$adProperties += $property.Parameter
}
}
Write-Verbose -Message ($script:localizedData.RetrievingADUser -f $UserName, $DomainName)
$adUser = Get-ADUser @adCommonParameters -Properties $adProperties
Write-Verbose -Message ($script:localizedData.ADUserIsPresent -f $UserName, $DomainName)
$ensure = 'Present'
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Verbose -Message ($script:localizedData.ADUserNotPresent -f $UserName, $DomainName)
$ensure = 'Absent'
}
catch
{
$errorMessage = $script:localizedData.RetrievingADUserError -f $UserName, $DomainName
New-InvalidOperationException -Message $errorMessage -ErrorRecord $_
}
$targetResource = @{
DomainName = $DomainName
UserName = $UserName
Password = $null
DistinguishedName = $adUser.DistinguishedName; # Read-only property
Ensure = $ensure
DomainController = $DomainController
}
# Retrieve each property from the ADPropertyMap and add to the hashtable
foreach ($property in $adPropertyMap)
{
$parameter = $property.Parameter
if ($parameter -eq 'Path')
{
# The path returned is not the parent container
if (-not [System.String]::IsNullOrEmpty($adUser.DistinguishedName))
{
$targetResource[$parameter] = Get-ADObjectParentDN -DN $adUser.DistinguishedName
}
}
elseif ($parameter -eq 'ChangePasswordAtLogon')
{
if ($adUser.pwdlastset -eq 0)
{
$targetResource[$parameter] = $true
}
else
{
$targetResource[$parameter] = $false
}
}
elseif ($parameter -eq 'ThumbnailPhoto')
{
if ([System.String]::IsNullOrEmpty($adUser.$parameter))
{
$targetResource[$parameter] = $null
$targetResource['ThumbnailPhotoHash'] = $null
}
else
{
$targetResource[$parameter] = [System.Convert]::ToBase64String($adUser.$parameter)
$targetResource['ThumbnailPhotoHash'] = Get-MD5HashString -Bytes $adUser.$parameter
}
}
elseif ($property.ADProperty)
{
# The AD property name is different to the function parameter to use this
$aDProperty = $property.ADProperty
if ($property.Type -eq 'Array')
{
$targetResource[$parameter] = [System.String[]] $adUser.$aDProperty
}
else
{
$targetResource[$parameter] = $adUser.$aDProperty
}
}
else
{
# The AD property name matches the function parameter
if ($property.Type -eq 'Array')
{
$targetResource[$Parameter] = [System.String[]] $adUser.$parameter
}
else
{
$targetResource[$Parameter] = $adUser.$parameter
}
}
}
return $targetResource
} # end function Get-TargetResource
<#
.SYNOPSIS
Tests the state of the Active Directory user account.
.PARAMETER DomainName
Name of the domain where the user account is located (only used if
password is managed).
.PARAMETER UserName
Specifies the Security Account Manager (SAM) account name of the user
(ldapDisplayName 'sAMAccountName').
.PARAMETER Password
Specifies a new password value for the account.
.PARAMETER Ensure
Specifies whether the user account should be present or absent. Default
value is 'Present'.
.PARAMETER CommonName
Specifies the common name assigned to the user account (ldapDisplayName
'cn'). If not specified the default value will be the same value
provided in parameter UserName.
.PARAMETER UserPrincipalName
Specifies the User Principal Name (UPN) assigned to the user account
(ldapDisplayName 'userPrincipalName').
.PARAMETER DisplayName
Specifies the display name of the object (ldapDisplayName
'displayName').
.PARAMETER Path
Specifies the X.500 path of the Organizational Unit (OU) or container
where the new object is created.
.PARAMETER GivenName
Specifies the user's given name (ldapDisplayName 'givenName').
.PARAMETER Initials
Specifies the initials that represent part of a user's name
(ldapDisplayName 'initials').
.PARAMETER Surname
Specifies the user's last name or surname (ldapDisplayName 'sn').
.PARAMETER Description
Specifies a description of the object (ldapDisplayName 'description').
.PARAMETER StreetAddress
Specifies the user's street address (ldapDisplayName 'streetAddress').
.PARAMETER POBox
Specifies the user's post office box number (ldapDisplayName
'postOfficeBox').
.PARAMETER City
Specifies the user's town or city (ldapDisplayName 'l').
.PARAMETER State
Specifies the user's or Organizational Unit's state or province
(ldapDisplayName 'st').
.PARAMETER PostalCode
Specifies the user's postal code or zip code (ldapDisplayName
'postalCode').
.PARAMETER Country
Specifies the country or region code for the user's language of choice
(ldapDisplayName 'c').
.PARAMETER Department
Specifies the user's department (ldapDisplayName 'department').
.PARAMETER Division
Specifies the user's division (ldapDisplayName 'division').
.PARAMETER Company
Specifies the user's company (ldapDisplayName 'company').
.PARAMETER Office
Specifies the location of the user's office or place of business
(ldapDisplayName 'physicalDeliveryOfficeName').
.PARAMETER JobTitle
Specifies the user's title (ldapDisplayName 'title').
.PARAMETER EmailAddress
Specifies the user's e-mail address (ldapDisplayName 'mail').
.PARAMETER EmployeeID
Specifies the user's employee ID (ldapDisplayName 'employeeID').
.PARAMETER EmployeeNumber
Specifies the user's employee number (ldapDisplayName 'employeeNumber').
.PARAMETER HomeDirectory
Specifies a user's home directory path (ldapDisplayName
'homeDirectory').
.PARAMETER HomeDrive
Specifies a drive that is associated with the UNC path defined by the
HomeDirectory property (ldapDisplayName 'homeDrive').
.PARAMETER HomePage
Specifies the URL of the home page of the object (ldapDisplayName
'wWWHomePage').
.PARAMETER ProfilePath
Specifies a path to the user's profile (ldapDisplayName 'profilePath').
.PARAMETER LogonScript
Specifies a path to the user's log on script (ldapDisplayName
'scriptPath').
.PARAMETER Notes
Specifies the notes attached to the user's accoutn (ldapDisplayName
'info').
.PARAMETER OfficePhone
Specifies the user's office telephone number (ldapDisplayName
'telephoneNumber').
.PARAMETER MobilePhone
Specifies the user's mobile phone number (ldapDisplayName 'mobile').
.PARAMETER Fax
Specifies the user's fax phone number (ldapDisplayName
'facsimileTelephoneNumber').
.PARAMETER HomePhone
Specifies the user's home telephone number (ldapDisplayName
'homePhone').
.PARAMETER Pager
Specifies the user's pager number (ldapDisplayName 'pager').
.PARAMETER IPPhone
Specifies the user's IP telephony phone number (ldapDisplayName
'ipPhone').
.PARAMETER Manager
Specifies the user's manager specified as a Distinguished Name
(ldapDisplayName 'manager').
.PARAMETER LogonWorkstations
Specifies the computers that the user can access. To specify more than
one computer, create a single comma-separated list. You can identify a
computer by using the Security Account Manager (SAM) account name
(sAMAccountName) or the DNS host name of the computer. The SAM account
name is the same as the NetBIOS name of the computer. The LDAP display
name (ldapDisplayName) for this property is userWorkStations.
.PARAMETER Organization
Specifies the user's organization. This parameter sets the Organization
property of a user object. The LDAP display name (ldapDisplayName) of
this property is 'o'.
.PARAMETER OtherName
Specifies a name in addition to a user's given name and surname, such as
the user's middle name. This parameter sets the OtherName property of a
user object. The LDAP display name (ldapDisplayName) of this property is
'middleName'.
.PARAMETER Enabled
Specifies if the account is enabled. Default value is $true.
.PARAMETER CannotChangePassword
Specifies whether the account password can be changed.
.PARAMETER ChangePasswordAtLogon
Specifies whether the account password must be changed during the next
logon attempt. This will only be enabled when the user is initially
created. This parameter cannot be set to $true if the parameter
PasswordNeverExpires is also set to $true.
.PARAMETER PasswordNeverExpires
Specifies whether the password of an account can expire.
.PARAMETER TrustedForDelegation
Specifies whether an account is trusted for Kerberos delegation. Default
value is $false.
.PARAMETER AccountNotDelegated
Indicates whether the security context of the user is delegated to a
service. When this parameter is set to true, the security context of
the account is not delegated to a service even when the service account
is set as trusted for Kerberos delegation. This parameter sets the
AccountNotDelegated property for an Active Directory account. This
parameter also sets the ADS_UF_NOT_DELEGATED flag of the Active
Directory User Account Control (UAC) attribute.
.PARAMETER AllowReversiblePasswordEncryption
Indicates whether reversible password encryption is allowed for the
account. This parameter sets the AllowReversiblePasswordEncryption
property of the account. This parameter also sets the
ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED flag of the Active Directory User
Account Control (UAC) attribute.
.PARAMETER CompoundIdentitySupported
Specifies whether an account supports Kerberos service tickets which
includes the authorization data for the user's device. This value sets
the compound identity supported flag of the Active Directory
msDS-SupportedEncryptionTypes attribute.
.PARAMETER PasswordNotRequired
Specifies whether the account requires a password. A password is not
required for a new account. This parameter sets the PasswordNotRequired
property of an account object.
.PARAMETER SmartcardLogonRequired
Specifies whether a smart card is required to logon. This parameter sets
the SmartCardLoginRequired property for a user object. This parameter
also sets the ADS_UF_SMARTCARD_REQUIRED flag of the Active Directory
User Account Control attribute.
.PARAMETER DomainController
Specifies the Active Directory Domain Services instance to use to
perform the task.
.PARAMETER Credential
Specifies the user account credentials to use to perform this task.
.PARAMETER PasswordAuthentication
Specifies the authentication context type used when testing passwords.
Default value is 'Default'.
.PARAMETER PasswordNeverResets
Specifies whether existing user's password should be reset. Default
value is $false.
.PARAMETER RestoreFromRecycleBin
Try to restore the user object from the recycle bin before creating a
new one.
.PARAMETER ServicePrincipalNames
Specifies the service principal names for the user account.
.PARAMETER ProxyAddresses
Specifies the proxy addresses for the user account.
.PARAMETER ThumbnailPhoto
Specifies the thumbnail photo to be used for the user object. Can be set
either to a path pointing to a .jpg-file, or to a Base64-encoded jpeg
image. If set to an empty string ('') the current thumbnail photo will be
removed. The property ThumbnailPhoto will always return the image as a
Base64-encoded string even if the configuration specified a file path.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.String]
$UserName,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Password,
[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[ValidateNotNull()]
[System.String]
$CommonName,
[Parameter()]
[ValidateNotNull()]
[System.String]
$UserPrincipalName,
[Parameter()]
[ValidateNotNull()]
[System.String]
$DisplayName,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Path,
[Parameter()]
[ValidateNotNull()]
[System.String]
$GivenName,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Initials,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Surname,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Description,
[Parameter()]
[ValidateNotNull()]
[System.String]
$StreetAddress,
[Parameter()]
[ValidateNotNull()]
[System.String]
$POBox,
[Parameter()]
[ValidateNotNull()]
[System.String]
$City,
[Parameter()]
[ValidateNotNull()]
[System.String]
$State,
[Parameter()]
[ValidateNotNull()]
[System.String]
$PostalCode,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Country,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Department,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Division,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Company,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Office,
[Parameter()]
[ValidateNotNull()]
[System.String]
$JobTitle,
[Parameter()]
[ValidateNotNull()]
[System.String]
$EmailAddress,
[Parameter()]
[ValidateNotNull()]
[System.String]
$EmployeeID,
[Parameter()]
[ValidateNotNull()]
[System.String]
$EmployeeNumber,
[Parameter()]
[ValidateNotNull()]
[System.String]
$HomeDirectory,
[Parameter()]
[ValidateNotNull()]
[System.String]
$HomeDrive,
[Parameter()]
[ValidateNotNull()]
[System.String]
$HomePage,
[Parameter()]
[ValidateNotNull()]
[System.String]
$ProfilePath,
[Parameter()]
[ValidateNotNull()]
[System.String]
$LogonScript,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Notes,
[Parameter()]
[ValidateNotNull()]
[System.String]
$OfficePhone,
[Parameter()]
[ValidateNotNull()]
[System.String]
$MobilePhone,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Fax,
[Parameter()]
[ValidateNotNull()]
[System.String]
$HomePhone,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Pager,
[Parameter()]
[ValidateNotNull()]
[System.String]
$IPPhone,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Manager,
[Parameter()]
[ValidateNotNull()]
[System.String]
$LogonWorkstations,
[Parameter()]
[ValidateNotNull()]
[System.String]
$Organization,
[Parameter()]
[ValidateNotNull()]
[System.String]
$OtherName,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$Enabled = $true,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$CannotChangePassword,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$ChangePasswordAtLogon,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$PasswordNeverExpires,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$TrustedForDelegation,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$AccountNotDelegated,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$AllowReversiblePasswordEncryption,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$CompoundIdentitySupported,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$PasswordNotRequired,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$SmartcardLogonRequired,
[Parameter()]
[ValidateNotNull()]
[System.String]
$DomainController,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential,
[Parameter()]
[ValidateSet('Default', 'Negotiate')]
[System.String]
$PasswordAuthentication = 'Default',
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$PasswordNeverResets = $false,
[Parameter()]
[ValidateNotNull()]
[System.Boolean]
$RestoreFromRecycleBin,
[Parameter()]
[ValidateNotNull()]
[System.String[]]
$ServicePrincipalNames,
[Parameter()]
[ValidateNotNull()]
[System.String[]]
$ProxyAddresses,
[Parameter()]
[System.String]
$ThumbnailPhoto
)
<#
This is a workaround to make the resource able to enter debug mode.
For more information see issue https://github.com/PowerShell/ActiveDirectoryDsc/issues/427.
#>
if (-not $PSBoundParameters.ContainsKey('CommonName'))
{
$CommonName = $UserName
}
Assert-Parameters @PSBoundParameters
$getParameters = @{ }
$getParameters = @{
DomainName = $DomainName
UserName = $UserName
}
if ($PSBoundParameters.ContainsKey('DomainController'))
{
$getParameters['DomainController'] = $DomainController
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getParameters['Credential'] = $Credential
}
$targetResource = Get-TargetResource @getParameters
$isCompliant = $true
if ($Ensure -eq 'Absent')
{
if ($targetResource.Ensure -eq 'Present')
{
Write-Verbose -Message ($script:localizedData.ADUserNotDesiredPropertyState -f 'Ensure', $PSBoundParameters.Ensure, $targetResource.Ensure)
$isCompliant = $false
}
}
else
{
# Add common name, Ensure and enabled as they may not be explicitly passed and we want to enumerate them
$PSBoundParameters['Ensure'] = $Ensure
$PSBoundParameters['Enabled'] = $Enabled
foreach ($parameter in $PSBoundParameters.Keys)
{
if ($parameter -eq 'Password' -and $PasswordNeverResets -eq $false)
{
$testPasswordParams = @{
Username = $UserName
Password = $Password
DomainName = $DomainName
PasswordAuthentication = $PasswordAuthentication
}
if ($Credential)
{
$testPasswordParams['Credential'] = $Credential
}
if (-not (Test-Password @testPasswordParams))
{
Write-Verbose -Message ($script:localizedData.ADUserNotDesiredPropertyState -f 'Password', '<Password>', '<Password>')
$isCompliant = $false
}
}
elseif ($parameter -eq 'ChangePasswordAtLogon' -and $PSBoundParameters.$parameter -eq $true -and $targetResource.Ensure -eq 'Present')
{
# Only process the ChangePasswordAtLogon = $true parameter during new user creation
continue
}
elseif ($parameter -eq 'ThumbnailPhoto')
{
<#
Compare thumbnail hash, if they are the same the function
Compare-ThumbnailPhoto returns $null if they are the same.
#>
$compareThumbnailPhotoResult = Compare-ThumbnailPhoto -DesiredThumbnailPhoto $ThumbnailPhoto -CurrentThumbnailPhotoHash $targetResource.ThumbnailPhotoHash
if ($compareThumbnailPhotoResult)
{
Write-Verbose -Message (
$script:localizedData.ADUserNotDesiredPropertyState `
-f $parameter, $compareThumbnailPhotoResult.DesiredThumbnailPhotoHash, $compareThumbnailPhotoResult.CurrentThumbnailPhotoHash
)
$isCompliant = $false
}