Set up a PowerShell script that runs periodically on your domain controller to check for new servers. Here’s an outline of how you can approach this:



PowerShell Script


# Set the security group and email details

$groupName = "AllWindowsServers"

$emailRecipient = "admin@example.com"

$emailSender = "alert@example.com"

$smtpServer = "smtp.example.com"


# Find all Windows Server computers not already in the group

$servers = Get-ADComputer -Filter 'OperatingSystem -like "*Windows Server*"' -Properties OperatingSystem, MemberOf | 

    Where-Object { ($_ | Get-ADGroupMember -Recursive | Where-Object { $_.Name -eq $groupName }) -eq $null }


# Initialize a list to track newly added servers

$newServers = @()


foreach ($server in $servers) {

    # Add the server to the group

    Add-ADGroupMember -Identity $groupName -Members $server

    $newServers += $server.Name

}


# If there were new servers added, send an email notification

if ($newServers.Count -gt 0) {

    $subject = "New Windows Servers Added to Security Group"

    $body = "The following Windows Servers were added to the $groupName security group:`n`n" + ($newServers -join "`n")

    

    Send-MailMessage -From $emailSender -To $emailRecipient -Subject $subject -Body $body -SmtpServer $smtpServer

}