My commande does not work New ADUSER

Welcome Forums General PowerShell Q&A My commande does not work New ADUSER

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #115177

    Participant
    Points: 11
    Rank: Member

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

    $users = Import-Csv -Delimiter ";" -Path "C:\add\import.csv"

    #********************Création des OU général********************************

    New-ADOrganizationalUnit -Name "Paris" -Path "DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "utilisateur" -Path "OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "projet" -Path "OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "ordinateurs" -Path "OU=Paris,DC=certification,DC=LeRebours,DC=lrb"

    #********************Création des OU secondaire********************************

    New-ADOrganizationalUnit -Name "reseau" -Path "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "developpeur" -Path "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "chefdeprojet" -Path "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "administrateur" -Path "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "STS1" -Path "OU=projet,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "STS2" -Path "OU=projet,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "fixe" -Path "OU=ordinateurs,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "portable" -Path "OU=ordinateurs,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"
    New-ADOrganizationalUnit -Name "serveur" -Path "OU=ordinateurs,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"

    #*******Ajout de chaque utilisateur dans son OU spécifique*******

    foreach ($user in $users){

    $name = $user.prenom + " " + $user.nom
    $fname = $user.prenom
    $lname = $user.nom
    $login = $user.prenom + "." + $user.nom
    $Uservice = $user.service
    $Upassword = $user.password
    $groupe = $user.groupe

    switch($user.service){
    "reseau" {$service = "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"}
    "developpeur" {$service = "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"}
    "chefdeprojet" {$service = "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"}
    "administrateur" {$service = "OU=utilisateur,OU=Paris,DC=certification,DC=LeRebours,DC=lrb"}
    default {$service = $null}
    }

    try {
    New-ADUser -Name $name -SamAccountName $login -UserPrincipalName $login -DisplayName $name -GivenName $fname -Surname $lname -AccountPassword (ConvertTo-SecureString $Upassword -AsPlainText -Force) -ServicePrincipalNames $Uservice -Path $service -Organization $groupe -Enabled $true
    echo "user add: $name"

    } catch{
    echo "user no add : $name"
    }

    }

    When I use this command, users are not added, can you help me ?

  • #117046

    Participant
    Points: 94
    Rank: Member

    Hi mralexdu7769 –

    There are a few things I would be curious on — but first, for future, it makes it a lot easier to read the code if it is formatted with the pre tags. What is your

    $ErrorActionPreference

    set to? You are in a try/catch for your New-ADUser cmdlet, but you won't necessarily catch all errors if your

    $ErrorActionPreference

    isn't set to Stop. It may be producing some unexpected results.

    Also – the documentation for the ServicePrincipalName parameter is used differently than you are using it here. I have adjusted your code to use paramter splatting to make it easier to read.

        try
        {
            $NewUserParams = @{
                Name                  = $name
                SamAccountName        = $login
                UserPrincipalName     = $login
                DisplayName           = $name
                GivenName             = $fname
                Surname               = $lname
                AccountPassword       = ConvertTo-SecureString $Upassword -AsPlainText -Force
                ServicePrincipalNames = @{Add = $Uservice}
                Path                  = $service
                Organization          = $groupe
                Enabled               = $true
                ErrorActionPreference = 'Stop'
            }
            New-ADUser  @NewUserParams
            echo "user add: $name"
    
        }
        catch [System.Exception]
        {
            echo "user no add : $name"
            # output the error
            $_
        }
    

    I am hoping this helps you determine what error is being produced.

    HTH,
    Steve

  • #117447

    Participant
    Points: 322
    Helping Hand
    Rank: Contributor

    Let Windows Server write this code for you first, then tweak as needed.

    See this guidance on how.

    Introduction to Active Directory Administrative Center Enhancements (Level 100)
    https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/get-started/adac/introduction-to-active-directory-administrative-center-enhancements–level-100-

    Learning PowerShell with Active Directory Administrative Center (PowerShell History Viewer)
    Learning PowerShell with Active Directory Administrative Center (PowerShell History Viewer)

    Step-By-Step: Utilizing PowerShell History Viewer in Windows Server 2012 R2
    https://blogs.technet.microsoft.com/canitpro/2015/03/04/step-by-step-utilizing-powershell-history-viewer-in-windows-server-2012-r2

    Use Active Directory Administrative Center to Create PowerShell Commands in Windows Server 2012

    Use Active Directory Administrative Center to Create PowerShell Commands in Windows Server 2012

You must be logged in to reply to this topic.