Skip to content

Instantly share code, notes, and snippets.

@AlexAsplund
Created May 29, 2019 22:57
Show Gist options
  • Save AlexAsplund/0aa11991ba93a94536616b564d0bf990 to your computer and use it in GitHub Desktop.
Save AlexAsplund/0aa11991ba93a94536616b564d0bf990 to your computer and use it in GitHub Desktop.
<#
.Synopsis
Kopierar en användare
.DESCRIPTION
Kopierar en användare med hjälp av en hashtable för mappning av attributer.
Hashtable ska vara enligt format @{>SourceUserAttribute> = <New-ADUser parametername>}
Exempel:
$Hashtable = @{
mail = 'EmailAddress'
GivenName = 'GivenName'
Name = 'Name'
DisplayName = 'DisplayName'
}
.EXAMPLE
$MappingHash = {
mail = 'EmailAddress'
GivenName = 'GivenName'
Name = 'Name'
DisplayName = 'DisplayName'
}
Copy-CustomADUser -Mapping $MappingHash -Identity jdoe -NewSamaccountName johndoe
.EXAMPLE
$MappingHash = {
mail = 'EmailAddress'
GivenName = 'GivenName'
Name = 'Name'
DisplayName = 'DisplayName'
}
Copy-CustomADUser -Mapping $MappingHash -Identity jdoe -NewSamaccountName johndoe -Path "OU=UserAccounts,DC=Contoso,DC=com" -AccountPassword 'ReallyStrongPassword' -CopyGroupMemberships
.EXAMPLE
$MappingHash = {
mail = 'EmailAddress'
GivenName = 'GivenName'
Name = 'Name'
DisplayName = 'DisplayName'
}
$TableOfUsers | Copy-CustomADUser -Mapping $MappingHash
#>
Function Copy-CustomADUser {
[cmdletbinding()]
param(
[parameter(mandatory,ValueFromPipelineByPropertyName)]
$Identity,
[parameter(ValueFromPipelineByPropertyName)]
$AccountPassword,
[parameter(mandatory,ValueFromPipelineByPropertyName)]
$NewSamaccountName,
[string]$Path,
[switch]$CopyGroupMemberships,
[parameter(mandatory,ValueFromPipelineByPropertyName)]
$NewUPN,
[string]$Server = (Get-ADDomainController).Name,
[parameter(mandatory)]
[HashTable]$Mapping,
[PSCredential]$Credential
)
Begin{
# Skapa splats för credential och server
$CredentialSplat = @{}
if($Null -ne $Credential){
$CredentialSplat = @{
Credential = $Credential
}
}
$ServerSplat = @{}
if($null -ne $Server){
$ServerSplat = @{Server = $Server}
}
}
Process{
$ADuser = Get-ADuser -Identity $Identity -Properties *
# Om AccountPassword inte är securestring - Konvertera till securestring
if($Null -eq $AccountPassword){
if($AccountPassword.GetType().Name -ne 'SecureString'){
$AccountPassword = $AccountPassword | ConvertTo-SecureString -AsPlainText -Force
}
}
# Skapa splat att fylla på för att skapa användaren i AD
$Splat = @{}
# Sätt värden för ny användare från parametrar
if($Null -eq $Path){
$Path = ($ADUser.DistinguishedName -split ',' | select -Skip 1) -join ','
}
$Splat.Add('samaccountname',$NewSamaccountName)
if($Null -ne $NewUPN){
$Splat.Add('UserPrincipalName',$NewUPN)
}
# Kopiera till splat enligt mapping hash
$Mapping.Keys | ? {$_ -notmatch 'samaccountname|userprincipalname'} | Foreach {
$Splat.Add($Mapping[$_],$ADUser.$_)
}
# Skapa användaren
Try{
New-ADUser @Splat @CredentialSplat @ServerSplat -ErrorAction stop
[PSCustomObject]@{
OldSamAccountName = $ADUser.Samaccountname
NewSamaccountname = $Splat.SamAccountname
}
}
Catch {
Write-Error $_ -ErrorAction $ErrorActionPreference
Write-Error 'Failed to creat AD user' -ErrorAction $ErrorActionPreference
}
if($CopyGroupMemberships){
$ADUser.MemberOf | Foreach (
Add-ADGroupMember -Identity $_ -Members $ADUser.SamAccountName @CredentialSplat @ServerSplat
)
}
}
End{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment