Untitled

 avatar
unknown
plain_text
25 days ago
1.9 kB
4
Indexable
Function ConvertTo-LogonHoursBytes {
param (
[array]$logonHoursArray
)
if ($logonHoursArray.Length -ne 168) {
throw "The logonHours array must contain exactly 168 bits (24 hours x 7 days)."
}
$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 "Applying restrictions for group $groupName..." -ForegroundColor Cyan
try {
$logonHoursBytes = ConvertTo-LogonHoursBytes -logonHoursArray $logonHours
} catch {
Write-Host "Error: $" -ForegroundColor Red
return
}
try {
$users = Get-ADGroupMember -Identity $groupName -Recursive | Where-Object { $.objectClass -eq "user" }
} catch {
Write-Host "Error: Unable to retrieve members of group $groupName. Verify the group exists." -ForegroundColor Red
return
}
if (-not $users) {
Write-Host "No users found in group $groupName." -ForegroundColor Yellow
return
}
$users | ForEach-Object {
Write-Host "  Applying for user: $($.SamAccountName)" -ForegroundColor Yellow
try {
Set-ADUser -Identity $.SamAccountName -Replace @{logonHours=$logonHoursBytes}
} catch {
Write-Host "Error: Unable to apply restrictions for user $($_.SamAccountName)." -ForegroundColor Red
}
}
}
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 "Login hour restrictions applied successfully!" -ForegroundColor Green
Editor is loading...
Leave a Comment