Showing posts with label Windows Server. Show all posts
Showing posts with label Windows Server. Show all posts

Thursday, April 17, 2025

๐Ÿงผ Cleanup: Invalid SID Members in Local Groups (Windows Server Maintenance)

๐Ÿงผ Cleanup: Invalid SID Members in Local Groups (Windows Server Maintenance)

Overview

This PowerShell script scans all local groups for orphaned (invalid) SIDs — entries left behind after a user or service account has been deleted. These show as raw SIDs (e.g., S-1-5-21-...) in group membership.

We use NinjaOne to deploy this as a recurring cleanup task across servers.

๐Ÿ”ง What It Does

  • Enumerates all local groups
  • Identifies broken/unresolvable SID entries
  • Removes only invalid entries
  • Outputs all actions to NinjaOne

๐Ÿ›  PowerShell Script

PowerShell Script

$badEntries = @()

$localGroups = Get-LocalGroup | Select-Object -ExpandProperty Name

foreach ($groupName in $localGroups) {
    Write-Output "`nProcessing group: $groupName"
    $badSIDs = @()

    $output = powershell -Command "Get-LocalGroupMember -Group '$groupName'" 2>&1

    foreach ($line in $output) {
        if ($line -match "SID '([^']+)'") {
            $badSIDs += $matches[1]
        }
    }

    if ($badSIDs.Count -gt 0) {
        $group = [ADSI]"WinNT://./$groupName,group"
        foreach ($sid in $badSIDs | Select-Object -Unique) {
            $path = "WinNT://$sid"
            try {
                $group.Remove($path)
                Write-Output "Removed: $sid from $groupName"
                $badEntries += [PSCustomObject]@{ Group = $groupName; SID = $sid; Status = "Removed" }
            } catch {
                Write-Warning "Failed to remove: $sid from $groupName"
                $badEntries += [PSCustomObject]@{ Group = $groupName; SID = $sid; Status = "Failed" }
            }
        }
    } else {
        Write-Output "No orphaned SIDs found in $groupName"
    }
}

$badEntries | Format-Table
        

๐Ÿงช Example Output

Processing group: Administrators
Removed: S-1-5-21-...-6274 from Administrators

Processing group: Users
Removed: S-1-5-21-...-6835 from Users
Removed: S-1-5-21-...-13465 from Users
    

๐Ÿ”’ Safety

  • Only removes entries matching S-1-5-21-*
  • Only removes if the SID causes an error in Get-LocalGroupMember
  • Leaves all valid users/groups untouched


Tuesday, June 27, 2017

Replacement Motherboard causes DC server to not allow login.

Recently I had a server that had to have a motherboard replaced. It was a Dell server for anyone who cares. I won't go into the drama around this issue, but basically the Windows Server 2012 was forcing the time on the server. We thought it was the motherboard BIOS doing it but actually it was the Windows OS.

The main issue here is the would reboot, and the time would be January, 1, 1981, or some weird number.

Needless to say since this was a Domain Controller and the time from when it was last on and the new time are soooo off, it would not let you log in.

To fix this, just boot into safe mode, log in, change the time and then reboot the system.

Problem solved.

Server 2012 R2 Evaluation won't register

Building new server you run into things sometimes that are annoying or weird. One of those can registration and activation. 

Building some new 2012 R2 Standard server and I couldn't register the server. It kept saying the activation code was wrong. 

Well there is a VERY easy fix for this. 

I found it here.
https://social.technet.microsoft.com/Forums/windows/en-US/4211c642-b15d-49ea-8124-f0628aa0f92e/activate-windows-server-2012-evaluation-standard-version-with-a-product-key-oem?forum=winserver8gen&prof=required

Run this code to change it from evaluation.

DISM /online /Set-Edition:<edition ID> /ProductKey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX /AcceptEula


Change <edition ID> to the edition you have the licence for eg.

DISM /online /Set-Edition:ServerStandard /ProductKey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX /AcceptEula

You will need to reboot, so keep that in mind. 

The one thing that the source where i found this doesn't tell you is, you still need to activate your server with your key. 

So go to the Server Manager > Local Server > Properties > Click Activate next to Product ID > Enter your information.

Enjoy.