-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc_shares.ps1
89 lines (73 loc) · 2.72 KB
/
dc_shares.ps1
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
param (
[Parameter(Mandatory=$true)]
[string]$DomainName
)
# Function to get the list of domain controllers
function Get-DomainControllers {
param (
[Parameter(Mandatory=$true)]
[string]$DomainName
)
Write-Host "Enumerating domain controllers for domain: $DomainName" -ForegroundColor Cyan
try {
$output = nltest /dclist:$DomainName
$domainControllers = $output | Select-String -Pattern '^\s+(\S+)' | ForEach-Object { $_.Matches[0].Groups[1].Value }
if ($domainControllers) {
Write-Host "Found domain controllers:" -ForegroundColor Green
$domainControllers | ForEach-Object { Write-Host $_ }
} else {
Write-Host "No domain controllers found." -ForegroundColor Red
}
} catch {
Write-Host ("Failed to enumerate domain controllers: $($_.Exception.Message)") -ForegroundColor Red
}
return $domainControllers
}
# Function to query shared folders using LDAP
function Query-SharesLDAP {
param (
[Parameter(Mandatory=$true)]
[string]$Server
)
Write-Host "Querying shares on server: $Server using LDAP" -ForegroundColor Cyan
$shareList = @()
try {
$searcher = New-Object DirectoryServices.DirectorySearcher
$searcher.SearchRoot = "LDAP://$Server"
$searcher.Filter = "(objectClass=volume)"
$searcher.PropertiesToLoad.Add("name") | Out-Null
$searcher.PropertiesToLoad.Add("remotePath") | Out-Null
$results = $searcher.FindAll()
foreach ($result in $results) {
$shareName = $result.Properties["name"]
$remotePath = $result.Properties["remotePath"]
if ($shareName -and $remotePath) {
$shareList += [PSCustomObject]@{
ShareName = $shareName
Path = $remotePath
Domain = $Server
}
}
}
} catch {
Write-Host ("Failed to query shares on server ${Server} using LDAP: $($_.Exception.Message)") -ForegroundColor Red
}
return $shareList
}
# Main script
$domainControllers = Get-DomainControllers -DomainName $DomainName
$allShares = @()
if ($domainControllers) {
foreach ($dc in $domainControllers) {
$dcName = $dc.TrimStart('\')
$allShares += Query-SharesLDAP -Server $dcName
}
if ($allShares) {
Write-Host "Listed shares:" -ForegroundColor Green
$allShares | Format-Table -AutoSize
} else {
Write-Host "No shares found." -ForegroundColor Red
}
} else {
Write-Host "No domain controllers to enumerate." -ForegroundColor Red
}