AD-用户脚本没有输出

AD-user script has no output

我正在创建用于在 Active Directory 中添加多个用户的脚本。当我无法使问题中描述的指南起作用时,我偶然发现了 this link。然后我尝试了评论中的一种解决方案

Import-Module ActiveDirectory
# this defaults to csv fields delimited by a comma. If your CSV file uses a different 
# character, then add parameter '-Delimiter' followed by the actual character
$ADUsers = Import-Csv -Path 'C:\Users\Desktop\Powershell files\EM-mis-new-AD.csv'

# the Where-Object clause is just a precaution to omit records that have no username value
$ADUsers | Where-Object { $_.username -match '\S'} | ForEach-Object {
    $Username = $_.username
    if (Get-ADUser -Filter "SamAccountName -eq '$Username'" -ErrorAction SilentlyContinue) {
        Write-Warning "A user account with SamAccountName '$Username' already exist in Active Directory."
    }
    else {
        $Firstname  = $_.firstname
        $Lastname   = $_.lastname

        # use splatting on cmdlets that use a lot of parameters
        $userParams = @{
            SamAccountName        = $Username
            UserPrincipalName     = "$Username@Mydomain.com"
            Name                  = "$Firstname $Lastname"
            GivenName             = $Firstname
            Surname               = $Lastname
            Enabled               = $true
            DisplayName           = "$Firstname, $Lastname"
            Path                  = $_.ou
            AccountPassword       = (ConvertTo-SecureString $_.Password -AsPlainText -Force)
            ChangePasswordAtLogon = $true
        }
        # create the user and report back
        New-ADUser @userParams

        Write-Host "Created new user '$Username' with initial password: $($_.Password)"
    }
}

这是我的 CSV 文件

firstname;lastname;username;password;ou
Mette;Frederiksen;MeFr;Password1;OU=Salg,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Sussi;Hart;SuHa;Password1;OU=Salg,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Ove;Tylstrup;OvTy;Password1;OU=Salg,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Karlos;Mondolez;KaMo;Password1;OU=Lager,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Anne;Otto;AnOt;Password1;OU=Lager,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Dennis;Ågard;DeÅg;Password1;OU=Lager,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Helena;Riss;HeRi;Password1;OU=Okonomi,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local
Risa;Lamende;RiLa;Password1;OU=Okonomi,OU=Users,OU=RGD Aarhus,DC=rgd,DC=local

然而,当我运行上面的代码没有任何反应

PS C:\Users\RGDAdmin> C:\Users\RGDAdmin\Documents\ADUser.ps1
PS C:\Users\RGDAdmin>

当我添加 Delimiter 参数时,我得到这个

Created new user 'KaMo' with initial password: Password1
New-ADUser : The directory service was unable to allocate a relative identifier
At C:\Users\RGDAdmin\Documents\ADUser.ps1:31 char:9
+         New-ADUser @userParams
+         ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (CN=Anne Otto,OU...DC=rgd,DC=local:String) [New-ADUser], ADException
+ FullyQualifiedErrorId : 
ActiveDirectoryServer:8208,Microsoft.ActiveDirectory.Management.Commands.NewADUser

PS。我知道密码在密码方面是不好的做法

您的文件以分号分隔,因此您肯定需要指定 -Delimiter 参数。但是 the documentation 有一个警告:

To specify a semicolon (;) enclose it in single quotation marks.

所以它应该是这样的:

$ADUsers = Import-Csv -Delimiter ';' -Path 'C:\Users\Desktop\Powershell files\EM-mis-new-AD.csv'

如果仍然导致 RID 错误,则可能是服务器出了问题。您可以使用 AD 用户和计算机手动创建用户吗?

尝试查看此内容。我没有访问 ActiveDirectory 的权限,无法自行测试。

#helpers
        function usernameIsNotBlank {
            [CmdletBinding()]
            param(
                $Username
            )
            [regex]$rx = "\S"
            
            return $rx.match($Username)
        }
        
        function usernameDoesNotAlreadyExist {
            [CmdletBinding()]
            param(
                $Username
            )
            $UserDoesNotExist = $true
            
            $UserObject = $(
                try {
                    Get-ADUser $Username
                }
                catch {
                    $null
                }
            )
        
            if ($null -ne $UserObject) {
                $UserDoesNotExist = $false
                Write-Verbose "$Username already exists"
                
            }
            else {
                $UserDoesNotExist = $true
            }
        
            return $UserDoesNotExist
        }
        
        function suppliedUsernameIsAvailable {
            [CmdletBinding()]
            param(
                $Username
            )
        
            return ((usernameIsNotBlank -Username $Username) -and (usernameDoesNotAlreadyExist -Username $Username))
        }
        
    #script
        $OriginalVerbose = $VerbosePreference
        $VerbosePreference = "Continue"
        Import-Module ActiveDirectory
        
        $CSV = "C:\Users\Desktop\Powershell file\EM-mis-new-AD.csv"
        $Data = Import-CSV $CSV
        
        foreach ($Line in $Data) {
            if (suppliedUsernameIsAvailable($Line.username)) {
                New-ADUser -Name "$Line.firstname $Line.lastname" -GivenName "$Line.firstname" -Surname "$Line.lastname" -SamAccoutnname "$(Line.username)@mydomain.com" -AccountPassword (ConvertTo-SecureString $Line.password -AsPlainText -Force) -ChangePasswordAtLogon $true -Path "$Line.ou"
            }
        }
        $VerbosePreference = $OriginalVerbose