LaBouaBouate

Lister les permissions LogonAsBatch et LogonAsService en PowerShell

Permissions

Les permissions LogonAsBatch (SeBatchLogonRight) et LogonAsService (SeServiceLogonRight) sont des droits d’ouverture de session spéciaux dans Windows, attribués à des comptes utilisateurs ou groupes.

Récupération en PowerShell

Obtention des informations de SECPOL

Les informations pour ces permissions sont stockées dans la stratégie de sécurité locale (SECPOL), qui peut être exporté vers un fichier CFG.

Créer un dossier C:\temp\ :

mkdir C:\temp -Force

Exporter la stratégie de sécurité locale vers un fichier CFG :

secedit /export /cfg C:\temp\secpol.cfg

Extraction des informations

Parser la stratégie locale pour obtenir l’information :

$cfg = Get-Content -Path 'C:\temp\secpol.cfg'
$batchLogon = (($cfg | Select-String 'SeBatchLogonRight') -split '=')[-1]
$serviceLogon = (($cfg | Select-String 'SeServiceLogonRight') -split '=')[-1]

Traduction des SID et affichage des résultats :

Write-Host "Log on as a batch job" -ForegroundColor Yellow
$batchLogon = $batchLogon.Replace('*', '').Trim()
$batchLogon -split ',' | ForEach-Object {
    $SID = [System.Security.Principal.SecurityIdentifier]::New($_)
    ($SID.Translate([System.Security.Principal.NTAccount])).Value
}

Write-Host "Log on as a service" -ForegroundColor Yellow
$serviceLogon = $serviceLogon.Replace('*', '').Trim()
$serviceLogon -split ',' | ForEach-Object {
    $SID = [System.Security.Principal.SecurityIdentifier]::New($_)
    ($SID.Translate([System.Security.Principal.NTAccount])).Value
}
Photo de profil
Article écrit par Léo Bouard

Ingénieur systèmes spécialisé dans les infrastructures, les technologies et l'écosystème Microsoft. Je travaille depuis plus de cinq ans sur les questions de gestion d'identité, d'outils collaboratifs et d'automatisation.

Cet article pourrait-il vous intéresser ?