简体   繁体   English

尝试使用 PowerShell 脚本将用户添加到 AD 时出错

[英]Error when I try to add users to AD with my PowerShell script

I've a WindowsServer 2019 virtual machine with VMware Workstation.我有一台带有 VMware Workstation 的 WindowsServer 2019 虚拟机。

I've this data ( they are no sense, it's just for a test ) :我有这个数据(它们没有意义,只是为了测试):

firstname;lastname;office;password;departement    
Tim;machin;Paris;test1;direction
    Anais;machine;Lille;test1;direction
    Luc;truc;Lille;test1;traders
    Pauline;bidule;Toulouse;test1;traders
    Jean;lulu;Lille;test1;traders

And this Powershell script :这个 Powershell 脚本:

Import-Module ActiveDirectory
Import-Module 'Microsoft.PowerShell.Security'

$users = Import-Csv -Delimiter ";" -Path "C:\Users\Administrateur\Desktop\user.csv"

New-ADOrganizationalUnit -Name "Employes" -Path "dc=devensys-poc,dc=lan"
New-ADOrganizationalUnit -Name "Lille" -Path "ou=Employes,dc=devensys-poc,dc=lan"
New-ADOrganizationalUnit -Name "Paris" -Path "ou=Employes,dc=devensys-poc,dc=lan"
New-ADOrganizationalUnit -Name "Toulouse" -Path "ou=Employes,dc=devensys-poc,dc=lan"

foreach ($user in $users){

    $name = $user.firstname + " " + $user.lastname
    $fname = $user.firstname
    $lname = $user.lastname
    $login = $user.firstname + "." + $user.lastname
    $Uoffice = $user.office
    $Upassword = $user.password
    $dept = $user.departement

    switch($user.office){
        "Lille" {$office = "OU=Lille,OU=Employes,DC=devensys-poc,DC=lan"}
        "Paris" {$office = "OU=Paris,OU=Employes,DC=devensys-poc,DC=lan"}
        "Toulouse" {$office = "OU=Toulouse,OU=Employes,DC=devensys-poc,DC=lan"}
        default {$office = $null}
    }

    try {

            New-ADUser -Name $name -SamAccountName $login -UserPrincipalName $login -DisplayName $name -GivenName $fname -Surname $lname -AccountPassword (ConvertTo-SecureString $Upassword -AsPlainText -Force) -City $Uoffice -Path $office -Departement $dept -Enabled $true
            echo "User added : $name"

    } catch{
    echo "User not added : $name "
    }

    }

But everytime I try to execute my script, I've these errors:但是每次我尝试执行我的脚本时,都会出现以下错误:

New-ADOrganizationalUnit : Le serveur ne souhaite pas traiter la requête
Au caractère C:\Users\Administrateur\Desktop\script.ps1:6 : 1
+ New-ADOrganizationalUnit -Name "Employes" -Path "dc=devensys-poc,dc=l ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (OU=Employes,dc=devensys-poc,dc=lan:Stri 
   ng) [New-ADOrganizationalUnit], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Mana 
   gement.Commands.NewADOrganizationalUnit

New-ADOrganizationalUnit : Le serveur ne souhaite pas traiter la requête
Au caractère C:\Users\Administrateur\Desktop\script.ps1:7 : 1
+ New-ADOrganizationalUnit -Name "Lille" -Path "ou=Employes,dc=devensys ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (OU=Lille,ou=Emp...nsys-poc,dc=lan:Strin 
   g) [New-ADOrganizationalUnit], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Mana 
   gement.Commands.NewADOrganizationalUnit

New-ADOrganizationalUnit : Le serveur ne souhaite pas traiter la requête
Au caractère C:\Users\Administrateur\Desktop\script.ps1:8 : 1
+ New-ADOrganizationalUnit -Name "Paris" -Path "ou=Employes,dc=devensys ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (OU=Paris,ou=Emp...nsys-poc,dc=lan:Strin 
   g) [New-ADOrganizationalUnit], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Mana 
   gement.Commands.NewADOrganizationalUnit

New-ADOrganizationalUnit : Le serveur ne souhaite pas traiter la requête
Au caractère C:\Users\Administrateur\Desktop\script.ps1:9 : 1
+ New-ADOrganizationalUnit -Name "Toulouse" -Path "ou=Employes,dc=deven ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (OU=Toulouse,ou=...nsys-poc,dc=lan:Strin 
   g) [New-ADOrganizationalUnit], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Mana 
   gement.Commands.NewADOrganizationalUnit

User not added : Tim machin
User not added : Anais machine  
User not added : Luc truc  
User not added : Pauline bidule
User not added : Jean lulu

And I don't know why... Do you have any idea where are my mistakes ?我不知道为什么......你知道我的错误在哪里吗? I'm beginner with PowerShell !我是 PowerShell 的初学者!

Thanks a lot !非常感谢 !

This error in AD is very frustrating and can only be realized when you experience it. AD 中的这个错误非常令人沮丧,只有当您遇到它时才能意识到。

Correct me if I'm wrong, but Le serveur ne souhaite pas traiter la requête is the equivalent of The server is unwilling to process the request .如果我错了,请纠正我,但Le serveur ne souhaite pas traiter la requête相当于The server is unwilling to process the request Le serveur ne souhaite pas traiter la requête The server is unwilling to process the request

You can possibly get this error by having your DistinguishedName not explicitly typed out correctly.您可能会因为您的DistinguishedName没有明确输入正确而得到这个错误。 To prevent this, I would use the AD module to query it and pass it in as a variable so no mistakes are made.为了防止这种情况,我将使用 AD 模块来查询它并将其作为变量传递,这样就不会出错。

I would also make sure that you are on the domain with the computer/server you're running this script from.我还会确保您在运行此脚本的计算机/服务器所在的域中。

$ADDomain = Get-ADDomain
New-ADOrganizationalUnit -Name "Employes" -Path "$ADDomain.DistinguishedName"

$OUObject = Get-ADOrganizationalUnit -Name "Employes"
New-ADOrganizationalUnit -Name "Lille" -Path "$OUObject.DistinguishedName"
New-ADOrganizationalUnit -Name "Paris" -Path "$OUObject.DistinguishedName"
New-ADOrganizationalUnit -Name "Toulouse" -Path "$OUObject.DistinguishedName"

Let us know if this resolves your issue!如果这能解决您的问题,请告诉我们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM