Skip to content

Commit 0bb42f9

Browse files
authored
Added option to filter by computer pwdLastSet date
1 parent 5be2ed3 commit 0bb42f9

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Get-RBCD-Threaded/Program.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
@@ -60,6 +60,7 @@ static void Main(string[] args)
6060
bool ldapInSecure = false;
6161
bool help = false;
6262
bool searchForest = false;
63+
int computerDate = 0;
6364

6465
var options = new OptionSet()
6566
{
@@ -69,6 +70,7 @@ static void Main(string[] args)
6970
{"s|searchforest", "Enumerate all domains and forests", v => searchForest = true },
7071
{"o|outputfile=", "Output to a CSV file. Please provided full path to file and file name.", v => outputfile = v },
7172
{"i|insecure", "Force insecure LDAP connect if LDAPS is causing connection issues.", v => ldapInSecure = true },
73+
{"pwdlastset=", "Filter computers based on pwdLastSet to remove stale computer objects. If you set this to 90, it will filter out computer objects whose pwdLastSet date is more than 90 days ago", (int v) => computerDate = v },
7274
{ "h|?|help", "Show this help", v => help = true }
7375
};
7476

@@ -195,7 +197,7 @@ static void Main(string[] args)
195197

196198
Get_Users(adEntry, sidMapList, allSids, currentDomain);
197199
Get_Groups(adEntry, sidMapList, allSids, currentDomain);
198-
aclResults = Get_Computers(adEntry, sidMapList, allSids, currentDomain, aclResults);
200+
aclResults = Get_Computers(adEntry, sidMapList, allSids, currentDomain, aclResults, computerDate);
199201

200202

201203
foreach (SearchResult acl in aclResults)
@@ -231,7 +233,7 @@ static void Main(string[] args)
231233

232234
Get_Users(adEntry, sidMapList, allSids, currentDomain);
233235
Get_Groups(adEntry, sidMapList, allSids, currentDomain);
234-
aclResults = Get_Computers(adEntry, sidMapList, allSids, currentDomain, aclResults);
236+
aclResults = Get_Computers(adEntry, sidMapList, allSids, currentDomain, aclResults, computerDate);
235237

236238
foreach (SearchResult acl in aclResults)
237239
{
@@ -358,10 +360,21 @@ public static void Get_Groups(DirectoryEntry adEntry, List<sidMap> sidMapList, L
358360
}
359361
}
360362

361-
public static SearchResultCollection Get_Computers(DirectoryEntry adEntry, List<sidMap> sidMapList, List<string> allSids, string currentDomain, SearchResultCollection aclResults)
363+
public static SearchResultCollection Get_Computers(DirectoryEntry adEntry, List<sidMap> sidMapList, List<string> allSids, string currentDomain, SearchResultCollection aclResults, int computerDate)
362364
{
363365
DirectorySearcher aclSearch = new DirectorySearcher(adEntry);
364-
aclSearch.Filter = "(&(samAccountType=805306369))";
366+
long date = 0;
367+
if (computerDate != 0)
368+
{
369+
date = DateTime.Now.AddDays(Math.Abs(computerDate) * (-1)).ToFileTime();
370+
aclSearch.Filter = "(&(samAccountType=805306369)(pwdLastSet>=" + date.ToString() + "))";
371+
}
372+
else
373+
{
374+
aclSearch.Filter = "(&(samAccountType=805306369))";
375+
}
376+
377+
//aclSearch.Filter = "(&(samAccountType=805306369)(pwdlastset<=" + date.ToString() +")";
365378
var Properties = new[] { "samaccountname", "ntsecuritydescriptor", "objectsid", "dnshostname" };
366379
aclSearch.PropertiesToLoad.AddRange(Properties);
367380
aclSearch.SecurityMasks = SecurityMasks.Dacl | SecurityMasks.Owner;

0 commit comments

Comments
 (0)