Im creating active directory users from the file usersFile2.csv
The users gets added.
Right account information is added, for example firstname, lastname, etc...
The PROBLEM.
If a user with the same sAMAccountName exists I want the script to add a number to sAMAccountName..
Get-ADuser part is where I need to edit...
# Import active directory module
Import-Module activedirectory
#Load data from file.csv into $ADUsers variable.
$ADUsers = Import-csv C:\Users\Administrator\Downloads\Script5\usersFile2.csv
#Go through each row that has user data in the CSV we just imported.
foreach ($User in $ADUsers)
{
$Username = $User.sAMAccountName
$Password = $User.password
$Firstname = $User.givenName
$Lastname = $User.sn
#To see if the user already exists in AD.
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#Tell what happened.
Write-Output "$Username already existed."
}
else
{
#If the user does Not exist, then create the account with the attributes.
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "[email protected]" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Firstname $Lastname" `
-EmailAddress "[email protected]" `
-Description $user.Description `
-Department $user.Department `
-Office $user.Office `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
-Path 'OU=dd1, OU=My_users, DC=internal, DC=sonic, DC=com' `
-PasswordNeverExpires $True
#Tell what happened.
Write-Output "$Username was new and has been created"
}
}
Two users from the file usersFile2.csv being added, and it works. but if there is a user with the same sAMAccountName I want the script to add a number...
givenName,sn,displayName,UserPrincipalName,mail,sAMAccountName,Office,Department,Description,password,Path
Holger,Svensson,Holger Svensson,[email protected],[email protected],hosv,Executives,Executives,Head of Finance,Syp9393, 'OU=dd1, OU=My_users, DC=internal, DC=sonic, DC=com'
Marie,Bergqvist,Marie Bergqvist,[email protected],[email protected],mabe,Executives,Executives,Head of RND,Syp9393, 'OU=dd1, OU=My_users, DC=internal, DC=sonic, DC=com'
I badly need help.. I have tried for hours.. Im not the one to give up... but man.. Any ideas?