Untitled
unknown
plain_text
a year ago
4.2 kB
10
Indexable
Function ConvertTo-LogonHoursBytes {
param (
[array]$logonHoursArray
)
if ($logonHoursArray.Length -ne 168) {
throw "Le tableau logonHours doit contenir exactement 168 bits (24 heures x 7 jours)."
}
$byteArray = @()
for ($i = 0; $i -lt 21; $i++) {
$byte = 0
for ($j = 0; $j -lt 8; $j++) {
$byte += $logonHoursArray[$i * 8 + $j] * [math]::Pow(2, 7 - $j)
}
$byteArray += [byte]$byte
}
return ,$byteArray
}
Function Set-LogonHours {
param (
[string]$groupName,
[array]$logonHours
)
Write-Host "Application des restrictions pour le groupe $groupName..." -ForegroundColor Cyan
# Vérification de l'existence du groupe
try {
$group = Get-ADGroup -Identity $groupName -ErrorAction Stop
} catch {
Write-Host "Erreur : Le groupe $groupName n'existe pas." -ForegroundColor Red
return
}
# Convertir les heures de connexion en tableau de bytes
try {
$logonHoursBytes = ConvertTo-LogonHoursBytes -logonHoursArray $logonHours
} catch {
Write-Host "Erreur : Impossible de convertir les heures de connexion." -ForegroundColor Red
return
}
# Récupérer les utilisateurs du groupe
try {
$users = Get-ADGroupMember -Identity $groupName -Recursive | Where-Object { $_.objectClass -eq "user" }
} catch {
Write-Host "Erreur : Impossible de récupérer les membres du groupe $groupName. Vérifiez que le groupe existe." -ForegroundColor Red
return
}
if (-not $users) {
Write-Host "Aucun utilisateur trouvé dans le groupe $groupName." -ForegroundColor Yellow
return
}
# Appliquer les restrictions aux utilisateurs du groupe
$users | ForEach-Object {
Write-Host " Application des restrictions pour l'utilisateur : $($_.SamAccountName)" -ForegroundColor Yellow
try {
Set-ADUser -Identity $_.SamAccountName -Replace @{logonHours=$logonHoursBytes}
} catch {
Write-Host "Erreur : Impossible d'appliquer les restrictions pour l'utilisateur $($_.SamAccountName)." -ForegroundColor Red
}
}
}
# Définir les heures de connexion pour les utilisateurs standard (8h-18h, lun-ven)
$timeRestrictionsStandard = @(
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Lundi (8h-18h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Mardi (8h-18h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Mercredi (8h-18h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Jeudi (8h-18h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Vendredi (8h-18h)
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, # Samedi (aucune connexion)
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 # Dimanche (aucune connexion)
)
# Définir les heures de connexion pour les utilisateurs IT (7h lundi - 23h samedi)
$timeRestrictionsIT = @(
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, # Lundi (7h-23h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, # Mardi (7h-23h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, # Mercredi (7h-23h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, # Jeudi (7h-23h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, # Vendredi (7h-23h)
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0, # Samedi (7h-23h)
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 # Dimanche (aucune connexion)
)
# Appliquer les restrictions pour chaque groupe
Set-LogonHours -groupName "GG-DIRECTION" -logonHours $timeRestrictionsStandard
Set-LogonHours -groupName "GG-RH" -logonHours $timeRestrictionsStandard
Set-LogonHours -groupName "GG-COMPTA" -logonHours $timeRestrictionsStandard
Set-LogonHours -groupName "GG-COMMERCIAUX" -logonHours $timeRestrictionsStandard
Set-LogonHours -groupName "GG-R&D" -logonHours $timeRestrictionsStandard
Set-LogonHours -groupName "GG-IT" -logonHours $timeRestrictionsIT
Write-Host "Les restrictions horaires ont été appliquées avec succès !" -ForegroundColor GreenEditor is loading...
Leave a Comment