By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: This PowerShell Script Watches Your Domain Admins Like a Hawk—Literally | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > This PowerShell Script Watches Your Domain Admins Like a Hawk—Literally | HackerNoon
Computing

This PowerShell Script Watches Your Domain Admins Like a Hawk—Literally | HackerNoon

News Room
Last updated: 2025/04/12 at 8:53 AM
News Room Published 12 April 2025
Share
SHARE

In every enterprise environment, Active Directory (AD) group memberships play a pivotal role in access control and security. Unauthorized modifications to privileged groups like “Domain Admins” or “Enterprise Admins” can expose your organization to risk. Enter ADHawk: a simple but powerful PowerShell script that watches over your AD group memberships like a hawk—hence the name.

ADHawk is designed to run as a scheduled task every five minutes, automatically logging changes and sending email alerts for any detected modifications. Lightweight, modular, and highly customizable, this tool is perfect for security-conscious sysadmins who want real-time visibility without deploying heavyweight monitoring solutions.


What ADHawk Monitors

By default, ADHawk keeps a close eye on the following critical groups:

"Domain Admins", "Exchange Admins", "Enterprise Admins",
"Schema Admins", "Backup Operators", "Account Operators", "Server Operators"

You can easily modify this list to suit your organizational structure.


How It Works

  1. Initialization:
    • Checks for a designated state directory (e.g., C:customappsadgroupmembers).
    • Creates it if it doesn’t exist.
  2. Group Enumeration:
    • Fetches the current members of each monitored group via Get-ADGroupMember.
    • Stores each group’s state as a sorted list of SamAccountNames.
  3. Change Detection:
    • Compares current membership against previously saved state.
    • Uses Compare-Object to identify additions or removals.
  4. Notification & Logging:
    • Constructs a detailed message with a timestamp and change summary.
    • Sends the alert via email.
    • Appends the event to a log file (change.log) for audit-ability.
  5. State Refresh:
    • Updates the stored file with the current membership snapshot.

Email Notification Setup (REQUIRED EDIT)

Before using ADHawk in your environment, you must customize the following parameters:

$EmailFrom = "[email protected]"
$EmailTo = "[email protected],[email protected]"
$SMTPServer = "Your SMTP server IP here"

These fields define where your email alerts come from, who receives them, and which SMTP server relays them.


Implementation Steps

  1. Prerequisites:
    • PowerShell with ActiveDirectory module installed
    • SMTP server configured and accessible
  2. Deployment:
    • Save the full script to a .ps1 file, e.g., ADHawk.ps1
    • Modify the group list or email fields as needed
    • Create the directory C:customappsadgroupmembers
  3. Scheduling the Task:
    • Open Task Scheduler
    • Create a new task:
      • Trigger: Every 5 minutes

      • Action: Run powershell.exe with argument:

        -ExecutionPolicy Bypass -File "C:PathToADHawk.ps1"
        
      • Run with highest privileges

  4. Testing:
    • Manually run the script and simulate changes by adding/removing users
    • Verify emails are sent and logs are updated

Why ADHawk?

  • Minimal Footprint: No agents or external dependencies
  • Real-Time Monitoring: 5-minute check intervals provide near-instant detection
  • Customizable: Choose which groups to watch and who gets notified
  • Audit Trail: Keeps a log of every change for forensic or compliance reviews

Final Thoughts

ADHawk embodies what PowerShell does best—simple, direct, and effective automation. Whether you’re a solo sysadmin or part of a larger security team, this script gives you a lightweight, proactive way to harden your AD infrastructure.

Try it out, customize it to your needs, and let ADHawk keep a vigilant eye on your domain.

#Another        /_[]_/
#    fine      |] _||_ [|
#       ___     / || /
#      /___       ||
#     (|0 0|)      ||
#   __/{U/}_ ___/vvv
#  /   {~}   / _|_P|
#  | /  ~   /_/   []
#  |_| (____)        
#  _]/______  Barberion  
#     __||_/_     Production      
#    (_,_||_,_)
#
#Requires -Modules ActiveDirectory
$groups = @("Domain Admins", "Exchange Admins", "Enterprise Admins",  "Schema Admins", "Backup Operators", "Account Operators", "Server Operators")
$stateFilePath = "C:customappsadgroupmembers"
$logFilePath = "C:customappsadgroupmemberschange.log"

# directory exists?
if (-not (Test-Path $stateFilePath)) {
    New-Item -Path $stateFilePath -ItemType Directory
}

# get group members and return as simple string array
function Get-GroupMembers {
    param (
        [string]$GroupName
    )
    Get-ADGroupMember -Identity $GroupName | Select-Object -ExpandProperty SamAccountName | Sort-Object
}

# send email notification
function Send-Email {
    param (
        [string]$Subject,
        [string]$Body
    )
    $EmailFrom = "[email protected]"
    $EmailTo = "[email protected],[email protected]"
    $SMTPServer = "Your SMTP server IP here"

    Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer
    # Append to change.log
    Add-Content -Path $logFilePath -Value $Body
    Add-Content -Path $logFilePath -Value "rn" # Add a blank line
}

foreach ($group in $groups) {
    $currentMembers = Get-GroupMembers -GroupName $group
    $filePath = Join-Path -Path $stateFilePath -ChildPath "$group.csv"
    
    if (Test-Path $filePath) {
        $previousMembers = Get-Content $filePath
        $differences = Compare-Object -ReferenceObject $previousMembers -DifferenceObject $currentMembers
    
        if ($differences) {
            $changes = $differences | ForEach-Object {
                if ($_.SideIndicator -eq "<=") {
                    "$($_.InputObject) was removed from $group"
                } else {
                    "$($_.InputObject) was added to $group"
                }
            }
            $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            $body = "Changes detected at ${timestamp}:rn" + ($changes -join "rn")
            Send-Email -Subject "AD Group Altered" -Body $body
        }
    }
    
    # Always update the file with the current state for next comparison
    $currentMembers | Out-File $filePath
}

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article What Is a Multi-Cloud Strategy & How to Implement It in 2025
Next Article Own Microsoft Office Pro for just $30
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

The AI Box Is Coming—Build Your Own or Be Owned by Big Tech | HackerNoon
Computing
Beloved grocery chain now selling $24.99 ‘colossal’ treat that weighs 4 pounds
News
Toyota sees growth in China in November after nine-month decline · TechNode
Computing
Virgin Media customers have just hours to claim FREE Apple iPad worth £439
News

You Might also Like

Computing

The AI Box Is Coming—Build Your Own or Be Owned by Big Tech | HackerNoon

10 Min Read
Computing

Toyota sees growth in China in November after nine-month decline · TechNode

1 Min Read
Computing

How to Set up a Facebook Business Page in 10 Minutes – Blog

11 Min Read
Computing

How We Taught a Neural Network to Design Headstones | HackerNoon

5 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?