Skip to content

Commit 67c9070

Browse files
committed
Draft. Refactor Collect-MissingSubnets
1 parent 112c8e1 commit 67c9070

File tree

2 files changed

+581
-0
lines changed

2 files changed

+581
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
$NetlogonLogPath = 'C:\Windows\Debug\netlogon.log'
2+
3+
4+
Import-Module ActiveDirectory
5+
6+
# Get all subnets from Active Directory Sites and Services
7+
$ADSubnets = Get-ADSubnet -Filter * | Select-Object -ExpandProperty Name
8+
9+
# Read the netlogon.log file
10+
$LogEntries = Get-Content -Path $NetlogonLogPath
11+
12+
# Define a regex pattern to match log entries with IP addresses
13+
$IpPattern = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
14+
15+
# Initialize an array to store unmatched subnets
16+
$UnmatchedSubnets = New-Object System.Collections.Generic.List[ipaddress]
17+
18+
# Parse the log entries
19+
foreach ($entry in $LogEntries) {
20+
if ($entry -match $IpPattern) {
21+
$IpAddress = $matches[0]
22+
$SubnetMatch = $false
23+
24+
# Check if the IP address belongs to any AD subnet
25+
foreach ($subnet in $ADSubnets) {
26+
if ($IpAddress -like "$subnet*") {
27+
$SubnetMatch = $true
28+
break
29+
}
30+
}
31+
32+
# If no match found, add to unmatched subnets
33+
if (-not $SubnetMatch) {
34+
$UnmatchedSubnets.Add($IpAddress)
35+
}
36+
}
37+
}
38+
39+
# Output the unmatched subnets
40+
if ($UnmatchedSubnets.Count -gt 0) {
41+
Write-Output 'The following IP addresses are from subnets not listed in Active Directory Sites and Services:'
42+
$UnmatchedSubnets | Sort-Object | Get-Unique | ForEach-Object { Write-Output $_ }
43+
} else {
44+
Write-Output 'No unmatched subnets found.'
45+
}

0 commit comments

Comments
 (0)