提供的名称不是正确格式的帐户名称

问题描述 投票:0回答:1

有人可以帮助解决我遇到的另一个错误吗?

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

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 "[email protected]" `
        -Name $Firstname $Lastname `
        -GivenName $Firstname `
        -Department "$Department" `
        -Company "$Company" `
        -EmailAddress "[email protected]" `
        -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:提供的名称不是正确格式的帐户名
在 C:\AD_Scripts\psscripts\user_create.ps1:34 char:9
+ 新 ADUser `
+ ~~~~~~~~~~~~
    + 类别信息:未指定:(CN=\ ,OU=stude...dc=clasd,dc=net:String) [New-ADUser], ADException
    + FullQualifiedErrorId :提供的名称不是格式正确的帐户名称,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=学生,OU=用户,OU=Elem,DC=clasd,DC=net.FNAME+ ' ' + CN=User2 User2,OU=学生,OU=用户,OU=Elem,DC=clasd, DC=net.LNAME New-ADUser :提供的名称不是正确格式的帐户名称 在 C:\AD_Scripts\psscripts\Untitled1.ps1:48 char:1 + 新 ADUser @ADUsers} + ~~~~~~~~~~~~~~~~~~~ + 类别信息:未指定:(CN=CN\=User2 Us...dc=clasd,dc=net:String) [New-ADUser],ADException + FullQualifiedErrorId :提供的名称不是格式正确的帐户名称,Microsoft.ActiveDirectory.Management.Commands.NewADUser

powershell active-directory
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.