578 lines
15 KiB
PowerShell
578 lines
15 KiB
PowerShell
# Activation des logs
|
||
Start-Transcript -Path ".\logs\creation-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
|
||
|
||
# Prérequis :
|
||
Install-Module Microsoft.Graph -Scope CurrentUser
|
||
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
|
||
|
||
Install-Module -Name ExchangeOnlineManagement
|
||
Import-Module -Name ExchangeOnlineManagement
|
||
Install-Module -Name MSOnline
|
||
Import-Module -Name MSOnline
|
||
|
||
# => Compte Administrateur du tenant du domaine.
|
||
######## CONNEXION ###########
|
||
Connect-ExchangeOnline -UserPrincipalName "*******@h3campus.fr"
|
||
# => Domaine utilisé pour les adresses email
|
||
#$domain = "h3hitema.fr"
|
||
$domain = "h3campus.fr"
|
||
|
||
# => Paramètres pour Office 365 pour l'envoie des identifiants aux étudiants
|
||
$smtpServer = "smtp.office365.com"
|
||
$smtpPort = 587
|
||
$smtpUser = "Compte_EnvoieMail@h3hitema.fr" # L'adresse email de l'expéditeur
|
||
$smtpPassword = "********"
|
||
|
||
# => Définition du comportement pour les comptes existants
|
||
# Valeurs possibles : "IgnorerExistants" ou "ReinitializerMotDePasse"
|
||
$ComportementComptesExistants = "IgnorerExistants"
|
||
|
||
# => Charger les informations des étudiants à partir du fichier CSV
|
||
$csvPath = "students.csv"
|
||
$etudiants = Import-Csv -Path $csvPath
|
||
|
||
# Définir les codes de couleur
|
||
$red = "`e[31m"
|
||
$green = "`e[32m"
|
||
$reset = "`e[0m"
|
||
|
||
function Normalize-String {
|
||
param (
|
||
[string]$inputString
|
||
)
|
||
|
||
# Remplacer les caractères accentués et non valides
|
||
$normalizedString = $inputString -replace '[éèêë]', 'e' `
|
||
-replace '[àâä]', 'a' `
|
||
-replace '[ôö]', 'o' `
|
||
-replace '[ûü]', 'u' `
|
||
-replace '[ç]', 'c' `
|
||
-replace '[^a-zA-Z0-9]', '' # Supprime les caractères non alphanumériques
|
||
|
||
return $normalizedString
|
||
}
|
||
|
||
# Fonction pour vérifier si une adresse email existe déjà dans Office 365
|
||
function Adresse-Existe {
|
||
param (
|
||
[string]$email
|
||
)
|
||
try {
|
||
# Filtre correctement formatté avec la syntaxe de chaîne
|
||
$filter = "PrimarySmtpAddress -eq '{0}'" -f $email
|
||
$user = Get-Mailbox -RecipientTypeDetails UserMailbox -Filter $filter
|
||
if ($user) {
|
||
return $true
|
||
} else {
|
||
return $false
|
||
}
|
||
} catch {
|
||
return $false
|
||
}
|
||
}
|
||
|
||
# Fonction pour générer une adresse email unique
|
||
function Generer-Email {
|
||
param (
|
||
[string]$prenom,
|
||
[string]$nom,
|
||
[string]$domain = "h3campus.fr"
|
||
)
|
||
|
||
# Normaliser les chaînes et les convertir en minuscules
|
||
$prenomNormalise = (Normalize-String $prenom).ToLower()
|
||
$nomNormalise = (Normalize-String $nom).ToLower()
|
||
$domainLower = $domain.ToLower()
|
||
|
||
$index = 1
|
||
# Générer l'email en minuscules
|
||
$email = "{0}.{1}@{2}" -f $prenomNormalise.Substring(0, $index), $nomNormalise, $domainLower
|
||
|
||
# Boucle tant qu'une adresse existe
|
||
while (Adresse-Existe $email) {
|
||
$index++
|
||
$email = "{0}.{1}@{2}" -f $prenomNormalise.Substring(0, $index), $nomNormalise, $domainLower
|
||
}
|
||
|
||
return $email
|
||
}
|
||
|
||
function Generer-MotDePasse {
|
||
param (
|
||
[int]$length = 12,
|
||
[int]$specialCharsCount = 2
|
||
)
|
||
|
||
# Vérification de la longueur
|
||
if ($length -le $specialCharsCount) {
|
||
throw "La longueur du mot de passe doit être supérieure au nombre de caractères spéciaux."
|
||
}
|
||
|
||
# Définir les ensembles de caractères
|
||
$letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||
$numbers = '0123456789'
|
||
$specialChars = '!@#$%^&*()-+='
|
||
|
||
# Initialiser le mot de passe
|
||
$password = ''
|
||
|
||
# Ajouter des lettres
|
||
for ($i = 0; $i -lt ($length - $specialCharsCount - 1); $i++) {
|
||
$index = Get-Random -Minimum 0 -Maximum $letters.Length
|
||
$password += $letters[$index] # Prendre une lettre en utilisant l'index
|
||
}
|
||
|
||
# Ajouter un chiffre aléatoire
|
||
$index = Get-Random -Minimum 0 -Maximum $numbers.Length
|
||
$password += $numbers[$index] # Ajouter un chiffre en utilisant l'index
|
||
|
||
# Ajouter les caractères spéciaux
|
||
for ($i = 0; $i -lt $specialCharsCount; $i++) {
|
||
$index = Get-Random -Minimum 0 -Maximum $specialChars.Length
|
||
$password += $specialChars[$index] # Prendre un caractère spécial en utilisant l'index
|
||
}
|
||
|
||
# Mélanger les caractères
|
||
$finalPassword = -join ($password.ToCharArray() | Sort-Object { Get-Random })
|
||
|
||
return $finalPassword
|
||
}
|
||
|
||
# Fonction pour envoyer un email avec les informations de connexion
|
||
function Envoyer-Email {
|
||
param (
|
||
[string]$destinataire,
|
||
[string]$login,
|
||
[string]$password,
|
||
[bool]$isReset = $false
|
||
)
|
||
|
||
# Contenu de l'email avec mise en forme HTML moderne
|
||
$messageType = if ($isReset) { "Réinitialisation de votre mot de passe" } else { "Bienvenue sur votre compte $domain" }
|
||
$titre = if ($isReset) { "Réinitialisation de mot de passe" } else { "Création de compte" }
|
||
$subject = $messageType
|
||
$body = @"
|
||
<html>
|
||
<head>
|
||
<style>
|
||
body {
|
||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||
background-color: #f4f4f4;
|
||
margin: 0;
|
||
padding: 0;
|
||
}
|
||
.email-container {
|
||
max-width: 600px;
|
||
margin: 20px auto;
|
||
background-color: #ffffff;
|
||
border-radius: 10px;
|
||
overflow: hidden;
|
||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||
}
|
||
.header {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 30px;
|
||
text-align: center;
|
||
}
|
||
.header img.logo {
|
||
max-width: 150px;
|
||
height: auto;
|
||
margin-bottom: 15px;
|
||
}
|
||
.header h1 {
|
||
margin: 0;
|
||
font-size: 24px;
|
||
font-weight: 600;
|
||
}
|
||
.content {
|
||
padding: 30px;
|
||
color: #333;
|
||
}
|
||
.greeting {
|
||
font-size: 18px;
|
||
color: #333;
|
||
margin-bottom: 20px;
|
||
}
|
||
.info-box {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
border-radius: 8px;
|
||
padding: 25px;
|
||
margin: 25px 0;
|
||
color: white;
|
||
}
|
||
.info-row {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin: 15px 0;
|
||
padding: 12px;
|
||
background: rgba(255, 255, 255, 0.15);
|
||
border-radius: 5px;
|
||
backdrop-filter: blur(10px);
|
||
}
|
||
.info-label {
|
||
font-weight: 600;
|
||
font-size: 14px;
|
||
text-transform: uppercase;
|
||
letter-spacing: 0.5px;
|
||
}
|
||
.info-value {
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 16px;
|
||
font-weight: bold;
|
||
background: rgba(255, 255, 255, 0.2);
|
||
padding: 8px 15px;
|
||
border-radius: 5px;
|
||
}
|
||
.alert-box {
|
||
background-color: #fff3cd;
|
||
border-left: 4px solid #ffc107;
|
||
padding: 15px;
|
||
margin: 20px 0;
|
||
border-radius: 5px;
|
||
}
|
||
.alert-box strong {
|
||
color: #856404;
|
||
}
|
||
.info-section {
|
||
background-color: #e8f4fd;
|
||
border-left: 4px solid #2196F3;
|
||
padding: 15px;
|
||
margin: 20px 0;
|
||
border-radius: 5px;
|
||
}
|
||
.info-section p {
|
||
margin: 8px 0;
|
||
color: #0d47a1;
|
||
}
|
||
.link-button {
|
||
display: inline-block;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 12px 30px;
|
||
text-decoration: none;
|
||
border-radius: 25px;
|
||
margin: 20px 0;
|
||
font-weight: 600;
|
||
transition: transform 0.2s;
|
||
}
|
||
.footer {
|
||
background-color: #f8f9fa;
|
||
padding: 20px 30px;
|
||
text-align: center;
|
||
border-top: 1px solid #e0e0e0;
|
||
color: #666;
|
||
font-size: 14px;
|
||
}
|
||
.footer strong {
|
||
color: #333;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="email-container">
|
||
<div class="header">
|
||
<img src="https://www.h3campus.fr/images/h3-education.jpeg" alt="Logo H3 Campus" class="logo">
|
||
<h1>🔐 $titre</h1>
|
||
</div>
|
||
<div class="content">
|
||
<p class="greeting">Bonjour,</p>
|
||
<p>Nous avons le plaisir de vous transmettre vos identifiants de connexion pour accéder à votre espace Microsoft 365.</p>
|
||
|
||
<div class="info-box">
|
||
<div class="info-row">
|
||
<span class="info-label">📧 Identifiant</span>
|
||
<span class="info-value">$login</span>
|
||
</div>
|
||
<div class="info-row">
|
||
<span class="info-label">🔑 Mot de passe</span>
|
||
<span class="info-value">$password</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="alert-box">
|
||
<strong>⚠️ Important :</strong> Pour votre sécurité, vous devrez modifier ce mot de passe lors de votre première connexion.
|
||
</div>
|
||
|
||
<div class="info-section">
|
||
<p><strong>📱 Accès à vos services :</strong></p>
|
||
<p>• Messagerie Outlook</p>
|
||
<p>• Microsoft Teams</p>
|
||
<p>• OneDrive</p>
|
||
<p>• Office en ligne (Word, Excel, PowerPoint)</p>
|
||
</div>
|
||
|
||
<center>
|
||
<a href="https://www.office.com" class="link-button">🚀 Accéder à mon compte</a>
|
||
</center>
|
||
|
||
<p style="margin-top: 30px; font-size: 14px; color: #666;">
|
||
<strong>Conseil de sécurité :</strong> Conservez ces informations dans un endroit sûr et ne les partagez jamais avec quiconque.
|
||
</p>
|
||
</div>
|
||
<div class="footer">
|
||
<p><strong>Service Informatique</strong></p>
|
||
<p>H3 Campus</p>
|
||
<p style="font-size: 12px; color: #999; margin-top: 10px;">Cet email a été envoyé automatiquement, merci de ne pas y répondre.</p>
|
||
</div>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
"@
|
||
|
||
# Envoyer l'email
|
||
$smtpClient = New-Object Net.Mail.SmtpClient($smtpServer, $smtpPort)
|
||
$smtpClient.EnableSsl = $true
|
||
$smtpClient.Credentials = New-Object System.Net.NetworkCredential($smtpUser, $smtpPassword)
|
||
|
||
$mailMessage = New-Object Net.Mail.MailMessage
|
||
$mailMessage.From = $smtpUser
|
||
$mailMessage.To.Add($destinataire)
|
||
$mailMessage.Subject = $subject
|
||
$mailMessage.Body = $body
|
||
$mailMessage.IsBodyHtml = $true
|
||
|
||
try {
|
||
$smtpClient.Send($mailMessage)
|
||
Write-Host "Email envoyé avec succès à $destinataire" -ForegroundColor Green
|
||
} catch {
|
||
Write-Host "Échec de l'envoi de l'email à $destinataire : $_" -ForegroundColor Red
|
||
}
|
||
}
|
||
|
||
# NOUVELLE FONCTION : Attribuer toutes les licences cochées
|
||
function Attribuer-Licences {
|
||
param (
|
||
[string]$userId
|
||
)
|
||
|
||
try {
|
||
# Récupérer toutes les licences disponibles
|
||
$allLicenses = Get-MgSubscribedSku
|
||
|
||
# Définir les licences à attribuer
|
||
$licensesToAssign = @(
|
||
"EXCHANGESTANDARD_STUDENT", # Exchange Online (plan 1) pour les étudiants
|
||
"POWERAPPS_DEV", # Microsoft Power Apps for Developer
|
||
"STANDARDWOFFPACK_STUDENT" # Office 365 A1 pour les étudiants
|
||
)
|
||
|
||
$licenseIds = @()
|
||
|
||
foreach ($licenseName in $licensesToAssign) {
|
||
$license = $allLicenses | Where-Object { $_.SkuPartNumber -eq $licenseName }
|
||
if ($null -ne $license) {
|
||
$licenseIds += @{ SkuId = $license.SkuId }
|
||
Write-Host " ✓ Licence '$licenseName' trouvée" -ForegroundColor Green
|
||
} else {
|
||
Write-Host " ⚠ Licence '$licenseName' non disponible dans votre tenant" -ForegroundColor Yellow
|
||
}
|
||
}
|
||
|
||
if ($licenseIds.Count -gt 0) {
|
||
Set-MgUserLicense -UserId $userId -AddLicenses $licenseIds -RemoveLicenses @()
|
||
Write-Host " ✓ $($licenseIds.Count) licence(s) attribuée(s) avec succès" -ForegroundColor Green
|
||
return $true
|
||
} else {
|
||
Write-Host " ✗ Aucune licence disponible à attribuer" -ForegroundColor Red
|
||
return $false
|
||
}
|
||
|
||
} catch {
|
||
Write-Host " ✗ Erreur lors de l'attribution des licences : $_" -ForegroundColor Red
|
||
return $false
|
||
}
|
||
}
|
||
|
||
# Réinitialiser le mot de passe d'un utilisateur existant
|
||
function Reinitialiser-MotDePasse {
|
||
param (
|
||
[string]$email
|
||
)
|
||
|
||
try {
|
||
$newPassword = Generer-MotDePasse
|
||
|
||
$PasswordProfile = @{
|
||
Password = $newPassword
|
||
ForceChangePasswordNextSignIn = $true
|
||
}
|
||
|
||
$existingUser = Get-MgUser -Filter "UserPrincipalName eq '$email'" -ErrorAction Stop
|
||
|
||
Update-MgUser -UserId $existingUser.Id -PasswordProfile $PasswordProfile
|
||
|
||
Write-Host " ✓ Mot de passe réinitialisé pour $email" -ForegroundColor Green
|
||
|
||
return @{
|
||
Success = $true
|
||
Email = $email
|
||
Password = $newPassword
|
||
IsReset = $true
|
||
}
|
||
|
||
} catch {
|
||
Write-Host " ✗ Erreur lors de la réinitialisation du mot de passe : $_" -ForegroundColor Red
|
||
return @{
|
||
Success = $false
|
||
Email = $email
|
||
Password = $null
|
||
IsReset = $false
|
||
}
|
||
}
|
||
}
|
||
|
||
function Creer-Utilisateur {
|
||
param (
|
||
[string]$prenom,
|
||
[string]$nom,
|
||
[string]$email,
|
||
[string]$domain = "h3campus.fr",
|
||
[string]$usageLocation = "FR"
|
||
)
|
||
|
||
$password = Generer-MotDePasse
|
||
|
||
$PasswordProfile = @{
|
||
Password = $password
|
||
ForceChangePasswordNextSignIn = $true
|
||
}
|
||
|
||
# Générer un mailNickname valide
|
||
$mailNickname = ($email -split '@')[0] -replace '[^a-zA-Z0-9]', ''
|
||
if ([string]::IsNullOrEmpty($mailNickname)) {
|
||
$mailNickname = "user" + (Get-Random -Minimum 1000 -Maximum 9999)
|
||
}
|
||
|
||
try {
|
||
# Vérifier si l'utilisateur existe déjà
|
||
$existingUser = Get-MgUser -Filter "UserPrincipalName eq '$email'" -ErrorAction SilentlyContinue
|
||
|
||
if ($existingUser) {
|
||
Write-Host "L'utilisateur $email existe déjà." -ForegroundColor Yellow
|
||
return @{
|
||
Success = $true
|
||
Email = $email
|
||
Password = $null
|
||
IsNewUser = $false
|
||
UserId = $existingUser.Id
|
||
}
|
||
}
|
||
|
||
Write-Host "Création de l'utilisateur : $email ..." -ForegroundColor Yellow
|
||
|
||
$newUser = New-MgUser -DisplayName "$prenom $nom" `
|
||
-GivenName $prenom `
|
||
-Surname $nom `
|
||
-UserPrincipalName $email `
|
||
-MailNickname $mailNickname `
|
||
-PasswordProfile $PasswordProfile `
|
||
-AccountEnabled:$true `
|
||
-UsageLocation $usageLocation
|
||
|
||
if ($null -ne $newUser) {
|
||
Write-Host " ✓ Utilisateur créé avec succès" -ForegroundColor Green
|
||
Write-Host "Attribution des licences..." -ForegroundColor Yellow
|
||
|
||
# Attribuer toutes les licences
|
||
$licenseSuccess = Attribuer-Licences -userId $newUser.Id
|
||
|
||
if ($licenseSuccess) {
|
||
return @{
|
||
Success = $true
|
||
Email = $email
|
||
Password = $password
|
||
IsNewUser = $true
|
||
UserId = $newUser.Id
|
||
}
|
||
} else {
|
||
Write-Host " ⚠ Utilisateur créé mais erreur lors de l'attribution des licences" -ForegroundColor Yellow
|
||
return @{
|
||
Success = $true
|
||
Email = $email
|
||
Password = $password
|
||
IsNewUser = $true
|
||
UserId = $newUser.Id
|
||
}
|
||
}
|
||
} else {
|
||
Write-Host " ✗ Erreur : L'utilisateur $email n'a pas pu être créé." -ForegroundColor Red
|
||
}
|
||
|
||
} catch {
|
||
Write-Host " ✗ Erreur lors de la création de l'utilisateur : $_" -ForegroundColor Red
|
||
}
|
||
|
||
return @{
|
||
Success = $false
|
||
Email = $email
|
||
Password = $null
|
||
IsNewUser = $false
|
||
UserId = $null
|
||
}
|
||
}
|
||
|
||
# BOUCLE PRINCIPALE MODIFIÉE
|
||
foreach ($etudiant in $etudiants) {
|
||
$prenom = $etudiant.Prenom
|
||
$nom = $etudiant.Nom
|
||
|
||
Write-Host "`n========================================" -ForegroundColor Cyan
|
||
Write-Host "Traitement de l'étudiant : $prenom $nom" -ForegroundColor Cyan
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
|
||
# Normaliser les chaînes et les convertir en minuscules
|
||
$prenomuser = (Normalize-String $prenom).ToLower()
|
||
$nomuser = (Normalize-String $nom).ToLower()
|
||
$domainuser = $domain.ToLower()
|
||
|
||
$Usermail = "$($prenomuser.substring(0,1)).$($nomuser)@$($domainuser)"
|
||
$AddressExist = Adresse-Existe $Usermail
|
||
|
||
if ($AddressExist) {
|
||
Write-Host "Adresse mail $Usermail existante !" -ForegroundColor Blue
|
||
|
||
# Appliquer le comportement défini pour les comptes existants
|
||
if ($ComportementComptesExistants -eq "ReinitializerMotDePasse") {
|
||
Write-Host "Réinitialisation du mot de passe en cours..." -ForegroundColor Yellow
|
||
$resetResult = Reinitialiser-MotDePasse -email $Usermail
|
||
|
||
if ($resetResult.Success) {
|
||
# Envoyer un email avec le nouveau mot de passe
|
||
Envoyer-Email -destinataire $etudiant.Email -login $resetResult.Email -password $resetResult.Password -isReset $true
|
||
Write-Host "E-mail de réinitialisation envoyé à $($etudiant.Email)" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "Échec de la réinitialisation du mot de passe pour $Usermail" -ForegroundColor Red
|
||
}
|
||
} else {
|
||
Write-Host "Compte existant ignoré (aucune action effectuée)" -ForegroundColor Gray
|
||
}
|
||
|
||
} else {
|
||
Write-Host "Création d'un nouvel utilisateur avec l'adresse $Usermail" -ForegroundColor Yellow
|
||
|
||
# Créer l'utilisateur dans Office 365
|
||
$result = Creer-Utilisateur -prenom $prenom -nom $nom -email $Usermail -domain $domainuser
|
||
|
||
# Vérifier si l'adresse a bien été créée
|
||
if ($result.Success) {
|
||
# Envoyer un email avec les informations de connexion
|
||
Envoyer-Email -destinataire $etudiant.Email -login $result.Email -password $result.Password
|
||
Write-Host "E-mail envoyé à $($etudiant.Email) pour le compte $($result.Email)" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "Erreur : Le compte $($result.Email) n'a pas été créé. Aucun e-mail envoyé." -ForegroundColor Red
|
||
}
|
||
}
|
||
|
||
# Faire une pause de 10 secondes
|
||
Write-Host "`nPause de 10 secondes avant le traitement du prochain étudiant..." -ForegroundColor Gray
|
||
Start-Sleep -Seconds 10
|
||
}
|
||
|
||
Write-Host "`n========================================" -ForegroundColor Green
|
||
Write-Host "Traitement terminé pour tous les étudiants" -ForegroundColor Green
|
||
Write-Host "========================================" -ForegroundColor Green
|
||
|
||
# Arrêt du log
|
||
Stop-Transcript |