¡ Por favor active Javascript! / Please turn on Javascript!

Otras versiones Servidor de Windows no clasificadas: NT 4.0 ...
Avatar de Usuario
Cazador
Colaborador
Mensajes: 1899
Registrado: 16 Abril 2015, 19:16
C. Prof.: Analista de Sistemas
Contactar:

Powershell - Agregar usuarios desde un fichero a un grupo de Directorio Activo

Mensajepor Cazador » 28 Agosto 2018, 13:50

Script que permite añadir un listado de usuarios desde un fichero CSV a un grupo de directorio activo de forma automatica.

Código: Seleccionar todo

<#power<# Creator: ELSATE
Creation date: 2018-07-28

.SYNOPSIS
   Special Script to add users included in CSV file to a specific group
.DESCRIPTION
   This script allow add users included in CSV file to a specific group

#>
#>

Import-Module ActiveDirectory

Clear-Host
Write-Host "--------------------------------------------------------------------"
Write-Host "Special Script to add users included in CSV file to a specific group"
Write-Host "--------------------------------------------------------------------"
Write-Host -BackgroundColor Red "NOTE: The CSV file specified must include only a column with the first name UserPrincipalName in first line. Example:"
Write-Host   "----------------------------"
Write-Host   "userPrincipalName           "
Write-Host   "dom.altea@elsate.com"
Write-Host   "salt.ea@elsate.com"
Write-Host   "bes.asea@elsate.com"
Write-Host   "ttess.ltea@elsate.com"
Write-Host   "alea.coma@elsate.com"
Write-Host   "tes.aste@elsate.com"
Write-Host   "..."
Write-Host   "----------------------------"

 Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
   
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "CSV (*.csv)| *.csv|TXT (*.txt)| *.txt"

    if($OpenFileDialog.ShowDialog() -eq 'Ok'){

    #Ask for the groupname
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

    $title = 'AD Group Name'
    $msg   = 'Enter the AD Group Name:'

    $groupname = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
    write-host "Adding CSV's Users to group $groupname"

    #Write-host "File created $($dlg.filename)"
        $groups = Import-Csv $OpenFileDialog.filename -header UserPrincipalName
        $groups | ForEach-Object {
            $user = $_.UserPrincipalName
            $user = Get-ADUser -Filter { UserPrincipalName -like $user }
           
            #Show user details
            #Get-ADUser -Filter { UserPrincipalName -like $user } -Properties userPrincipalName, name, emailAddress, SAMAccountName | Select userPrincipalName,name
           
            #Add users to AD Group
            if ($user -ne  $null) {
            Write-Host "Adding $user to group $groupname"
            Add-ADGroupMember -Identity $groupname -Members $user 
            }
        }
   
    }
}

#Ask for the File's location
get-filename("c:\temp")
Redactor de ELSATE.com. Aficionado de la Tecnología, los Videojuegos, la Música Electrónica y el Deporte.





Volver a “Otros”

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados