-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AD-Sites-Subnets-Report.ps1
211 lines (174 loc) · 6.42 KB
/
Get-AD-Sites-Subnets-Report.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
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
<#
Author: Stan Crider
Date: 6Feb2018
What this crap does:
Get a specified list of sites and subnets from AD
### Must have ImportExcel module installed!!!
### https://github.com/dfinke/ImportExcel
#>
#Requires -Module ImportExcel
## User variables
$Date = Get-Date -Format yyMMdd
$LogFile = "C:\Temp\Domain_Sites_$Date.xlsx"
$Domains = @('production.acme.com'
'development.acme.com'
'acme.com'
)
## Functions
# Retrieve site partners and combine into string output
Function Get-SitePartners{
<#
.SYNOPSIS
Converts AD object array into single comma-separated string
.DESCRIPTION
Takes a provided AD object property with multiple values and combines them into a single string
.EXAMPLE
Get-SitePartners -SiteIncludes (Get-ADReplicationSiteLink).StiesIncluded -DomainName 'development.acme.com'
.INPUTS
String array
.OUTPUTS
String
.NOTES
Author: Stan Crider
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory,
HelpMessage = 'What AD object property array would you like to convert to a single string?')]
[string[]]
$SiteIncludes,
[Parameter(Mandatory,
HelpMessage = 'What domain would you like to retrieve AD objects for?')]
[string]
$DomainName
)
Process{
$SitePartners = @()
ForEach($LSite in $SiteIncludes){
$SitePartners += Get-ADReplicationSite -Server $DomainName -Identity $LSite
}
$Output = $SitePartners.Name -join ", "
$Output
}
}
# Convert number of object items into Excel column headers
Function Get-ColumnName ([int]$ColumnCount){
<#
.SYNOPSIS
Converts integer into Excel column headers
.DESCRIPTION
Takes a provided number of columns in a table and converts the number into Excel header format
Input: 27 - Output: AA
Input: 2 - Ouput: B
.EXAMPLE
Get-ColumnName 27
.INPUTS
Integer
.OUTPUTS
String
.NOTES
Author: Stan Crider and Dennis Magee
#>
If(($ColumnCount -le 702) -and ($ColumnCount -ge 1)){
$ColumnCount = [Math]::Floor($ColumnCount)
$CharStart = 64
$FirstCharacter = $null
# Convert number into double letter column name (AA-ZZ)
If($ColumnCount -gt 26){
$FirstNumber = [Math]::Floor(($ColumnCount)/26)
$SecondNumber = ($ColumnCount) % 26
# Reset increment for base-26
If($SecondNumber -eq 0){
$FirstNumber--
$SecondNumber = 26
}
# Left-side column letter (first character from left to right)
$FirstLetter = [int]($FirstNumber + $CharStart)
$FirstCharacter = [char]$FirstLetter
# Right-side column letter (second character from left to right)
$SecondLetter = $SecondNumber + $CharStart
$SecondCharacter = [char]$SecondLetter
# Combine both letters into column name
$CharacterOutput = $FirstCharacter + $SecondCharacter
}
# Convert number into single letter column name (A-Z)
Else{
$CharacterOutput = [char]($ColumnCount + $CharStart)
}
}
Else{
$CharacterOutput = "ZZ"
}
# Output column name
$CharacterOutput
}
## Script below
$Subnets = @()
$Sites = @()
$SiteLinks = @()
ForEach($Domain in $Domains){
# Subnets
$SubnetsRaw = Get-ADReplicationSubnet -Server $Domain -Filter * -Properties Name,Location,Site,Description | Sort-Object Name
ForEach($Subnet in $SubnetsRaw){
$SubnetSite = $null
If($Subnet.Site){
$SubnetSite = Get-SitePartners -SiteIncludes $Subnet.Site -DomainName $Domain
}
$Subnets += [PSCustomObject]@{
'Subnet' = $Subnet.Name
'Location' = $Subnet.Location
'Description' = $Subnet.Description
'Site' = $SubnetSite
'Domain' = $Domain
}
}
# Sites
$SitesRaw = Get-ADReplicationSite -Server $Domain -Filter * -Properties Name,Location,Description | Sort-Object Name
ForEach($SiteObj in $SitesRaw){
$Sites += [PSCustomObject]@{
'Name' = $SiteObj.Name
'Location' = $SiteObj.Location
'Description' = $SiteObj.Description
'Domain' = $Domain
}
}
# Site links
$SiteLinksRaw = Get-ADReplicationSiteLink -Server $Domain -Filter * -Properties Name,Description,Cost,ReplicationFrequencyInMinutes,SitesIncluded | Sort-Object Name
ForEach($SiteLink in $SiteLinksRaw){
$MemberSites = $null
If($SiteLink.SitesIncluded){
$MemberSites = Get-SitePartners -SiteIncludes $SiteLink.SitesIncluded -DomainName $Domain
}
$SiteLinks += [PSCustomObject]@{
'Name' = $SiteLink.Name
'Description' = $SiteLink.Description
'Cost' = $SiteLink.Cost
'RepFreq' = $SiteLink.ReplicationFrequencyInMinutes
'Member Sites' = $MemberSites
'Domain' = $Domain
}
}
}
## Output
# Create Excel standard configuration properties
$ExcelProps = @{
Autosize = $true;
FreezeTopRow = $true;
BoldTopRow = $true;
}
$ExcelProps.Path = $LogFile
# Subnets sheet
$SubnetHeaderCount = Get-ColumnName ($Subnets | Get-Member | Where-Object{$_.MemberType -match "NoteProperty"} | Measure-Object).Count
$SubnetHeaderRow = "`$A`$1:`$$SubnetHeaderCount`$1"
$SubnetSheetStyle = New-ExcelStyle -Range "'File Servers'$SubnetHeaderRow" -HorizontalAlignment Center
$Subnets | Export-Excel @ExcelProps -WorkSheetname "Subnets" -Style $SubnetSheetStyle
# Sites sheet
$SitesHeaderCount = Get-ColumnName ($Sites | Get-Member | Where-Object{$_.MemberType -match "NoteProperty"} | Measure-Object).Count
$SitesHeaderRow = "`$A`$1:`$$SitesHeaderCount`$1"
$SitesSheetStyle = New-ExcelStyle -Range "'File Servers'$SitesHeaderRow" -HorizontalAlignment Center
$Sites | Export-Excel @ExcelProps -WorkSheetname "Sites" -Style $SitesSheetStyle
# SiteLinks sheet
$SiteLinkHeaderCount = Get-ColumnName ($SiteLinks | Get-Member | Where-Object{$_.MemberType -match "NoteProperty"} | Measure-Object).Count
$SiteLinkHeaderRow = "`$A`$1:`$$SiteLinkHeaderCount`$1"
$SiteLinkSheetStyle = New-ExcelStyle -Range "'File Servers'$SiteLinkHeaderRow" -HorizontalAlignment Center
$SiteLinks | Export-Excel @ExcelProps -WorkSheetname "Links" -Style $SiteLinkSheetStyle