PowerShell 添加用户,如果存在,则添加一个数字

PowerShell 添加用户,如果存在,则添加一个数字

我正在从文件 usersFile2.csv 创建活动目录用户。用户已添加。添加了正确的帐户信息,例如名字、姓氏等...

问题。如果存在具有相同 sAMAccountName 的用户,我希望脚本将数字添加到 sAMAccountName。我需要编辑 Get-ADuser 部分...

# 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"
    }
}

从文件 usersFile2.csv 中添加两个用户,并且可以正常工作。但如果有一个具有相同 sAMAccountName 的用户,我希望脚本添加一个数字......

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'

我非常需要帮助..我已经尝试了好几个小时..我不会放弃...但是伙计..有什么想法吗?

答案1

使用while循环更新并测试用户名,直到找到有效的用户名:

foreach ($User in $ADUsers)
{
    $Username = $User.sAMAccountName
    $Password = $User.password
    $Firstname = $User.givenName
    $Lastname = $User.sn

    $usernameCounter = 0

    while(Get-ADUser -F {SamAccountName -eq $Username} -EA 0)
    {
        # bump numerical suffix value
        $usernameCounter++
        # update candidate username
        $Username = $User.sAMAccountName + $usernameCounter
    }

    New-ADUser ...
}

答案2

CN 必须是唯一的。replace -Name "$Firstname $Lastname"with -Name $Username

导入模块 ActiveDirectory

$ADUsers = 导入 Csv'C:\IT\users.csv'

$UPN =“测试.本地”

foreach ($Users 在 $ADUsers 中) {

$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$initials = $User.initials
$OU = $User.ou
$email = $User.email
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$description = $User.description
if ($lastname.Length -ge 2)
{
    $Username = $FirstName + $LastName.Substring(0,2 )
}
else
{
    $Username = $FirstName + $LastName
}
$usernameCounter = 0

while(Get-ADUser -F {SamAccountName -eq $Username})
{
    $usernameCounter++
    $Username = $username + $usernameCounter
}New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "$username@$UPN" `
        -Name $Username `
        -GivenName $firstname `
        -Surname $lastname `
        -Initials $initials `
        -Enabled $True `
        -DisplayName "$firstname $lastname" `
        -Path $OU `
        -Company $company `
        -Description $description `
        -EmailAddress $email `
        -Title $jobtitle `
        -Department $department `
        -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $False

    # If user is created, show message.
    Write-Host "The user account $username is created." -ForegroundColor Cyan

}

相关内容