<# .SYNOPSIS GGM Sign Enterprise - Agent de Déploiement & Synchronisation Signature Outlook .DESCRIPTION Ce script interroge le serveur GGM Sign, télécharge la signature officielle assignée à l'utilisateur connecté (avec campagne marketing active), génère les fichiers dans %APPDATA%\Microsoft\Signatures et configure Outlook par registre. Prêt pour déploiement GPO, Microsoft Intune ou Tâche Planifiée Windows. #> [CmdletBinding()] param ( [string]$ServerUrl = "https://pakit.gigamedia.net", [string]$UserEmail = "", [string]$SignatureName = "GGM_Sign_Officielle", [switch]$ForceDefault = $true, [switch]$Silent = $false ) $ErrorActionPreference = "Continue" $LogFile = "$env:TEMP\GGMSign-Outlook-Sync.log" function Write-Log { param([string]$Message, [string]$Level = "INFO") $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $line = "[$timestamp] [$Level] $Message" Add-Content -Path $LogFile -Value $line -ErrorAction SilentlyContinue if (-not $Silent) { switch ($Level) { "ERROR" { Write-Host $line -ForegroundColor Red } "SUCCESS" { Write-Host $line -ForegroundColor Green } "WARN" { Write-Host $line -ForegroundColor Yellow } Default { Write-Host $line -ForegroundColor Cyan } } } } Write-Log "=== Démarrage Synchronisation GGM Sign Outlook ===" # 1. Détection automatique de l'adresse email de l'utilisateur if ([string]::IsNullOrWhiteSpace($UserEmail)) { # Méthode A: AD System Info try { $adSysInfo = New-Object -ComObject ADSystemInfo $adUser = [ADSI]"LDAP://$($adSysInfo.UserName)" if ($adUser.mail) { $UserEmail = $adUser.mail.ToString() Write-Log "Email détecté via Active Directory: $UserEmail" } } catch { Write-Log "Impossible de lire Active Directory en direct: $($_.Exception.Message)" "WARN" } } if ([string]::IsNullOrWhiteSpace($UserEmail)) { # Méthode B: Outlook COM Object si ouvert try { $outlook = New-Object -ComObject Outlook.Application -ErrorAction Stop $accounts = $outlook.Session.Accounts if ($accounts.Count -gt 0) { $UserEmail = $accounts.Item(1).SmtpAddress Write-Log "Email détecté via profil Outlook: $UserEmail" } } catch { Write-Log "Outlook non joignable via COM: $($_.Exception.Message)" "WARN" } } if ([string]::IsNullOrWhiteSpace($UserEmail)) { # Méthode C: Nom d'utilisateur Windows $UserEmail = "$env:USERNAME@gigamedia.net" Write-Log "Utilisation du login Windows par défaut: $UserEmail" "WARN" } # 2. Appel API vers le serveur GGM Sign $apiUrl = "$ServerUrl/api/agent/signature?email=$([System.Web.HttpUtility]::UrlEncode($UserEmail))" if (-not ([System.Type]::GetType("System.Web.HttpUtility"))) { Add-Type -AssemblyName System.Web $apiUrl = "$ServerUrl/api/agent/signature?email=$([System.Web.HttpUtility]::UrlEncode($UserEmail))" } Write-Log "Interrogation de l'API GGM Sign: $apiUrl" try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13 $response = Invoke-RestMethod -Uri $apiUrl -Method Get -TimeoutSec 15 -Headers @{"Accept"="application/json"} } catch { Write-Log "Erreur lors de la récupération de la signature sur le serveur: $($_.Exception.Message)" "ERROR" exit 1 } if (-not $response -or -not $response.html_content) { Write-Log "Réponse invalide reçue du serveur GGM Sign." "ERROR" exit 1 } Write-Log "Signature récupérée avec succès pour $($response.user_name) ($($response.user_email))" "SUCCESS" # 3. Répertoire des signatures Outlook locales $sigDir = Join-Path $env:APPDATA "Microsoft\Signatures" if (-not (Test-Path $sigDir)) { New-Item -Path $sigDir -ItemType Directory -Force | Out-Null Write-Log "Création du dossier des signatures: $sigDir" } $htmFile = Join-Path $sigDir "$SignatureName.htm" $txtFile = Join-Path $sigDir "$SignatureName.txt" $rtfFile = Join-Path $sigDir "$SignatureName.rtf" # 4. Écriture des fichiers de signature try { # UTF8 avec BOM pour compatibilité moteur MS Word / Outlook $utf8NoBom = New-Object System.Text.UTF8Encoding($true) [System.IO.File]::WriteAllText($htmFile, $response.html_content, $utf8NoBom) Write-Log "Fichier HTM créé: $htmFile" "SUCCESS" # Fichier texte $plainText = if ($response.plain_text) { $response.plain_text } else { $response.user_name } [System.IO.File]::WriteAllText($txtFile, $plainText, [System.Text.Encoding]::UTF8) # Fichier RTF basique $rtfContent = "{\rtf1\ansi\deff0 {\fonttbl {\f0 Segoe UI;}}\f0\fs20 " + ($plainText -replace "\n", "\par ") + "}" [System.IO.File]::WriteAllText($rtfFile, $rtfContent, [System.Text.Encoding]::ASCII) } catch { Write-Log "Erreur lors de l'écriture des fichiers signatures: $($_.Exception.Message)" "ERROR" exit 1 } # 5. Configuration automatique du Registre Outlook if ($ForceDefault) { # Versions Outlook: 16.0 (2016/2019/2021/365), 15.0 (2013), 14.0 (2010) $officeVersions = @("16.0", "15.0", "14.0") foreach ($ver in $officeVersions) { $mailSettingsKey = "HKCU:\Software\Microsoft\Office\$ver\Common\MailSettings" if (Test-Path "HKCU:\Software\Microsoft\Office\$ver") { if (-not (Test-Path $mailSettingsKey)) { New-Item -Path $mailSettingsKey -Force | Out-Null } # Définir signature par défaut pour les nouveaux messages et les réponses Set-ItemProperty -Path $mailSettingsKey -Name "NewSignature" -Value $SignatureName -Type String -Force Set-ItemProperty -Path $mailSettingsKey -Name "ReplySignature" -Value $SignatureName -Type String -Force Write-Log "Clés Registre Outlook ($ver) configurées: NewSignature & ReplySignature = $SignatureName" "SUCCESS" # Désactiver le blocage Roaming Signature Microsoft 365 pour forcer la signature locale $setupKey = "HKCU:\Software\Microsoft\Office\$ver\Outlook\Setup" if (-not (Test-Path $setupKey)) { New-Item -Path $setupKey -Force | Out-Null } Set-ItemProperty -Path $setupKey -Name "DisableRoamingSignaturesTemporaryToggle" -Value 1 -Type DWord -Force } } } Write-Log "=== Synchronisation GGM Sign terminée avec succès ! ===" "SUCCESS" exit 0