0 votes

I am using this script modified for my testing. Import-Module Adaxes

$csvFilePath = "D:\TestFeed\ImportNewUsers.csv" # Path to pick up feed file
$userIdColumn = "Employee Number" # TODO: modify me
$userIdProperty = "EmployeeNumber" # TODO: modify me
$customColumnNames = @{
    "Employee ID/Contingent Worker ID" = "EmployeeID";
    "First Name" = "GivenName";
    "Last Name" = "sn";
    "Middle Initial" = "Initials"
    "Primary Job Title" = "title";
    "Primary Position Cost Sentor" = "Department";
    "Future Term Date" = "AccountExpirationDate";
    "Subsidiary" = "Division";
    "Entity" = "Company";
    "Location" = "City";
    "Suffix" = "PersonalTitle"
} # Set Manager as an AD Object
$aDObjectProperties = @("Manager") # Set Manager as an AD Object
$ouDN = "OU=Staging,DC=ADXTEST,DC=BENEFIS,DC=ORG" # OU to create users in

# E-mail settings
#$to = "heathhaywood@benefis.org" # TODO: modify me
#$subject = "Import New Users Report" # TODO: modify me
#$reportHeader = "<h2>Import report</h2>"
#$reportFooter = "<hr /><p><i>Please See Attached Report"

#$domainName = $Context.GetObjectDomain($ouDN)
#$importedUsers  = Import-Csv -Path $csvFilePath

$rootDSE = $Context.BindToObject("Adaxes://RootDSE")
$userFound = New-Object "System.Text.StringBuilder"
foreach ($userFromCSV in $importedUsers)
{
    $userObject = @{}
    $accountPassword = $NULL

    foreach ($property in $userFromCSV.PSObject.Properties)
    {
        $columnName = $property.Name
        $value = $property.Value

        if ($customColumnNames.ContainsKey($columnName))
        {
            $propertyName = $customColumnNames[$columnName]
        }
        else
        {
            $propertyName = $columnName
        }

        if ([System.String]::IsNullOrEmpty($value))
        {
            continue
        }

        # Parse special columns
        if ($columnName -ieq $userIdColumn)
        {
            $propertyName = $userIdProperty
        }
        elseif ($aDObjectProperties -icontains $columnName)
        {
            $aDObject = Get-AdmObject -Filter {(Name -eq $value) -or (DisplayName -eq $value) -or (distinguishedName -eq $value)} `
                -AdaxesService localhost -ErrorAction SilentlyContinue -Server $domainName

            if ($aDObject -is [System.Array])
            {
                $Context.LogMessage("Found more than one object with identity '$value'.", "Warning")
                continue
            }

            if ($aDObject -eq $NULL)
            {
                $Context.LogMessage("Could not locate object with identity '$value'.", "Warning")
                continue
            }

            $value = $aDObject.DistinguishedName
        }

        if ($value -ieq "True" -or $value -ieq "False")
        {
            $value = [System.Boolean]::Parse($value)
        }

        $userObject.Add($propertyName, $value)
    }
    # Build sAMAccountName
    $sAMAccountName = $userObject.sn.substring(0,4) + $userObject.GivenName.substring(0,3) + $userObject.initials

    # Check whether the user exists
    $valueForSearch = $userObject.$userIdProperty
    $userExists = Get-AdmUser -LdapFilter "($userIdProperty=$valueForSearch)" `
        -AdaxesService localhost -ErrorAction SilentlyContinue -Server $domainName

    if ($NULL -eq $userExists)
    {
        # Build user name
        $displayName = $userObject.GivenName + " " + $userObject.SN
        $parameters = @{
            "Path" = $ouDN
            "Name" = $displayName;
            "SamAccountName" = $SAMAccountName.ToLower()
            "Server" = $domainName;
            "AdaxesService" = "localhost"
            "Enabled" = $True
            "OtherAttributes" = $userObject
            "ErrorAction" = "Stop"
        }

        # Generate password
        $userAdsPathObj = New-Object Softerra.Adaxes.Adsi.AdsPath "Adaxes://$ouDN"
        $rdnValue = [Softerra.Adaxes.Ldap.Rdn]::EscapeAttributeValue($displayName)
        $userAdsPathObj.CreateChildPath("CN=$rdnValue")
        $passwordString = $rootDSE.GeneratePasswordForNewUser($userAdsPathObj)
        $passwordSecureString = ConvertTo-SecureString -AsPlainText $passwordString -Force
        $parameters.Add("AccountPassword", $passwordSecureString)

        # Create a new user account
        try
        {
            New-AdmUser @parameters
        }
        catch
        {
            $Context.LogMessage("An error occurred when creating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
        }
    }
    else
    {
        $userFound.Append("<li>$valueForSearch</li>")
    }
}

if ($userFound.Length -eq 0)
{
    return
}

# Build report
#$html = New-Object "System.Text.StringBuilder"
#$html.Append($reportHeader)
#$html.Append("<b>The following users were found in Active Directory:</b>")
#$html.Append("<ol>")
#$html.Append($userFound.ToString())
#$html.Append("</ol>")
#$html.Append($reportFooter)

#$Context.SendMail($to, $subject, $NULL, $html.ToString())

In this script I use # Build sAMAccountName $sAMAccountName = $userObject.sn.substring(0,4) + $userObject.GivenName.substring(0,3) + $userObject.initials This sets the Sam Name to first 4 of last name first three of first name and middle initial. I would like to add a 2 for now if the sam isn't unique.

by (420 points)

1 Answer

0 votes
by (237k points)

Hello,

There is no need to do it in the import script itself. You can use a business rule triggering Before creating a user. For details, have a look at the following tutorial: https://www.adaxes.com/tutorials_SimplifyingDataEntry_ValidateModifyUserInputWithScript.htm.

Related questions

0 votes
1 answer

Hello, is it possible to add computers to the basket, imported from a csv file? We get a list from our client team to disable computer accounts in bulk. regards Helmut

asked Feb 22, 2021 by a423385 (510 points)
0 votes
0 answers

We are in hybrid mode with 365. All the accounts we create have to made with a .com instead of .local. How can I make that change in adaxes? Or is this some default I need to change in AD instead?

asked Apr 11, 2022 by LEGIT1 (150 points)
0 votes
1 answer

Have a csv file of users that I need to import into Adaxes. I had initially found an article for this, but upon going today, it gave me an error (looks like it was deleted). Thank you

asked Nov 19, 2022 by wangl (20 points)
0 votes
1 answer

Hallo Everyone I'm attempting to import a CSV list to Update Current users. I Used the Script 2 from Repostitory https://www.adaxes.com/script-repository/ ... "employeeNumber") # TODO: modify me Test file: sAMAccountName,employeeNumber, Peter.Muster,AB052

asked Jul 13, 2021 by Sandberg94 (340 points)
0 votes
1 answer

So I need to export a list of all user's Line URI's to a CSV file. Running Adaxes 2021 Version 3.14.18804.0 (64 bit) and Teams Powershell 4.1.0 ... a Microsoft 365 account } finally { # Close the connection and release resources Disconnect-MicrosoftTeams }

asked Aug 4, 2022 by TheLexicon (180 points)
2,937 questions
2,654 answers
6,870 comments
160,328 users