-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADMappingToolkit.psm1
More file actions
864 lines (755 loc) · 33.9 KB
/
ADMappingToolkit.psm1
File metadata and controls
864 lines (755 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# ADMappingToolkit.psm1
Set-StrictMode -Version Latest
# ---------- Utils ----------
function Resolve-ToIP {
param([Parameter(Mandatory)][string]$Target)
if ($Target -match '^\d{1,3}(\.\d{1,3}){3}$') { return $Target }
try {
$a = Resolve-DnsName -Name $Target -Type A -ErrorAction Stop | Select-Object -First 1 -ExpandProperty IPAddress
if ($a) { return $a }
}
catch {}
try {
$addr = [System.Net.Dns]::GetHostAddresses($Target) |
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
Select-Object -First 1
if ($addr) { return $addr.IPAddressToString }
}
catch {}
return $null
}
function Test-Icmp {
param([Parameter(Mandatory)][string]$Computer, [int]$Count = 1)
try {
return (Test-Connection -ComputerName $Computer -Count $Count -Quiet -ErrorAction SilentlyContinue)
}
catch { return $false }
}
function Test-TcpPort {
param([Parameter(Mandatory)][string]$Computer, [Parameter(Mandatory)][int]$Port, [int]$TimeoutMs = 1200)
$client = New-Object System.Net.Sockets.TcpClient
try {
$iar = $client.BeginConnect($Computer, $Port, $null, $null)
if (-not $iar.AsyncWaitHandle.WaitOne($TimeoutMs, $false)) { $client.Close(); return $false }
$client.EndConnect($iar)
$client.Close()
return $true
}
catch {
try { $client.Close() } catch {}
return $false
}
}
# ---------- Inventory ----------
# --- Helper Functions for Target Parsing ---
function Get-IpsFromCidr {
param([string]$Cidr)
try {
$ipStr, $maskBits = $Cidr.Split('/')
[int]$mask = $maskBits
$ipAddr = [System.Net.IPAddress]::Parse($ipStr)
$bytes = $ipAddr.GetAddressBytes()
if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($bytes) }
[uint32]$ipInt = [System.BitConverter]::ToUInt32($bytes, 0)
[uint32]$maskInt = [uint32]::MaxValue -shl (32 - $mask)
[uint32]$network = $ipInt -band $maskInt
[uint32]$broadcast = $network -bor (-bnot $maskInt)
$start = $network + 1
$end = $broadcast - 1
$results = @()
# Cap large ranges to avoid infinite loops if user scans /8
if (($end - $start) -gt 65536) {
Write-Warning "CIDR $Cidr is too large. Scanning first 65536 IPs only."
$end = $start + 65536
}
for ($i = $start; $i -le $end; $i++) {
$b = [System.BitConverter]::GetBytes([uint32]$i)
if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($b) }
$results += ([System.Net.IPAddress]::new($b)).IPAddressToString
}
return $results
}
catch {
Write-Warning "Failed to parse CIDR: $Cidr"
return @()
}
}
function Get-IpsFromRange {
param([string]$Range)
# Supports: 192.168.1.1-192.168.1.50 OR 192.168.1.1-50
try {
$startStr, $endStr = $Range.Split('-')
$startIP = [System.Net.IPAddress]::Parse($startStr)
$endIP = $null
if ($endStr -match '^\d+$') {
# Format: 192.168.1.1-50
$base = $startStr.Substring(0, $startStr.LastIndexOf('.'))
$endIP = [System.Net.IPAddress]::Parse("$base.$endStr")
}
else {
# Format: 192.168.1.1-192.168.1.50
$endIP = [System.Net.IPAddress]::Parse($endStr)
}
# Simple conversion to Int for loop
$bytesS = $startIP.GetAddressBytes(); if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($bytesS) }
[uint32]$s = [System.BitConverter]::ToUInt32($bytesS, 0)
$bytesE = $endIP.GetAddressBytes(); if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($bytesE) }
[uint32]$e = [System.BitConverter]::ToUInt32($bytesE, 0)
$results = @()
if ($e -lt $s) { return @() }
if (($e - $s) -gt 65536) { Write-Warning "Range $Range is too large. Scanning first 65536 IPs only."; $e = $s + 65536 }
for ($i = $s; $i -le $e; $i++) {
$b = [System.BitConverter]::GetBytes([uint32]$i)
if ([System.BitConverter]::IsLittleEndian) { [Array]::Reverse($b) }
$results += ([System.Net.IPAddress]::new($b)).IPAddressToString
}
return $results
}
catch {
Write-Warning "Failed to parse Range: $Range"
return @()
}
}
function Get-MachineInventory {
[CmdletBinding()]
param(
[string]$SearchBase = "",
[string]$ADFilter = '*',
[switch]$IncludeDisabled,
[switch]$OnlyDisabled,
[switch]$TcpCheck,
[string]$Ports = "445,3389,5985",
[switch]$NoIcmp,
[int]$PingCount = 1,
[int]$TimeoutMs = 1200,
[switch]$OnlyReachable,
[switch]$OnlyUnreachable,
[int]$MinDaysInactive = 0,
[switch]$ADOnly,
[switch]$Stealth,
[switch]$CheckWinRM,
[switch]$ResolveDns,
[switch]$CheckUnconstrained,
[switch]$CheckPSSession,
[switch]$ShowDescription,
[switch]$ShowOU,
[switch]$Turbo,
[switch]$ForceICMP = $false,
[int[]]$StealthDelay = @(2, 10),
[int]$Threads = 50,
[string[]]$Targets,
[PSCredential]$Credential,
[switch]$CheckLDAP,
[switch]$CheckEOS,
[string]$Report,
[int]$ReportDays = 30
)
# Turbo Mode Safety Check
if ($Turbo -and $PSVersionTable.PSVersion.Major -lt 7) {
Write-Warning "Turbo mode requires PowerShell 7+. Falling back to sequential mode."
$Turbo = $false
}
if ($Turbo -and $Stealth) {
Write-Warning "Turbo mode and Stealth mode are mutually exclusive. Disabling Turbo to prioritize Stealth safety."
$Turbo = $false
}
if ($Turbo) {
if ($Threads -lt 1) {
$Threads = 1
}
elseif ($Threads -gt 1000) {
Write-Warning "Thread count $Threads exceeds safe limit of 1000. Capping at 1000 to prevent network stack exhaustion."
$Threads = 1000
}
}
$machines = @()
# --- Target Processing Logic ---
if ($Targets) {
$adFilters = @()
$ipTargets = @()
foreach ($t in $Targets) {
if ($t -match '[*?]') {
# Wildcard -> AD Filter pattern
Write-Verbose "Target '$t' identified as Wildcard/AD Filter."
$adFilters += $t
}
elseif ($t -match '/') {
# CIDR
Write-Verbose "Target '$t' identified as CIDR."
$ipTargets += Get-IpsFromCidr $t
}
elseif ($t -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.*-.*') {
# IP Range
Write-Verbose "Target '$t' identified as IP Range."
$ipTargets += Get-IpsFromRange $t
}
elseif ($t -as [System.Net.IPAddress]) {
# IP
Write-Verbose "Target '$t' identified as Single IP."
$ipTargets += $t
}
else {
# Fallback
Write-Verbose "Target '$t' identified as Hostname/AD Name."
$adFilters += $t
}
}
Write-Verbose "Found $($ipTargets.Count) IP targets and $($adFilters.Count) AD Filters."
# Dependency Logic Update
$needAd = $CheckUnconstrained -or $ShowDescription -or $ShowOU
if ($ipTargets.Count -gt 0 -and $needAd -and -not $ResolveDns) {
Write-Warning "Creating a dependency: Options ($CheckUnconstrained/$ShowDescription/$ShowOU) require '-ResolveDns' for IP targets. Enabling automatically."
$ResolveDns = $true
}
# Fetch AD Targets
if ($adFilters.Count -gt 0) {
$props = @('Name', 'IPv4Address', 'DNSHostName', 'OperatingSystem', 'Enabled', 'CanonicalName', 'LastLogonDate')
if ($CheckUnconstrained) { $props += 'TrustedForDelegation' }
if ($ShowDescription) { $props += 'Description' }
foreach ($f in $adFilters) {
# Construct AD Filter. simple 'Name -like'
# Note: Get-ADComputer -Filter "Name -like '$f'"
$params = @{
Filter = "Name -like '$f'"
Properties = $props
}
if ($SearchBase) { $params['SearchBase'] = $SearchBase }
try {
$res = Get-ADComputer @params
if ($res) { $machines += $res }
}
catch {
Write-Warning "Error querying AD for '$f': $_"
}
}
}
# Add IP Targets
if ($Turbo -and $ipTargets.Count -gt 0) {
Write-Verbose "Resolving $($ipTargets.Count) IP targets in parallel (Turbo Mode)..."
$synMachines = $ipTargets | ForEach-Object -Parallel {
$ip = $_
$machineObj = $null
$dnsName = $ip
$foundInAd = $false
# Using modifiers for outer variables
$doResolve = $using:ResolveDns
$doUnconst = $using:CheckUnconstrained
if ($doResolve) {
try {
$dnsRec = Resolve-DnsName -Name $ip -Type PTR -ErrorAction Stop | Select-Object -First 1
if ($dnsRec -and $dnsRec.NameHost) {
$dnsName = $dnsRec.NameHost
$cleanName = $dnsName.TrimEnd('.')
try {
$props = @('Name', 'IPv4Address', 'DNSHostName', 'OperatingSystem', 'Enabled', 'CanonicalName', 'LastLogonDate')
if ($doUnconst) { $props += 'TrustedForDelegation' }
if ($using:ShowDescription) { $props += 'Description' }
$adObj = Get-ADComputer -Filter "DNSHostName -like '$cleanName*' -or Name -eq '$cleanName'" -Properties $props -ErrorAction SilentlyContinue | Select-Object -First 1
if ($adObj) {
$finalObj = [ordered]@{
Name = $adObj.Name
IPv4Address = $adObj.IPv4Address
DNSHostName = $adObj.DNSHostName
OperatingSystem = $adObj.OperatingSystem
Enabled = $adObj.Enabled
CanonicalName = $adObj.CanonicalName
LastLogonDate = $adObj.LastLogonDate
ManualTargetIP = "$ip"
}
if ($doUnconst) { $finalObj['TrustedForDelegation'] = $adObj.TrustedForDelegation }
if ($using:ShowDescription) { $finalObj['Description'] = $adObj.Description }
return [PSCustomObject]$finalObj
}
}
catch {}
}
}
catch {}
}
# Synthetic Object
$synObj = [ordered]@{
Name = if ($doResolve -and $dnsName -ne $ip) { $dnsName.TrimEnd('.') } else { "$ip" }
IPv4Address = "$ip"
DNSHostName = if ($doResolve) { $dnsName } else { "$ip" }
OperatingSystem = "Unknown (Network Scan)"
Enabled = $true
CanonicalName = "Network/IP"
LastLogonDate = $null
ManualTargetIP = "$ip"
}
if ($doUnconst) { $synObj['TrustedForDelegation'] = $false }
if ($using:ShowDescription) { $synObj['Description'] = $null }
return [PSCustomObject]$synObj
} -ThrottleLimit $Threads
$machines += $synMachines
}
else {
# Sequential Fallback
foreach ($ip in $ipTargets) {
$machineObj = $null
$dnsName = $ip
$foundInAd = $false
if ($ResolveDns) {
try {
$dnsRec = Resolve-DnsName -Name $ip -Type PTR -ErrorAction Stop | Select-Object -First 1
if ($dnsRec -and $dnsRec.NameHost) {
$dnsName = $dnsRec.NameHost
$cleanName = $dnsName.TrimEnd('.')
try {
$props = @('Name', 'IPv4Address', 'DNSHostName', 'OperatingSystem', 'Enabled', 'CanonicalName', 'LastLogonDate')
if ($CheckUnconstrained) { $props += 'TrustedForDelegation' }
if ($ShowDescription) { $props += 'Description' }
$adObj = Get-ADComputer -Filter "DNSHostName -like '$cleanName*' -or Name -eq '$cleanName'" -Properties $props -ErrorAction SilentlyContinue | Select-Object -First 1
if ($adObj) {
$finalObj = [ordered]@{
Name = $adObj.Name
IPv4Address = $adObj.IPv4Address
DNSHostName = $adObj.DNSHostName
OperatingSystem = $adObj.OperatingSystem
Enabled = $adObj.Enabled
CanonicalName = $adObj.CanonicalName
LastLogonDate = $adObj.LastLogonDate
ManualTargetIP = "$ip"
}
if ($CheckUnconstrained) { $finalObj['TrustedForDelegation'] = $adObj.TrustedForDelegation }
if ($ShowDescription) { $finalObj['Description'] = $adObj.Description }
$machineObj = [PSCustomObject]$finalObj
$foundInAd = $true
}
}
catch {}
}
}
catch {}
}
if (-not $foundInAd) {
$synObj = [ordered]@{
Name = if ($ResolveDns -and $dnsName -ne $ip) { $dnsName.TrimEnd('.') } else { "$ip" }
IPv4Address = "$ip"
DNSHostName = if ($ResolveDns) { $dnsName } else { "$ip" }
OperatingSystem = "Unknown (Network Scan)"
Enabled = $true
CanonicalName = "Network/IP"
LastLogonDate = $null
ManualTargetIP = "$ip"
}
if ($CheckUnconstrained) { $synObj['TrustedForDelegation'] = $false }
if ($ShowDescription) { $synObj['Description'] = $null }
$machineObj = [PSCustomObject]$synObj
}
$machines += $machineObj
}
}
Write-Verbose "Total machines before dedupe: $(@($machines).Count)"
# Remove duplicates
$machines = $machines | Sort-Object Name -Unique
Write-Verbose "Total machines after dedupe: $(@($machines).Count)"
}
else {
# --- Standard AD Scan ---
$props = @('Name', 'IPv4Address', 'DNSHostName', 'OperatingSystem', 'Enabled', 'CanonicalName', 'LastLogonDate')
if ($CheckUnconstrained) { $props += 'TrustedForDelegation' }
if ($ShowDescription) { $props += 'Description' }
$adParams = @{
Filter = $ADFilter
Properties = $props
}
if ($SearchBase) { $adParams['SearchBase'] = $SearchBase }
$machines = Get-ADComputer @adParams
}
# Normalize Object for processing
$selectProps = @('Name', 'IPv4Address', 'DNSHostName', 'OperatingSystem', 'Enabled', 'CanonicalName', 'LastLogonDate', 'ManualTargetIP')
if ($CheckUnconstrained) { $selectProps += 'TrustedForDelegation' }
if ($ShowDescription) { $selectProps += 'Description' }
$machines = $machines | Select-Object -Property * | Select-Object $selectProps
if ($OnlyDisabled) {
if ($Targets) { Write-Warning "Filtering by Disabled status may hide IP targets as they have synthetic Enabled=True." }
$machines = @($machines | Where-Object { -not $_.Enabled })
}
elseif (-not $IncludeDisabled) {
$machines = @($machines | Where-Object { $_.Enabled })
}
if ($Stealth) {
$machines = $machines | Sort-Object { Get-Random }
}
$legacyServerRegex = 'Windows (2000|.*2003|.*2008|.*2012)'
$legacyClientRegex = 'Windows (XP|Vista|7|8)'
# --- Port Keyword Expansion ---
$top5 = "21,22,80,443,3389"
$top10 = "$top5,25,53,135,139,445"
$top20 = "$top10,110,143,389,1433,3306,5900,5985,8080,8443"
$top50 = "$top20,23,69,88,111,119,161,162,179,199,381,382,383,464,444,513,514,515,543,544,548,554,587,631,636,873,989,990,993,995"
$top100 = "$top50,1025,1194,1521,1701,1723,2000,2049,2082,2083,2121,2222,2375,2376,2483,2484,3268,3269,3388,4000,4444,4500,4848,5000,5060,5432,5555,5631,5632,5800,5901,5902,5938,6000,6001,6667,7000,7001,8000,8008,8081,8088,8090,8222,8444,8500,8888,9000,9090,9418,9999"
if ($Ports -match '^Top(\d+)$') {
switch ($Ports) {
"Top5" { $Ports = $top5; break }
"Top10" { $Ports = $top10; break }
"Top20" { $Ports = $top20; break }
"Top50" { $Ports = $top50; break }
"Top100" { $Ports = $top100; break }
Default { Write-Warning "Unknown Top list '$Ports'. Using default."; $Ports = "445,3389,5985" }
}
Write-Verbose "Expanded Ports '$Ports'"
# Auto-enable TcpCheck if user asks for a Top list
$TcpCheck = $true
}
if ($Report -eq 'OpenRDP') {
$TcpCheck = $true
$p = $Ports -split '[,; ]+'
if ('3389' -notin $p) { $Ports += ",3389" }
}
$portList = @()
if ($CheckLDAP) {
if (-not $TcpCheck) {
# User requested ONLY LDAP, override default ports
$Ports = "389,636"
$TcpCheck = $true
}
else {
# User requested TCP Check AND LDAP, append
$p = $Ports -split '[,; ]+'
if ('389' -notin $p) { $Ports += ",389" }
if ('636' -notin $p) { $Ports += ",636" }
}
}
if ($TcpCheck -and -not $ADOnly) {
# If Report=OpenRDP, ensure 3389 is checked. Moved logic up to force TcpCheck.
$portList = @($Ports -split '[,; ]+' | Where-Object { $_ -match '^\d+$' } | ForEach-Object { [int]$_ })
}
# --- Execution Block ---
if ($Turbo -and -not $ADOnly) {
# PARALLEL MODE (PowerShell 7+)
$rows = $machines | ForEach-Object -Parallel {
$m = $_
$daysSince = $null
if ($m.LastLogonDate) {
$daysSince = (New-TimeSpan -Start $m.LastLogonDate -End (Get-Date)).Days
}
# Filtering inside parallel block
if ($using:MinDaysInactive -gt 0) {
if ($null -eq $daysSince -or $daysSince -lt $using:MinDaysInactive) { return }
}
$base = if ($m.IPv4Address) { $m.IPv4Address } elseif ($m.DNSHostName) { $m.DNSHostName } else { $m.Name }
# --- Inlined Network Checks for Parallel Performance ---
# IP Resolution
$ip = $null
if ($m.ManualTargetIP) {
# If we scanned a specific IP, ALWAYS use it. Do not resolve $base (which might be stale AD IP).
$ip = $m.ManualTargetIP
}
else {
try {
$ip = [System.Net.Dns]::GetHostAddresses($base) | Select-Object -First 1 -ExpandProperty IPAddressToString -ErrorAction SilentlyContinue
}
catch {}
}
$target = if ($ip) { $ip } else { $base }
# Ping
$ping = $null
if (-not $using:NoIcmp) {
try {
if ($using:ForceICMP -or ($using:PSVersionTable).PSVersion.Major -lt 7) {
$ping = (Test-Connection -ComputerName $target -Count 1 -Quiet -ErrorAction SilentlyContinue)
}
else {
$p = New-Object System.Net.NetworkInformation.Ping
$reply = $p.Send($target, $using:TimeoutMs)
$ping = ($reply.Status -eq 'Success')
}
}
catch {
$ping = $false
}
}
# Ports
$portStates = @{}
$portList = $using:portList
if ($using:TcpCheck -and $portList.Count -gt 0) {
foreach ($p in $portList) {
$isOpen = $false
try {
$tcp = New-Object System.Net.Sockets.TcpClient
$connect = $tcp.BeginConnect($target, $p, $null, $null)
$wait = $connect.AsyncWaitHandle.WaitOne($using:TimeoutMs, $false)
if ($wait) {
$isOpen = $tcp.Connected
$tcp.EndConnect($connect)
}
$tcp.Close()
$tcp.Dispose()
}
catch {}
$portStates["TCP_$p"] = $isOpen
}
}
# WinRM
$winrmStatus = $null
if ($using:CheckWinRM) {
try {
Test-WSMan -ComputerName $target -ErrorAction Stop | Out-Null
$winrmStatus = $true
}
catch {
$winrmStatus = $false
}
}
# Helper function for PSSession outside? No, inside for parallel.
$pssessionStatus = $null
if ($using:CheckPSSession) {
# Only try if we think it's reachable or blindly? Blindly is safer if ICMP blocked.
try {
if ($using:Credential) {
Invoke-Command -ComputerName $target -Credential $using:Credential -ScriptBlock { $true } -ErrorAction Stop | Out-Null
}
else {
Invoke-Command -ComputerName $target -ScriptBlock { $true } -ErrorAction Stop | Out-Null
}
$pssessionStatus = $true
}
catch {
$pssessionStatus = $false
}
}
$reachable = $false
if ($null -ne $ping) { $reachable = $reachable -or $ping }
if ($using:TcpCheck) { $reachable = $reachable -or ($portStates.Values -contains $true) }
if ($using:CheckWinRM -and $winrmStatus -eq $true) { $reachable = $true }
if ($using:CheckPSSession -and $pssessionStatus -eq $true) { $reachable = $true }
$obj = [ordered]@{
Name = $m.Name
IPv4Address = if ($ip) { $ip } else { $m.IPv4Address }
OS = $m.OperatingSystem
Enabled = $m.Enabled
OU = $m.CanonicalName
LastLogon = $m.LastLogonDate
DaysSince = $daysSince
TargetUsed = $target
PingICMP = if ($null -ne $ping) { [bool]$ping } else { $null }
EOS = if ($using:CheckEOS) { ($m.OperatingSystem -match $using:legacyServerRegex -or $m.OperatingSystem -match $using:legacyClientRegex) } else { $null }
}
if ($using:CheckUnconstrained) { $obj['Unconstrained'] = [bool]$m.TrustedForDelegation }
if ($using:ShowDescription) { $obj['Description'] = $m.Description }
foreach ($k in $portStates.Keys) { $obj[$k] = $portStates[$k] }
if ($using:CheckWinRM) { $obj['WinRM'] = $winrmStatus }
if ($using:CheckPSSession) { $obj['PSSession'] = $pssessionStatus }
$obj['Reachable'] = $reachable
[pscustomobject]$obj
} -ThrottleLimit $Threads
}
else {
# SEQUENTIAL MODE (Standard)
# Validate StealthDelay
$minDelay = 0
$maxDelay = 0
if ($Stealth) {
if ($StealthDelay.Count -eq 1) {
$minDelay = $StealthDelay[0]
$maxDelay = $StealthDelay[0]
}
elseif ($StealthDelay.Count -ge 2) {
$minDelay = $StealthDelay[0]
$maxDelay = $StealthDelay[1]
}
}
$rows = foreach ($m in $machines) {
$daysSince = $null
if ($m.LastLogonDate) {
$daysSince = (New-TimeSpan -Start $m.LastLogonDate -End (Get-Date)).Days
}
if ($MinDaysInactive -gt 0) {
if ($null -eq $daysSince -or $daysSince -lt $MinDaysInactive) { continue }
}
if ($Stealth -and -not $ADOnly) {
$delay = 0
if ($minDelay -eq $maxDelay) {
$delay = $minDelay
}
else {
$delay = Get-Random -Minimum $minDelay -Maximum ($maxDelay + 1)
}
if ($delay -gt 0) {
Write-Verbose "[Stealth] Pausing $delay seconds before $($m.Name)..."
Start-Sleep -Seconds $delay
}
}
$base = if ($m.IPv4Address) { $m.IPv4Address } elseif ($m.DNSHostName) { $m.DNSHostName } else { $m.Name }
# Initialize $ip to avoid VariableIsUndefined errors
$ip = $null
if ($ADOnly) {
$target = $base
$ping = $null
$portStates = @{}
$winrmStatus = $null
}
else {
# Check if ManualTargetIP property exists and has a value
if (($m.PSObject.Properties.Name -contains 'ManualTargetIP') -and $m.ManualTargetIP) {
$ip = $m.ManualTargetIP
$target = $m.ManualTargetIP
}
else {
$ip = Resolve-ToIP $base
$target = if ($ip) { $ip } else { $base }
}
$ping = $null
if (-not $NoIcmp) { $ping = Test-Icmp -Computer $target -Count $PingCount }
$portStates = @{}
if ($TcpCheck -and $portList.Count -gt 0) {
foreach ($p in $portList) {
$portStates["TCP_$p"] = Test-TcpPort -Computer $target -Port $p -TimeoutMs $TimeoutMs
}
}
$winrmStatus = $null
if ($CheckWinRM) {
try {
Test-WSMan -ComputerName $target -ErrorAction Stop | Out-Null
$winrmStatus = $true
}
catch {
$winrmStatus = $false
}
}
}
$pssessionStatus = $null
if ($CheckPSSession) {
try {
if ($Credential) {
Invoke-Command -ComputerName $target -Credential $Credential -ScriptBlock { $true } -ErrorAction Stop | Out-Null
}
else {
Invoke-Command -ComputerName $target -ScriptBlock { $true } -ErrorAction Stop | Out-Null
}
$pssessionStatus = $true
}
catch {
$pssessionStatus = $false
}
}
$reachable = $false
if ($null -ne $ping) { $reachable = $reachable -or $ping }
if ($TcpCheck) { $reachable = $reachable -or ($portStates.Values -contains $true) }
if ($CheckWinRM -and $winrmStatus -eq $true) { $reachable = $true }
if ($CheckPSSession -and $pssessionStatus -eq $true) { $reachable = $true }
$obj = [ordered]@{
Name = $m.Name
IPv4Address = if ($ip) { $ip } else { $m.IPv4Address }
OS = $m.OperatingSystem
Enabled = $m.Enabled
OU = $m.CanonicalName
LastLogon = $m.LastLogonDate
DaysSince = $daysSince
TargetUsed = if ($target) { $target } else { "N/A" }
PingICMP = if ($null -ne $ping) { [bool]$ping } else { $null }
EOS = if ($CheckEOS) { ($m.OperatingSystem -match $legacyServerRegex -or $m.OperatingSystem -match $legacyClientRegex) } else { $null }
}
if ($CheckUnconstrained) { $obj['Unconstrained'] = [bool]$m.TrustedForDelegation }
if ($ShowDescription) { $obj['Description'] = $m.Description }
foreach ($k in $portStates.Keys) { $obj[$k] = $portStates[$k] }
if ($CheckWinRM) { $obj['WinRM'] = $winrmStatus }
if ($CheckPSSession) { $obj['PSSession'] = $pssessionStatus }
if ($obj) { $obj['Reachable'] = $reachable }
[pscustomobject]$obj
}
}
if ($Turbo -and -not $ADOnly) {
$view = $rows
}
else {
# In sequential mode, $rows is an array of PSCustomObjects, but if it's empty we need at least an empty array
if ($null -eq $rows) { $view = @() } else { $view = @($rows) }
}
if ($OnlyReachable) { $view = $view | Where-Object { $_.Reachable } }
if ($OnlyUnreachable) { $view = $view | Where-Object { -not $_.Reachable } }
# --- Reporting Filter ---
if ($Report -eq 'Inactive') {
$view = $view | Where-Object { $_.DaysSince -ge $ReportDays }
}
elseif ($Report -eq 'OpenRDP') {
# Ensure property exists before filtering to avoid errors
if ($view -and ($view[0].PSObject.Properties.Name -contains 'TCP_3389')) {
$view = $view | Where-Object { $_.TCP_3389 -eq $true }
}
else {
Write-Warning "Report 'OpenRDP' requested but TCP_3389 property missing. Ensure -TcpCheck is enabled (it should be automatic)."
$view = @()
}
}
return $view
}
function Get-ServerInventory {
[CmdletBinding()]
param(
[string]$SearchBase = "",
[switch]$IncludeDisabled,
[switch]$OnlyDisabled,
[switch]$TcpCheck,
[string]$Ports = "445,3389,5985",
[switch]$NoIcmp,
[int]$PingCount = 1,
[int]$TimeoutMs = 1200,
[switch]$OnlyReachable,
[switch]$OnlyUnreachable,
[int]$MinDaysInactive = 0,
[switch]$ADOnly,
[switch]$Stealth,
[switch]$CheckWinRM,
[switch]$ResolveDns,
[switch]$CheckUnconstrained,
[switch]$CheckPSSession,
[switch]$ShowDescription,
[switch]$ShowOU,
[switch]$Turbo,
[int[]]$StealthDelay = @(2, 10),
[int]$Threads = 50,
[string[]]$Targets,
[PSCredential]$Credential,
[switch]$CheckLDAP,
[switch]$CheckEOS,
[string]$Report,
[int]$ReportDays = 30
)
$params = @{
ADFilter = 'OperatingSystem -like "*Server*"'
}
$PSBoundParameters.Keys | Where-Object { $_ -ne 'ADFilter' } | ForEach-Object { $params[$_] = $PSBoundParameters[$_] }
Get-MachineInventory @params
}
function Get-ClientInventory {
[CmdletBinding()]
param(
[string]$SearchBase = "",
[switch]$IncludeDisabled,
[switch]$OnlyDisabled,
[switch]$TcpCheck,
[string]$Ports = "445,3389,5985",
[switch]$NoIcmp,
[int]$PingCount = 1,
[int]$TimeoutMs = 1200,
[switch]$OnlyReachable,
[switch]$OnlyUnreachable,
[int]$MinDaysInactive = 0,
[switch]$ADOnly,
[switch]$Stealth,
[switch]$CheckWinRM,
[switch]$ResolveDns,
[switch]$CheckUnconstrained,
[switch]$CheckPSSession,
[switch]$ShowDescription,
[switch]$ShowOU,
[switch]$Turbo,
[int[]]$StealthDelay = @(2, 10),
[int]$Threads = 50,
[string[]]$Targets,
[PSCredential]$Credential,
[switch]$CheckLDAP,
[switch]$CheckEOS,
[string]$Report,
[int]$ReportDays = 30
)
$params = @{
ADFilter = 'OperatingSystem -notlike "*Server*"'
}
$PSBoundParameters.Keys | Where-Object { $_ -ne 'ADFilter' } | ForEach-Object { $params[$_] = $PSBoundParameters[$_] }
Get-MachineInventory @params
}
Export-ModuleMember -Function Get-MachineInventory, Get-ServerInventory, Get-ClientInventory, Get-IpsFromCidr, Get-IpsFromRange