There are a few things with Set-ADSIUser that I addressed or improved.
- Broken use of -Credential
- Implemented New-ADSIDirectoryEntry
- Changed -DomainDN to -DomainName to fall in line with the rest of the module
- Added DomainDN alias to keep backwards compatibility
- Added UserPrincipalName to the identity search
Credential Issue
When using Set-ADSIUser -Credential the command was building a Directory Entry, but it did not actually use it. Below you can see the snippet of code where it builds $Cred, but then sets the SearchRoot to $DomainDN instead.
IF ($PSBoundParameters['Credential'])
{
$Cred = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDN, $($Credential.UserName), $($Credential.GetNetworkCredential().password)
$Search.SearchRoot = $DomainDN
}
I have a Pull Request coming that resolves this by implementing the New-ADSIDirectoryEntry. I implemented it the same way as I did in #40 so the Params are not ignored.
DomainName
This command did not follow the same format as the other functions in the module. Since I was going to be implementing New-ADSIDirectoryEntry, I updated the DomainDN param to DomainName so it was the same as the others. To keep backwards compatibility I added DomainDN to the aliases.
UserPrincipalName
I went ahead and added this to the Identity search. This could likely be improved more or maybe utilize some of the other commands in the module. I just stopped with upn because I didn't have the time to look into it further.
Notes
Also, a lot of the default param values are ignored throughout the module. When using the code below in the Begin block, the default values are being ignored since they don't show up in the PSBoundParameters. This means they never get added to the context splat.
IF ($PSBoundParameters['Credential']) { $ContextSplatting.Credential = $Credential }
IF ($PSBoundParameters['DomainName']) { $ContextSplatting.DomainName = $DomainName }
This doesn't seem to impact anything since you have the same defaults set in the commands that use the splat, but just wanted to point it out. You could probably remove all of those default values, but I left them alone since I wasn't sure if you had something planned for them or anything like that.