Benutzerliste in Active Directory importieren – PowerShell

Wenn man verschiedene Systeme hat, sich einen User – Export zieht und möchte diese Benutzer in eine ActriveDirectory Gruppe füllen, sorgt dieses Script für Abhilfe. Diese Benutzerliste in Active Directory importieren geht ganz einfach mit einem PowerShell Script. Dieses kann mit mehreren Varianten wie SID, Mail oder Benutzername umgehen. Ebenfalls werden die Errors, also Fehler die auftreten per Mail verschickt.

Benutzerliste in Active Directory importieren

Diese Input Varianten sind möglich:

  • Username
  • Email-Adress
  • User SID

Benutzerliste in active directory importieren

Dazu müssen noch folgende Zeilen bearbeitet werden:

    • 7-13 (User / Group / List – Settings)
    • 20-24 (Mail – Settings)
  • 56-73 (CSV – Settings) (Split-String, default ‘;’)
  • 98 (Domain – Settings)

(userimport.ps1 – Powershell)

<#
.SYNOPSIS
  Benutzer Liste in AD importieren
.DESCRIPTION
  Importiert Benutzer aus einer Liste in das Active Directory
.NOTES
  Version:        1.0
  Author:         www.ITnator.net | Johannes Huber
  Creation Date:  15.02.2017
.EXAMPLE
  Run as administrator
#>



############################################### SCRIPT SETTINGS ##################################################
$inputfile = "C:\JOBS\input\list_sid.txt" # EVERY USER IN A NEW LINE
$logfile = "C:\JOBS\logs\list_LOG.txt" # Logfile
$groupname = "youractivedirectorygroup" # AD Group Name
$grouplocation = "OU=yourOU,OU=yourOU,DC=your,DC=domain" # AD structure
$inputtypeuser = "sid" # user = Username, mail = Mail-Adress, sid = User SID (INPUT)
$trimscv = "false" # true = file is an CSV file, false = do NOT trim file (Settings in line 57-74)
$howtotrimcsv = "0" # get first info (sid=0) (user=1) (date=6)
##################################################################################################################




############################################### MAIL SETTINGS ####################################################
$smtpServer="0.0.0.0" # yourMailserver
$from = "yourname <[email protected]>" # SENDER
$receiver = "[email protected]" # RECEIVER
$subject= "Fehler bei Datenimport" # Message subject
$textEncoding = [System.Text.Encoding]::UTF8 # text encoding
###################################################################################################################




############################################### GENERAL SETTINGS ##################################################
$date = Get-Date -format ddMMyyyy
###################################################################################################################




############################################### SCRIPT ############################################################


#IF AD-GROUP EXISTS, CREATE NEW
try {Get-ADGroup $groupname}
  catch{
        #CREATE AD-GROUP
        NEW-ADGroup –name $groupname –groupscope Global –path $grouplocation
     }

#CLEAR AD-GROUP
Get-ADGroupMember $groupname | ForEach-Object {Remove-ADGroupMember $groupname $_ -Confirm:$false}

#READ AD-USER FROM FILE
$inputusers = Get-Content $inputfile

#ADD USER TO AD-GROUP
foreach ($line in $inputusers)
{
    #IS USER DISABLED
    If($trimscv -eq "true"){
    $isdisabled = $line.split(';')[4]
    if($isdisabled -ne ""){
    continue #nextline
    }
    #IS USER DISABLED
    If($trimscv -eq "true"){
    $isdisabled = $line.split(';')[5]
    if($isdisabled -ne ""){
    continue #nextline
    }
    }
    }
    #TRIM FILE
    If($trimscv -eq "true"){
    $line = $line.split(';')[$howtotrimcsv]
    }
    

    #TRY ADD USER AND WRITE LOG
    Try
    {
    #IF INPUT = user -> ADD USER
        If($inputtypeuser -eq "user"){
        Add-ADGroupMember $groupname $line -ErrorAction Stop
        add-content -path $logfile -value "User successfull added: $line"
        }
    #IF INPUT = mail -> ADD MAIL
        If($inputtypeuser -eq "mail"){
        $line = $line.Trim()
        $linemail = Get-ADUser -Filter {EmailAddress -like $line} -Properties EmailAddress | Select samaccountname | ft -HideTableHeaders | out-string
        $linemail = $linemail.Trim('')
        Add-ADGroupMember $groupname $linemail -ErrorAction Stop
        add-content -path $logfile -value "User successfull added: $line"
        }
    #IF INPUT = sid -> ADD SID
        If($inputtypeuser -eq "sid"){
        $line = $line.trim()
        $objSID = New-Object System.Security.Principal.SecurityIdentifier("$line")
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
        $objUser = $objUser.value.ToString()
        $objUser = $objUser.TrimStart('YOURDOMAIN') # yourdomain\username
        $objUser = $objUser.TrimStart('\')
        Add-ADGroupMember $groupname $objUser -ErrorAction Stop
        add-content -path $logfile -value "User successfull added: $objUser"
        }
    }
    Catch #ON ERROR WRITE LOG
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    add-content -path $logfile -value "User $line NOT added. $errormessage"
    # Send Email Message
    $body = "User $line NOT added. $errormessage 

 LOGFILE: \\yourserver\logs"
    Send-Mailmessage -smtpServer $smtpServer -from $from -to $receiver -subject $subject -body $body -bodyasHTML -priority High -Encoding $textEncoding
    }
}

#IMPORT DONE
$date = Get-Date -format d
add-content -path $logfile -value "#########################################################################################################"
add-content -path $logfile -value "############################################## IMPORT DONE ##############################################"
add-content -path $logfile -value "############################################## $date  ##############################################"
add-content -path $logfile -value "#########################################################################################################"

🙂

Johannes Huber
 

In seiner Freizeit macht Johannes nichts lieber, als für ITnator Beiträge zu schreiben. Input bekommt er hierfür von Problemen in der IT Administration von Servern, Clients und vielen weiteren IT Komponenten.

sidebar
>