Detectar administradores locales no autorizados
Revisa el grupo de Administradores locales y compara contra una lista de cuentas permitidas. Con exit 1 dispara la corrección que quita a los demás.
Ejemplo en México
Casa de bolsa en Ciudad de México, 150 equipos con auditoría trimestral
Auditoría pide evidencia de quién tiene permisos de administrador local en las computadoras de operadores.
detect-local-admin-drift.ps1El reporte muestra en qué equipos hay cuentas extra y con eso responde a auditoría.
Cómo implementarlo
- Antes de subirlo, edita la lista $AllowedNames del script (por defecto solo "LocalAdmin") y agrega el nombre de tu cuenta administradora local, incluida la que administra LAPS. Haz lo mismo en el script de corrección.
- En el centro de administración de Microsoft Intune ve a Dispositivos > Administrar dispositivos > Scripts y correcciones (Scripts and remediations) > Remediations > Crear paquete de scripts.
- En Configuración sube detect-local-admin-drift.ps1 como script de detección y remediate-local-admin-drift.ps1 como script de corrección.
- Deja desactivadas "Ejecutar con las credenciales del usuario" y "Aplicar verificación de firma", asígnalo primero a un grupo piloto y elige la frecuencia.
- Revisa en el paquete qué equipos tienen miembros no autorizados.
Ver el código completo de detect-local-admin-drift.ps1
<#
.TITLE
Local Admin Drift Detection Script
.SYNOPSIS
Detects unauthorized members of the local Administrators group.
.DESCRIPTION
Enumerates the local Administrators group and compares every member against an
allowlist of approved accounts and well-known SIDs (built-in Administrator, the
Entra-joined device admin roles, and configurable extra entries). Returns exit
code 1 when unauthorized members are present, triggering the paired remediation
that removes them. This catches technician accounts, self-elevation leftovers,
and helpdesk additions that were never cleaned up.
.TAGS
Remediation,Detection
.REMEDIATIONTYPE
Detection
.PAIRSCRIPT
remediate-local-admin-drift.ps1
.PLATFORM
Windows
.MINROLE
Intune Service Administrator
.PERMISSIONS
DeviceManagementManagedDevices.ReadWrite.All
.AUTHOR
Ugur Koc
.VERSION
1.0
.CHANGELOG
1.0 - Initial release
.LASTUPDATE
2026-07-20
.EXAMPLE
.\detect-local-admin-drift.ps1
Returns exit 1 if the local Administrators group contains unauthorized members
.NOTES
- Runs in SYSTEM context via Intune Remediations
- Keep $AllowedNames in sync with the remediation script; add your LAPS-managed admin account name there
- The built-in Administrator (RID 500) and the Entra device administrator role SIDs (S-1-12-1-...) are always allowed
- Uses ADSI enumeration because Get-LocalGroupMember fails on orphaned SIDs
#>
$ErrorActionPreference = "Stop"
# Account names that are allowed local admins (keep in sync with remediation script)
$AllowedNames = @(
"LocalAdmin"
)
function Get-AdministratorsGroupMember {
# ADSI enumeration handles orphaned SIDs that break Get-LocalGroupMember
$adminGroupSid = "S-1-5-32-544"
$group = [ADSI]"WinNT://./$((Get-LocalGroup -SID $adminGroupSid).Name),group"
$members = @($group.Invoke("Members")) | ForEach-Object {
$path = ([ADSI]$_).InvokeGet("AdsPath")
$name = ([ADSI]$_).InvokeGet("Name")
$sid = $null
try {
$sidBytes = ([ADSI]$_).InvokeGet("objectSID")
$sid = (New-Object System.Security.Principal.SecurityIdentifier($sidBytes, 0)).Value
}
catch {
# Orphaned entries may not expose a SID - fall back to the path
}
[PSCustomObject]@{ Name = $name; Path = $path; Sid = $sid }
}
return @($members)
}
function Test-AllowedMember {
param([object]$Member)
# Built-in Administrator account (RID 500) is always allowed
if ($Member.Sid -and $Member.Sid -match "-500$") { return $true }
# Entra role SIDs (Global Administrator / Entra ID Joined Device Local Admin)
# are provisioned by the join process and always allowed
if ($Member.Sid -and $Member.Sid -like "S-1-12-1-*") { return $true }
# Domain Admins / Enterprise Admins for hybrid-joined devices
if ($Member.Sid -and ($Member.Sid -match "-512$" -or $Member.Sid -match "-519$")) { return $true }
if ($AllowedNames -contains $Member.Name) { return $true }
return $false
}
try {
$members = Get-AdministratorsGroupMember
$unauthorized = @($members | Where-Object { -not (Test-AllowedMember -Member $_) })
if ($unauthorized.Count -eq 0) {
Write-Output "Local Administrators group contains only approved members ($($members.Count) total)."
exit 0
}
$names = ($unauthorized | ForEach-Object { $_.Name }) -join ", "
Write-Output "Unauthorized local administrators found: $names"
exit 1
}
catch {
Write-Error $_
exit 2
}
# Copia sin cambios de https://github.com/ugurkocde/IntuneAutomation (commit 797bb2e).
# MIT License
#
# Copyright (c) 2025 Ugur Koc | Microsoft MVP
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.