Search code examples
powershellactive-directorypowershell-7.0powershell-5.1parameter-splatting

I am unable to get splatting to work with New-ADUser using given and created attributes


I am processing an array of AD User data pulled from one domain to recreate in another. I have created a hash table linking the New-ADUser parameters with the user data imported from a CSV (populated from the domain I intend to recreate). When I call New-ADUser with the hash table, the user is not created and there are no error.

Here is the hash table:

$NewUserAttr = @{
    'Name'              = $ADUser.UsersName
    'SamAccountName'    = $ADUser.UsersSamAccountName
    'Company'           = $ADUser.UsersCompany
    'Department'        = $ADUser.UsersDepartment
    'DisplayName'       = $ADUser.UsersDisplayName
    'EmailAddress'      = $ADUser.UsersMail
    'EmployeeID'        = $ADUser.UsersEmployeeID
    'Enabled'           = $UsersEnabled
    'GivenName'         = $ADUser.UsersGivenName
    'Initials'          = $ADUser.UsersInitials
    'Manager'           = $ADUser.Manager
    'MobilePhone'       = $ADUser.UsersMobileNum
    'OfficePhone'       = $ADUser.UsersTelephoneNumber
    'PostalCode'        = $ADUser.UsersPostalCode
    'State'             = $ADUser.UsersST
    'StreetAddress'     = $ADUser.UsersStreetAddress
    'Surname'           = $ADUser.UsersSN
    'Title'             = $ADUser.UsersTitle
    'userPrincipalname' = $ADUser.UsersUPN
    'Path'              = $ParentOU
    'Server'            = $TargetDomain

    'OtherAttr' = @{
        'c'                  = $ADUser.Usersc
        'GIDNumber'          = $ADUser.UsersGIDNumber
        'l'                  = $ADUser.UsersL
        'LoginShell'         = $ADUser.UsersLoginShell
        'msSFU30Name'        = $ADUser.UsersMsSFU30Name
        'msSFU30NisDomain'   = $ADUser.UsersMsSFU30NisDomain
        'PhysicalDeliveryOfficeName' = $ADUser.UsersPhysicalDeliveryOfficeName
        'SSN'                = $ADUser.UsersSSN
        'Uid'                = $ADUser.UsersUid
        'uidNumber'          = $ADUser.UsersUidNum
        'unixHomeDirectory'  = $ADUser.UsersUHD
    }
}

PS > New-ADUser @NewUserAttr

I have reduced the NewUserAttr to Name, SamAccountName, Path, and Server and that did create the user, but that is far less parameters than what I need.


Solution

  • Continuing from my comments:

    To avoid empty fields from being assembled in the attributes Hashtable to use for splatting, you could create two lookup tables in which you  map the CSV header names with the actual AD user attribute names.
    Something like this:
    
        # create two mapping lookup Hashtables for 'normal' attributes and one for the OtherAttributes
        # format is: PropertyNameFromCsv = PropertyNameForActiveDirectory
        $attribMap = @{
            UsersName           = 'Name'
            UsersSamAccountName = 'SamAccountName'
            UsersCompany        = 'Company'
            Usersc              = 'Country'
            Manager             = 'Manager'
            # etc.
        }
    
        $otherMap = @{
            UsersGIDNumber        = 'GIDNumber'
            UsersLoginShell       = 'LoginShell'
            UsersmsSFU30Name      = 'MsSFU30Name'
            UsersmsSFU30NisDomain = 'MsSFU30NisDomain'
            # etc.
        }
    

    Next, import the CSV and loop over each entry:

    $csv = Import-Csv -Path 'X:\your_importFile.csv'
    
    foreach ($item in $csv) {
        # two empty Hashtables for splatting
        $NewUserAttr = @{}
        $OtherAttr   = @{}
        # pre fill the default attributes you need for all users
        $NewUserAttr['Enabled'] = $UsersEnabled
        $NewUserAttr['Server']  = $TargetDomain
        $NewUserAttr['Path']    = $ParentOU
    
        # loop over the properties for each item in the CSV and only store 
        # the ones that are not empty and for which you can find a mapping
        $item.PsObject.Properties | ForEach-Object {
            if (![string]::IsNullOrWhiteSpace($_.Value)) { 
                if ($attribMap.Contains($_.Name))    { $NewUserAttr[$attribMap[$_.Name]] = $_.Value }
                elseif ($otherMap.Contains($_.Name)) { $OtherAttr[$otherMap[$_.Name]] = $_.Value }
            }
        }
        # join the hashtables together if we have OtherAttributes
        if ($OtherAttr.Count) { $NewUserAttr['OtherAttributes'] = $OtherAttr }
    
        # now try and create the new user
        try {
            New-ADUser @NewUserAttr -ErrorAction Stop
        }
        catch {
            Write-Warning "Error creating user $($NewUserAttr.Name): $($_.Exception.Message)"
        }
    }