Skip to content

About

Intune remediation scripts that remove unexpected local admins, show who added them, and warn when the same fix keeps coming back. MIT.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Local Admin Drift

Intune remediation scripts that remove unexpected members from the local Administrators group, and tell you when the same fix keeps coming back.

A remediation that fires every day is hiding a cause: an installer, a scheduled task or a helpdesk habit that puts the account back. These scripts fix the drift, count how often they've fixed it, and read event 4732 to show who added the account, so you can stop it at the source.

What it does

  • Lists members of the local Administrators group through ADSI, which doesn't fail on unresolvable SIDs the way Get-LocalGroupMember can
  • Always allows the built-in Administrator, Domain Admins and Entra roles and groups added at join, plus the accounts you list
  • Reads event 4732 to show who last added a member to Administrators (SYSTEM means a service, scheduled task or installer)
  • Counts the fixes from the last 7 days and says "Keeps coming back: find the cause or move this to a policy" after 3
  • Logs every check and fix to C:\ProgramData\Atliso\LocalAdminDrift.log, and writes event 1001 (source Atliso) to the Application log for each removal
  • Starts as a dry run. Nothing is removed until you set $WhatIf = $false

The files

File What it's for
Detect-LocalAdminDrift.ps1 Detection script. Exit 0 clean, exit 1 drift found. Read-only.
Remediate-LocalAdminDrift.ps1 Remediation script. Removes members who aren't allowed.
Invoke-AtlisoRemediation.ps1 Runner for devices without Windows Enterprise: detect, fix, detect again.
Install-LocalAdminDrift.ps1 One Intune platform script that installs all three and an hourly SYSTEM task.

Configuration

Set the same allow list in the detection and remediation scripts (or once in the installer):

$AllowedMembers = @('it-admin')   # accounts that may stay in local Administrators (names, not SIDs)
$WhatIf         = $true           # $true = log what would be removed, change nothing. $false = remove.

Run it as a dry run first. The log shows a WHATIF would remove line for each account it would take out. When that list holds only accounts you want gone, set $WhatIf = $false. While it's a dry run, the runner logs STILL-FAILING after each check, because nothing was removed.

Deploy as an Intune Remediation (Windows Enterprise E3/E5, Education A3/A5 or VDA)

  1. Intune admin center > Devices > Manage devices > Scripts and remediations > Create.
  2. Upload Detect-LocalAdminDrift.ps1 as the detection script and Remediate-LocalAdminDrift.ps1 as the remediation script. Upload the files rather than pasting, so the encoding stays intact.
  3. Leave Run this script using the logged-on credentials off. It needs SYSTEM to change group membership. Run in 64-bit PowerShell.
  4. Assign it to a device group on an hourly or daily schedule.
  5. After a day, check the detection output for devices that say Keeps coming back.

Deploy without Windows Enterprise (Business Premium, Intune Plan 1)

Remediations need Windows Enterprise licensing. Platform scripts don't, but they run once, so the installer sets up a scheduled task that does the hourly check.

  1. Edit the CONFIG block at the top of Install-LocalAdminDrift.ps1.
  2. Intune admin center > Devices > Manage devices > Scripts and remediations > Platform scripts > Add > Windows 10 and later.
  3. Upload the installer. Run as SYSTEM (logged-on credentials off), signature check off, 64-bit on.
  4. Assign it to a device group. Change the installer whenever you change the config, so Intune runs it again.

The installer locks C:\ProgramData\Atliso\Remediations\LocalAdminDrift so only SYSTEM and Administrators can change the scripts. A folder users can write to, run by SYSTEM every hour, would be a free route to SYSTEM.

To remove it: Unregister-ScheduledTask -TaskPath '\Atliso\' -TaskName 'Atliso - Local admin drift' and delete the folder.

What it looks like

Detection finding drift, with who added the account:

Detection finds an unexpected admin

Event 4732 (who added it) next to event 1001 (the fix):

Event 4732 and fix events

A day of the log when something keeps putting the account back:

Log showing the fix every two hours

When a script isn't the answer

If the count keeps climbing, find what adds the account and stop it, then enforce the group with a policy (Intune account protection, local user group membership) instead of correcting it every hour.

Changing the scripts

Install-LocalAdminDrift.ps1 is generated. Edit the three scripts or build/Install.template.ps1, then run .\build\Build-Installer.ps1. The tests fail if the installer is out of date.

Tests: Invoke-Pester ./tests in an elevated session. They create a throwaway local admin, then detect it, dry-run, remove it and confirm.

Requirements

  • Windows 10 or 11
  • Windows PowerShell 5.1 (what Intune uses)
  • Runs as SYSTEM

Full guide

The why, the event 4732 trick and the licensing detail: https://atliso.com/blog/intune-remediations-recurring-issues

License

MIT. Free to use, change, and ship. See LICENSE.

About

Intune remediation scripts that remove unexpected local admins, show who added them, and warn when the same fix keeps coming back. MIT.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages