Search code examples
powershellwindows-server-2016

can't create userS via powershell


I can't import users in powershell with a script via an csv file, but If I print the parameters on the screen,it shows them as it should. what I am doing wrong? in my life plenty with that mustache, but plis focus on the script.

is running windows server 2016 on the powershell ise, on virtualbox

The Script:


If(-Not(Get-ADOrganizationalUnit -Filter {Name -eq "991-5D"}))
{New-ADOrganizationalUnit "991-5D" -Path (Get-ADDomain).DistinguishedName}
If(-Not(Get-ADOrganizationalUnit -Filter {Name -eq "911-5V"}))
{New-ADOrganizationalUnit "911-5V" -Path (Get-ADDomain).DistinguishedName}
$domain=(Get-ADDomain).DNSRoot

Import-Csv -Path "C:\Alumnos.csv" | foreach-object {
[int]$number= $_.X
If($number -ge 10 -and $number -le 26)
{
$UO="991-5D"
}
//there  are many others O.U.

$ou= "UO="+$UO+","+$domain
$UPN = $_.LETRA+$_.PATERNO+$_.X+"@"+ "$domain"
$CUENTA= $_.LETRA+$_.PATERNO+$_.X

New-ADUser -SamAccountName $CUENTA -UserPrincipalName $CUENTA -Name $_.NOMBRE 
-SurName $_.PATERNO -GivenName $_.NOMBRE -EmailAddress $UPN -AccountPassword 
(ConvertTo-SecureString "Leica666" -AsPlainText -force) -Path $ou 
-Enabled $true -ChangePasswordAtLogon $true -Verbose}

the data:

X,PATERNO,MATERNO,NOMBRE,SEGUNDO,LETRA
10,ARÉVALO,CORNEJO,NICOLÁS,ALEJANDRO,N
11,BARRIOS,MONTERO,BENJAMÍN,IGNACIO,B
12,BUSTAMANTE,LOYOLA,IGNACIO,HERNANDO,I
13,BUSTOS,GARRIDO,ARTURO,IGNACIO,A

this are the results on each line:

+ New-ADUser -SamAccountName $CUENTA -UserPrincipalName $CUENTA -Name $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified: (CN=IGNACIO,UO=9...da.com:String) 
     [New-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8335,
Microsoft.ActiveDirectory.Management.Commands.NewADUser

the head: X,PATERNO,MATERNO,NOMBRE,SEGUNDO,LETRA

echo: @{X=42; PATERNO=PAYACÁN; MATERNO=ZAPATA; NOMBRE=NICOLÁS; SEGUNDO=N; LETRA=}.NOMBRE

I know that reads the file and instead of reading just the column reads all the line($_), and then prints whatever I wrote next to it(".name", ".section", etc).


Solution

  • I've made some variable and format changes to make this code more successful.

    $domain=Get-ADDomain
    Import-Csv -Path "C:\Alumnos.csv" |
      Foreach-Object {
        [int]$number= $_.X
        If($number -ge 10 -and $number -le 26)
        {
            $UO="991-5D"
        }
    
        $ou = "OU={0},{1}" -f $UO,$domain.DistinguishedName
        $UPN = "{0}{1}{2}@{3}" -f $_.LETRA,$_.PATERNO,$_.X,$domain.DNSRoot
        $CUENTA= "{0}{1}{2}" -f $_.LETRA,$_.PATERNO,$_.X
    
        New-ADUser -SamAccountName $CUENTA -UserPrincipalName $UPN -Name $_.NOMBRE `
        -SurName $_.PATERNO -GivenName $_.NOMBRE -EmailAddress $UPN `
        -AccountPassword (ConvertTo-SecureString "Leica666" -AsPlainText -force) -Path $ou `
        -Enabled $true -ChangePasswordAtLogon $true -Verbose
      }
    

    Explanation:

    • $domain: I've made this an ADDomain object. This allows the DistinguishedName and DNSRoot properties to be accessed where appropriate.
    • -f operator: I used the format operator to make it easier to read the string concatenation attempts.
    • $ou: This is constructed using the DistinguishedName of the domain. This is the proper format for the OU path.
    • $UPN: This is constructed using the DNSRoot of the domain. It can obviously be different than your domain, but must be in an email address or FQDN format.

    Additional Comments:

    You are setting -Name to be $_.NOMBRE. This could be problematic because Name must be unique in each OU. Name is used to build the CN, which is where uniqueness is required. If you have NICOLAS in OU 991-5D, you are going to get an error if you try to create another NICOLAS in the same place. IMHO, I would do something different. You could also implement the use of splatting for building the properties of your New-ADUser command, but that is only for readability purposes. Below is an example of splatting:

    $NewUserProperties = @{
        SamAccountName = $CUENTA
        UserPrincipalName = $UPN
        Name = $_.NOMBRE
        Surname = $_.PATERNO
        GivenName = $_.NOMBRE
        EmailAddress = $UPN
        AccountPassword = (ConvertTo-SecureString "Leica666" -AsPlainText -force)
        Path = $ou
        Enabled = $true
        ChangePasswordAtLogon = $true
    }
    
    New-ADUser @NewUserProperties -Verbose