-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ps1
113 lines (99 loc) · 3.8 KB
/
script.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<#
.SYNOPSIS
Import-ADUsers.ps1
.DESCRIPTION
Importe des utilisateurs dans Active Directory à partir d'un fichier CSV.
#>
# Demander le chemin du fichier CSV
$FichierCsv = Read-Host -Prompt "Veuillez entrer le chemin complet du fichier CSV :"
if (-not (Test-Path $FichierCsv)) {
Write-Error "Le fichier spécifié n'existe pas. Veuillez fournir un chemin valide."
exit
}
$Utilisateurs = Import-Csv $FichierCsv
# Importer le module Active Directory
Import-Module ActiveDirectory
function Get-ManagerDN {
param (
[string]$NomAfficheManager
)
if ($NomAfficheManager) {
return Get-ADUser -Filter "DisplayName -eq '$NomAfficheManager'" -Properties DisplayName |
Select-Object -ExpandProperty DistinguishedName
}
return $null
}
function New-ADUserFromCSV {
param (
[PSCustomObject]$Utilisateur
)
$Prenom = $Utilisateur.'First name'
$Nom = $Utilisateur.'Last name'
$NomAffiche = $Utilisateur.'Display name'
$NomCompte = $Utilisateur.'User logon name'
$NomPrincipal = $Utilisateur.'User principal name'
$Adresse = $Utilisateur.'Street'
$Ville = $Utilisateur.'City'
$Etat = $Utilisateur.'State/province'
$CodePostal = $Utilisateur.'Zip/Postal Code'
$Pays = $Utilisateur.'Country/region'
$Titre = $Utilisateur.'Job Title'
$Departement = $Utilisateur.'Department'
$Entreprise = $Utilisateur.'Company'
$Manager = Get-ManagerDN -NomAfficheManager $Utilisateur.'Manager'
$OU = $Utilisateur.'OU'
$Description = $Utilisateur.'Description'
$Bureau = $Utilisateur.'Office'
$Telephone = $Utilisateur.'Telephone number'
$Email = $Utilisateur.'E-mail'
$Mobile = $Utilisateur.'Mobile'
$Notes = $Utilisateur.'Notes'
$StatutCompte = $Utilisateur.'Account status'
# Vérifier si l'utilisateur existe déjà dans AD
$UtilisateurExiste = Get-ADUser -Filter { SamAccountName -eq $NomCompte } -ErrorAction SilentlyContinue
if ($UtilisateurExiste) {
Write-Warning "L'utilisateur '$NomCompte' existe déjà dans Active Directory."
return
}
# Paramètres pour le nouvel utilisateur
$ParamsNouvelUtilisateur = @{
Name = "$Prenom $Nom"
GivenName = $Prenom
Surname = $Nom
DisplayName = $NomAffiche
SamAccountName = $NomCompte
UserPrincipalName = $NomPrincipal
StreetAddress = $Adresse
City = $Ville
State = $Etat
PostalCode = $CodePostal
Country = $Pays
Title = $Titre
Department = $Departement
Company = $Entreprise
Manager = $Manager
Path = $OU
Description = $Description
Office = $Bureau
OfficePhone = $Telephone
EmailAddress = $Email
MobilePhone = $Mobile
AccountPassword = (ConvertTo-SecureString "P@ssw0rd1234" -AsPlainText -Force)
Enabled = if ($StatutCompte -eq "Enabled") { $true } else { $false }
ChangePasswordAtLogon = $true # L'utilisateur doit changer le mot de passe lors de la prochaine connexion
}
if (![string]::IsNullOrEmpty($Notes)) {
$ParamsNouvelUtilisateur.OtherAttributes = @{info = $Notes }
}
try {
New-ADUser @ParamsNouvelUtilisateur
Write-Host "Utilisateur $NomCompte créé avec succès." -ForegroundColor Cyan
}
catch {
Write-Warning "Échec de la création de l'utilisateur $NomCompte. $_"
}
}
# Créer les utilisateurs dans Active Directory
foreach ($Utilisateur in $Utilisateurs) {
New-ADUserFromCSV -Utilisateur $Utilisateur
}