locked
Import-CSV impor only the last line from the CSV and said than the User already exists RRS feed

  • Question

  • I have write a script with forms that import AD Users and fill in the domain name with the input field.

    The script works then the CSV only have one line. When the CSV has more then one line, the script only import the last line and said than "The specified account already exists".

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
    Import-Module ActiveDirectory 
    
    #GUI Oberfläche
    $Form = New-Object System.Windows.Forms.Form
    $Form.Size = New-Object System.Drawing.Size(280,150)
    $Form.Text = "Benutzer hinzufügen"
    
    #-------Labels-----------
    #Label Benutzer hinzufügen
    $labeladduser                    = New-Object System.Windows.Forms.Label
    $labeladduser.AutoSize           = $true
    $labeladduser.width              = 25
    $labeladduser.height             = 10
    $labeladduser.location           = New-Object System.Drawing.Point(66,13)
    $labeladduser.Font               = 'Microsoft Sans Serif,10' 
    $labeladduser.Text               = "Benutzer Import per CSV"
    $labeladduser.Name               = "Benutzer Import per CSV"
    $Form.Controls.Add($labeladduser)
    
    #Label Domain
    $Labeldomain                     = New-Object System.Windows.Forms.Label
    $Labeldomain.AutoSize            = $true
    $Labeldomain.width               = 25
    $Labeldomain.height              = 10
    $Labeldomain.location            = New-Object System.Drawing.Point(8,45)
    $Labeldomain.Font                = 'Microsoft Sans Serif,10'
    $Labeldomain.Text                = "Bitte Domäne angeben:"
    $Labeldomain.Name               = "Bitte Domäne angeben:"
    $Form.Controls.Add($Labeldomain)
    
    
    #Close GUI Button
    $BTcancel = New-Object System.Windows.Forms.Button
    $BTcancel.text                   = "Cancel"
    $BTcancel.width                  = 75
    $BTcancel.height                 = 23
    $BTcancel.location               = New-Object System.Drawing.Point(180,80)
    $BTcancel.Font                   = 'Microsoft Sans Serif,10'
    $BTcancel.Name                   = "Cancel"
    $BTcancel.Add_Click({$Form.Close()})
    $Form.Controls.Add($BTcancel) 
    
    #Speichern Button
    $BTsave = New-Object System.Windows.Forms.Button
    $BTsave.text                     = "Speichern"
    $BTsave.width                    = 75
    $BTsave.height                   = 23
    $BTsave.location                 = New-Object System.Drawing.Point(85,80)
    $BTsave.Font                     = 'Microsoft Sans Serif,10'
    $BTsave.Name                     = "Speichern"
    $BTsave.Add_Click({$Form.Close()})
    $Form.Controls.Add($BTsave) 
    
    #------Textfields------
    #Textfeld Domain
    $Textboxdomain = New-Object System.Windows.Forms.TextBox 
    $Textboxdomain.multiline         = $false
    $Textboxdomain.width             = 100
    $Textboxdomain.height            = 23
    $Textboxdomain.location          = New-Object System.Drawing.Point(160,43)
    $Textboxdomain.Font              = 'Microsoft Sans Serif,10'
    $Form.Controls.Add($Textboxdomain)
    
    
    #------Funktion------
    $Import =Import-CSV -Path "C:\_scripts\ADUsers.csv" 
    
    $BTsave.Add_Click({New-PSSession -ComputerName ($Textboxdomain.Text+"-dc02."+$Textboxdomain.Text+".dom") -Credential ($Textboxdomain.Text+"\administrator")})
    
    foreach ($user in $Import){
      $password = $user.password | ConvertTo-SecureString -AsPlainText -Force
      $BTsave.Add_Click({ 
      New-ADUser -Server ($Textboxdomain.Text+"-dc02."+$Textboxdomain.Text+".dom") `
      -Name ($user.Nachname + ", " + $user.Vorname)`
      -SamAccountName $user.username`
      -GivenName $user.Vorname `
      -Surname $user.Nachname `
      -EmailAddress $user.mail `
      -DisplayName ($user.Nachname + ", " + $user.Vorname)`
      -Mobile $user.mobile `
      -Path ("OU=Kunde,OU=Benutzer,DC="+$Textboxdomain.Text+",DC=dom") -AccountPassword $Password `
      -ChangePasswordAtLogon $False `
      -Enabled $True `
      -UserPrincipalName ($user.username+"@"+$Textboxdomain.Text+".dom") `
      -OfficePhone $user.phone
      })
      
    }
    [void] $Form.ShowDialog()

    This is the CSV:

    Vorname,Nachname,username,Password,mail,phone,mobile
    Thomas,Schuh,TSchuh,password123,thomas.schuh@xxx.de,+49 30 1111 1111 3,0
    Ramona,Mann,TSchuh,password123,thomas.schuh@xxx.de,+49 30 1111 1111 2,0

    Thursday, June 27, 2019 2:23 PM

Answers

  • You are adding each user to the click event in a loop.  This will not work.  You can only add the click event once.

    The following will work.

    $BTsave.Add_Click({
        foreach ($user in $Import) {
            $password = $user.password | ConvertTo-SecureString -AsPlainText -Force
            New-ADUser -Server ($Textboxdomain.Text + "-dc02." + $Textboxdomain.Text + ".dom") `
               -Name ($user.Nachname + ", " + $user.Vorname)`
               -SamAccountName $user.username`
               -GivenName $user.Vorname `
               -Surname $user.Nachname `
               -EmailAddress $user.mail `
               -DisplayName ($user.Nachname + ", " + $user.Vorname)`
               -MobilePhone $user.mobile `
               -Path ("OU=Kunde,OU=Benutzer,DC=" + $Textboxdomain.Text + ",DC=dom") 
               -AccountPassword $Password `
               -ChangePasswordAtLogon $False `
               -Enabled $True `
               -UserPrincipalName ($user.username + "@" + $Textboxdomain.Text + ".dom") `
               -OfficePhone $user.phone
        }
    })
    


    \_(ツ)_/

    • Marked as answer by ITKrissi Thursday, June 27, 2019 2:45 PM
    Thursday, June 27, 2019 2:40 PM