forked from AndrewEllis93/PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discover-Shares.ps1
132 lines (111 loc) · 4.55 KB
/
Discover-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
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
####################################################
#
# Title: Discover-Shares
# Date Created : 2017-10-31
# Last Edit: 2017-12-29
# Author : Andrew Ellis
# GitHub: https://github.com/AndrewEllis93/PowerShell-Scripts
#
# This function discovers all Windows Servers from Active Directory and discovers their file shares using WMI.
#
####################################################
Function Discover-Shares {
<#
.SYNOPSIS
This function discovers all Windows Servers from Active Directory and discovers their file shares using WMI.
.DESCRIPTION
The following are always excluded:
- Admin shares
- NETLOGON
- SYSVOL
- print$
- prnproc$
- ADMIN$
FilterShares is enabled by default.
Filter removes:
- *Sophos*
- *SMS*
- Wsus*
- SHARES
- REMINST
- *ClusterStorage$
- *SCCM*
.EXAMPLE
Find-Shares -DomainController DC1 -FilterShares $True
.LINK
.NOTES
Title: Discover-Shares
Date Created : 2017-10-31
Last Edit: 2017-12-29
Author : Andrew Ellis
GitHub: https://github.com/AndrewEllis93/PowerShell-Scripts
#>
param([boolean]$FilterShares = $True,
[string]$DomainContoller)
If (!$DomainController){$Servers = Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"} | Where-Object {$_.Name -notlike "ENTDP*"} | Sort-Object Name}
Else {$Servers = Get-ADComputer -Server $DomainContoller -Filter {OperatingSystem -Like "Windows Server*"} | Where-Object {$_.Name -notlike "ENTDP*"} | Sort-Object Name}
$ServerCount = $Servers.Count
$Iteration = 1
$Output = @()
$FailServers = @()
$Servers | ForEach-Object {
$Server = $_.Name
$Fail = $False
$WMI = $null
Try {
if ($FilterShares){
$WMI = get-WmiObject -class Win32_Share -computer $_.Name -ErrorAction Stop | Where-Object {`
$_.Name -notlike "?$" -and `
$_.Name -notlike "*Sophos*" -and `
$_.Name -notlike "*SCCM*" -and `
$_.Name -notlike "*ClusterStorage$" -and `
$_.Name -notlike "SMS*" -and `
$_.Name -notlike "Wsus*" -and `
$_.Name -ne "ADMIN$" -and `
$_.Name -ne "print$" -and `
$_.Name -ne "prnproc$" -and `
$_.Name -ne "NETLOGON" -and `
$_.Name -ne "SYSVOL" -and `
$_.Name -ne "SHARES" -and `
$_.Name -ne "REMINST" -and `
$_.Path -like "?:\*"}
}
Else {
$WMI = get-WmiObject -class Win32_Share -computer $_.Name -ErrorAction Stop | Where-Object {`
$_.Name -notlike "?$" -and `
$_.Path -like "?:\*"}
}
}
Catch {
$Fail = $True
Write-Warning ($Server + " discovery failed.")
Write-Error $Error[0]
}
If ($WMI){
$WMI | ForEach-Object {
$OutputObj = New-Object -TypeName PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Server' -Value $Server
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Share' -Value $_.Name
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Path' -Value $_.Path
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Description' -Value $_.Description
$Output += $OutputObj
}
}
If ($Fail){
$FailServers += $_.Name
$OutputObj = New-Object -TypeName PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Server' -Value $Server
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Share' -Value "FAIL"
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Path' -Value "FAIL"
$OutputObj | Add-Member -MemberType NoteProperty -Name 'Description' -Value "FAIL"
$Output += $OutputObj
}
$PercentComplete = [math]::Round((($Iteration / $ServerCount) * 100),0)
If ($PercentComplete -lt 100){Write-Progress -Activity "Scanning AD servers for shares" -Status "$PercentComplete% Complete ($Iteration/$ServerCount)" -PercentComplete $PercentComplete}
Else {Write-Progress -Activity "Scanning AD servers for shares" -Status "$PercentComplete% Complete ($Iteration/$ServerCount)" -PercentComplete $PercentComplete -Completed}
$Iteration++
}
Return $Output
}
$Results = Discover-Shares -FilterShares $False
$Results | Export-CSV C:\Temp\Shares.csv -NoTypeInformation