提供的名称不是正确形成的帐户名称

时间:2016-11-18 17:51:00

标签: powershell active-directory

有人可以帮助解决我遇到的其他错误吗?

我的创建用户脚本给了我另一个错误。

foreach ($User in $ADUsers)
{
    #Read user data from each field in each row and assign the data to a  variable as below

    $Username   = $User.ID
    $Password   = $User.BDATE
    $Firstname  = $User.FNAME
    $Lastname   = $User.LNAME
    $Department = $User.GRD
    $Company    = $User.SCHID #This field refers to the OU the user account is to be moved to

    # Choose OU
    switch ($Company)
    {
        "1480" {$OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'}
        "1479" {$OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'}
        "1480" {$Folder = '\\hs-ss\students\hs'}
        "1479" {$Folder = '\\hs-ss\students\elem'}
    }

    #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "$Username@clasd.net" `
        -Name $Firstname $Lastname `
        -GivenName $Firstname `
        -Department "$Department" `
        -Company "$Company" `
        -EmailAddress "$Username@clasd.net" `
        -Surname $Lastname `
        -Enabled $True `
        -Scriptpath "login.vbs" `
        -DisplayName "$Firstname $Lastname" `
        -Path $OU `
        -Homedrive "Z" `
        -homedirectory "$Folder\$username" `
        -AccountPassword (ConvertTo-SecureString "$User.BDATE" -AsPlainText -Force) `
        -ChangePasswordAtLogon $true
}

我的错误是:

New-ADUser : The name provided is not a properly formed account name
At C:\AD_Scripts\psscripts\user_create.ps1:34 char:9
+         New-ADUser `
+         ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=\\ ,OU=stude...dc=clasd,dc=net:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : The name provided is not a properly formed account name,Microsoft.ActiveDirectory.Management.Commands.NewADUser

编辑1

如果我Write-Host $Firstname $Lastname我得到“User2 User2”,这是正确的。

编辑2

即使收到我收到的消息,该帐户仍会被创建。

编辑3

我已经走了,并且像我被告知的那样滔滔不绝。我仍然在努力解决同样的错误。只有这次用户才会被创建。

# Import active directory module for running AD cmdlets
 Import-Module activedirectory

 #Store the data from ADUsers.csv in the $ADUsers variable
 $ADUsers = Import-csv userimport.csv
 #Store report in log file in the $log variable
 $log = "log.txt"

 #Set Additional Variables
 $Password = (ConvertTo-SecureString -AsPlainText "$User.BDATE" -Force)
 $DisplayName = "$User.FNAME+ ' ' + $user.LNAME"
 $Company = $User.SCHID

 # Choose OU

Switch ($Company)
{
    "1480" {$OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'}
    "1479" {$OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'}
    "1480" {$Folder = '\\hs-ss\students\hs'}
    "1479" {$Folder = '\\hs-ss\students\elem'}
}


Write-Host $DisplayName

#Create Hash Table for New User Creation

 $ADUsers = @{

'SamAccountName' = "$User.ID"
'UserPrincipalName' = "$User.ID + '@clasd.net'"
'GivenName' = "$User.FNAME"
'SurName' = "$User.LNAME"
'EmailAddress' = "$User.ID = '@clasd.net'"
'Path' = $OU
'Department' = "$User.GRD"
'Company' = "$User.SCHID"
'AccountPassword' = $Password
'ChangePasswordAtLogon' = $true
'Enabled' = $true
'DisplayName' = "$DisplayName"
'Name' = $Displayname
}

#Call New-ADUser with the parameters Above
Foreach ($User in $ADUsers) {
New-ADUser @ADUsers}

PS C:\ AD_Scripts \ psscripts> \ Untitled1.ps1 CN = User2 User2,OU = Students,OU = Users,OU = Elem,DC = clasd,DC = net.FNAME +''+ CN = User2 User2,OU = Students,OU = Users,OU = Elem,DC = clasd, DC = net.LNAME New-ADUser:提供的名称不是正确形成的帐户名称 在C:\ AD_Scripts \ psscripts \ Untitled1.ps1:48 char:1 +新ADUser @ADUsers} + ~~~~~~~~~~~~~~~~~~~     + CategoryInfo:NotSpecified:(CN = CN \ = User2 Us ... dc = clasd,dc = net:String)[New-ADUser],ADException     + FullyQualifiedErrorId:提供的名称不是正确形成的帐户名,Microsoft.ActiveDirectory.Management.Commands.NewADUser

1 个答案:

答案 0 :(得分:0)

我已将powershell更新到版本4,我在我发布的原始脚本中不再收到任何错误。以前我使用的是Ver 3