Production
This commit is contained in:
+170
-14
@@ -1,27 +1,183 @@
|
||||
function GetContactsFromListBrevo {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$IdListe
|
||||
)
|
||||
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"api-key" = "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"
|
||||
if (-not $Global:ApiKey) {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
|
||||
}
|
||||
|
||||
$response = Invoke-WebRequest `
|
||||
-Uri "https://api.brevo.com/v3/contacts/lists/$($IdListe)/contacts?limit=50&offset=0&sort=desc" `
|
||||
-Method GET `
|
||||
-Headers $headers
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"api-key" = $Global:ApiKey
|
||||
}
|
||||
|
||||
return $response.Content | ConvertFrom-Json
|
||||
try {
|
||||
$response = Invoke-WebRequest `
|
||||
-Uri "https://api.brevo.com/v3/contacts/lists/$($IdListe)/contacts?limit=50&offset=0&sort=desc" `
|
||||
-Method GET `
|
||||
-Headers $headers
|
||||
|
||||
$data = $response.Content | ConvertFrom-Json
|
||||
|
||||
return [PSCustomObject]@{
|
||||
Success = $true
|
||||
Value = $data
|
||||
}
|
||||
}
|
||||
catch {
|
||||
return [PSCustomObject]@{
|
||||
Success = $false
|
||||
Value = "Erreur lors de la récupération des contacts de la liste $IdListe : $($_.Exception.Message)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# $contacts = GetContactsFromListBrevo
|
||||
# $contacts.count
|
||||
# $contacts.contacts | ForEach-Object { $_.email }
|
||||
function AddContactToBrevo{
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Email
|
||||
)
|
||||
|
||||
if (-not $Global:ApiKey) {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
|
||||
}
|
||||
|
||||
# foreach ($email in $contacts.contacts) {
|
||||
# Write-Output $email.email
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"content-type" = "application/json"
|
||||
"api-key" = $Global:ApiKey
|
||||
}
|
||||
|
||||
$Body = @{
|
||||
email = $Email
|
||||
} | ConvertTo-Json -Depth 3
|
||||
|
||||
try {
|
||||
$response = Invoke-WebRequest -Uri 'https://api.brevo.com/v3/contacts' `
|
||||
-Method POST `
|
||||
-Headers $headers `
|
||||
-ContentType 'application/json' `
|
||||
-Body $Body
|
||||
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
|
||||
}
|
||||
catch {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de l'ajout de l'utilisateur $Email : $($_.Exception.Message)" }
|
||||
}
|
||||
}
|
||||
|
||||
function AddContactBrevoToList{
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Email,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$IdListe
|
||||
)
|
||||
|
||||
if (-not $Global:ApiKey) {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
|
||||
}
|
||||
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"content-type" = "application/json"
|
||||
"api-key" = $Global:ApiKey
|
||||
}
|
||||
|
||||
$Body = @{ emails = @($Email) } | ConvertTo-Json -Depth 3
|
||||
|
||||
try {
|
||||
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/lists/$IdListe/contacts/add" `
|
||||
-Method POST `
|
||||
-Headers $headers `
|
||||
-ContentType 'application/json' `
|
||||
-Body $Body
|
||||
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
|
||||
}
|
||||
catch {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de l'ajout de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
|
||||
}
|
||||
}
|
||||
|
||||
function RemoveContactFromListBrevo{
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Email,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$IdListe
|
||||
)
|
||||
|
||||
if (-not $Global:ApiKey) {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
|
||||
}
|
||||
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"content-type" = "application/json"
|
||||
"api-key" = $Global:ApiKey
|
||||
}
|
||||
|
||||
$Body = @{ emails = @($Email) } | ConvertTo-Json -Depth 3
|
||||
|
||||
try {
|
||||
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/lists/$IdListe/contacts/remove" `
|
||||
-Method POST `
|
||||
-Headers $headers `
|
||||
-ContentType 'application/json' `
|
||||
-Body $Body
|
||||
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
|
||||
}
|
||||
catch {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de la suppresion de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
|
||||
}
|
||||
}
|
||||
|
||||
function RemoveContactFromBrevo{
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Email
|
||||
)
|
||||
|
||||
if (-not $Global:ApiKey) {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "La clé API n'est pas définie" }
|
||||
}
|
||||
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"content-type" = "application/json"
|
||||
"api-key" = $Global:ApiKey
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Invoke-WebRequest -Uri "https://api.brevo.com/v3/contacts/$Email" `
|
||||
-Method DELETE `
|
||||
-Headers $headers `
|
||||
-ContentType 'application/json'
|
||||
return [PSCustomObject]@{ Success = $true; Value = $response.Content }
|
||||
}
|
||||
catch {
|
||||
return [PSCustomObject]@{ Success = $false; Value = "Erreur lors de la suppresion de l'utilisateur $Email à la liste $IdListe : $($_.Exception.Message)" }
|
||||
}
|
||||
}
|
||||
|
||||
#$test = AddContactToBrevo -Email "tris.ta@esadhar.fr"
|
||||
#$test = AddContactBrevoToList -Email "tris.ta@esadhar.fr" -IdListe 9
|
||||
#$test = RemoveContactFromListBrevo -Email "tris.ta@esadhar.fr" -IdListe 9
|
||||
#$test = RemoveContactFromBrevo -Email "toto.tata@esadhar.fr"
|
||||
|
||||
|
||||
# if (-not $test.Success)
|
||||
# {
|
||||
# Write-Output $test.Value -Level ERROR
|
||||
# Write-Output "error"
|
||||
# }
|
||||
# else{
|
||||
# Write-Output $test.Value
|
||||
# Write-Output "ok"
|
||||
# }
|
||||
+23
-5
@@ -3,18 +3,36 @@
|
||||
Service de logging pour les scripts PowerShell.
|
||||
.DESCRIPTION
|
||||
Fournit une fonction Write-Log qui logge des messages avec timestamp, niveau et fichier de log.
|
||||
Ajoute une rotation automatique par jour et supprime les logs vieux de plus de 5 jours.
|
||||
#>
|
||||
|
||||
# Définir le fichier de log globalement pour ce module
|
||||
$Global:LogFile = ".\app.log"
|
||||
# Définir le dossier de logs et le nom de fichier de base
|
||||
$Global:LogFolder = ".\logs"
|
||||
$Global:LogBaseName = "app"
|
||||
|
||||
# Crée le dossier de logs s'il n'existe pas
|
||||
if (-not (Test-Path $Global:LogFolder)) {
|
||||
New-Item -Path $Global:LogFolder -ItemType Directory | Out-Null
|
||||
}
|
||||
|
||||
# Nom du fichier de log du jour
|
||||
$Global:LogFile = Join-Path $Global:LogFolder "$($Global:LogBaseName)_$(Get-Date -Format 'yyyy-MM-dd').log"
|
||||
|
||||
# Supprimer les fichiers de log vieux de plus de 5 jours
|
||||
$logFiles = Get-ChildItem -Path $Global:LogFolder -Filter "$($Global:LogBaseName)_*.log" -File
|
||||
foreach ($file in $logFiles) {
|
||||
if ($file.CreationTime -lt (Get-Date).AddDays(-5)) {
|
||||
Remove-Item $file.FullName -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
function Write-Log {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Message,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ValidateSet("INFO", "WARN", "ERROR")]
|
||||
[string]$Level = "INFO"
|
||||
)
|
||||
@@ -26,7 +44,7 @@ function Write-Log {
|
||||
# Affiche à la console
|
||||
Write-Output $line
|
||||
|
||||
# Écrit dans le fichier de log
|
||||
# Écrit dans le fichier de log du jour
|
||||
try {
|
||||
Add-Content -Path $Global:LogFile -Value $line
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user