Actualiser Compte_Office.ps1
This commit is contained in:
@@ -7,27 +7,32 @@ 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
|
||||
# => Domaine utilisé pour les adresses email
|
||||
#$domain = "h3hitema.fr"
|
||||
$domain = "h3campus.fr"
|
||||
|
||||
# Paramètres pour Office 365
|
||||
# => 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éfinir les codes de couleur
|
||||
$red = "e[31m"
|
||||
$green = "e[32m"
|
||||
$reset = "`e[0m"
|
||||
# => Définition du comportement pour les comptes existants
|
||||
# Valeurs possibles : "IgnorerExistants" ou "ReinitializerMotDePasse"
|
||||
$ComportementComptesExistants = "IgnorerExistants"
|
||||
|
||||
# Charger les informations des étudiants à partir du fichier CSV
|
||||
# => 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
|
||||
@@ -139,31 +144,174 @@ function Envoyer-Email {
|
||||
param (
|
||||
[string]$destinataire,
|
||||
[string]$login,
|
||||
[string]$password
|
||||
[string]$password,
|
||||
[bool]$isReset = $false
|
||||
)
|
||||
|
||||
# Contenu de l'email avec mise en forme HTML
|
||||
$subject = "Compte - $domain"
|
||||
# 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 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>
|
||||
<p>Bonjour,</p>
|
||||
<p>Ci-dessous vous trouverez vos informations de connexion pour accéder à votre compte Teams :</p>
|
||||
<table style='border-collapse: collapse;'>
|
||||
<tr>
|
||||
<th style='border: 1px solid black; padding: 8px;'>Login</th>
|
||||
<th style='border: 1px solid black; padding: 8px;'>Mot de passe</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style='border: 1px solid black; padding: 8px;'>$login</td>
|
||||
<td style='border: 1px solid black; padding: 8px;'>$password</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="email-container">
|
||||
<div class="header">
|
||||
<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>
|
||||
|
||||
<p>Un changement du mot de passe vous sera demandé à la première connexion.</p>
|
||||
<p><b>Note:</b>Vous pouvez accéder à votre boite e-mail en vous rendant sur le site: https://www.office.com</p></br>
|
||||
<p>Veuillez conserver ces informations en lieu sûr.</p><br>
|
||||
<p>Cordialement,<br>Service Informatique</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>
|
||||
"@
|
||||
@@ -182,9 +330,91 @@ $mailMessage.IsBodyHtml = $true
|
||||
|
||||
try {
|
||||
$smtpClient.Send($mailMessage)
|
||||
Write-Host "Email envoyé avec succès à $destinataire"
|
||||
Write-Host "Email envoyé avec succès à $destinataire" -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host "Échec de l'envoi de l'email à $destinataire : $"
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +451,7 @@ Success = $true
|
||||
Email = $email
|
||||
Password = $null
|
||||
IsNewUser = $false
|
||||
UserId = $existingUser.Id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,30 +467,36 @@ $newUser = New-MgUser -DisplayName "$prenom $nom" `
|
||||
-UsageLocation $usageLocation
|
||||
|
||||
if ($null -ne $newUser) {
|
||||
$exchangeLicense = Get-MgSubscribedSku | Where-Object SkuPartNumber -eq 'EXCHANGESTANDARD_STUDENT'
|
||||
Write-Host " ✓ Utilisateur créé avec succès" -ForegroundColor Green
|
||||
Write-Host "Attribution des licences..." -ForegroundColor Yellow
|
||||
|
||||
if ($null -ne $exchangeLicense) {
|
||||
Set-MgUserLicense -UserId $newUser.Id `
|
||||
-AddLicenses @{SkuId = $exchangeLicense.SkuId} `
|
||||
-RemoveLicenses @()
|
||||
|
||||
Write-Host "Licence Exchange Online for Students attribuée à $email" -ForegroundColor Green
|
||||
# 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 "Erreur : Licence Exchange Online for Students non trouvée" -ForegroundColor Red
|
||||
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
|
||||
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
|
||||
Write-Host " ✗ Erreur lors de la création de l'utilisateur : $_" -ForegroundColor Red
|
||||
}
|
||||
|
||||
return @{
|
||||
@@ -267,14 +504,18 @@ Success = $false
|
||||
Email = $email
|
||||
Password = $null
|
||||
IsNewUser = $false
|
||||
UserId = $null
|
||||
}
|
||||
}
|
||||
# Parcourir chaque étudiant dans le fichier CSV et traiter
|
||||
|
||||
# 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()
|
||||
@@ -286,6 +527,23 @@ foreach ($etudiant in $etudiants) {
|
||||
|
||||
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
|
||||
|
||||
@@ -295,15 +553,18 @@ foreach ($etudiant in $etudiants) {
|
||||
# Vérifier si l'adresse a bien été créée
|
||||
if ($result.Success) {
|
||||
# Envoyer un email avec les informations de connexion
|
||||
$messageEnvoi = Envoyer-Email -destinataire $etudiant.Email -login $result.Email -password $result.Password
|
||||
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
|
||||
Write-Host $messageEnvoi -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 "Pause de 10 secondes avant le traitement du prochain étudiant..." -ForegroundColor Gray
|
||||
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
|
||||
Reference in New Issue
Block a user