-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-DFSR-Status-Report.ps1
221 lines (196 loc) · 7.07 KB
/
Get-DFSR-Status-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
212
213
214
215
216
217
218
219
220
221
<#
Author: Stan Crider
Date: 7Jul2021
Crap:
Lists DFSR folders and status on specified servers and outputs to Excel
### Must have ImportExcel module installed!!!
### https://github.com/dfinke/ImportExcel
#>
## Functions and script prep
# Configure variables
$RepServers = @()
$Errors = @()
$Date = Get-Date -Format yyyyMMdd
$LogFile = "C:\DFS\DFS_Report_$Date.xlsx"
$RepServers = "ServerFS01","ServerFS02"
# Class: worksheet properties
Class DfsrInfo{
[string]$Server
[string]$GroupName
[string]$FolderName
[string]$ContentPath
[string]$PrimaryMember
[string]$StagingQuota
[string]$State
[string]$Error
}
# Function: Convert raw data size to legible output
Function Get-Size([double]$DataSize){
Switch($DataSize){
{$_ -lt 1KB}{
$DataValue = "$DataSize B"
}
{($_ -ge 1KB) -and ($_ -lt 1MB)}{
$DataValue = "{0:N2}" -f ($DataSize/1KB) + " KiB"
}
{($_ -ge 1MB) -and ($_ -lt 1GB)}{
$DataValue = "{0:N2}" -f ($DataSize/1MB) + " MiB"
}
{($_ -ge 1GB) -and ($_ -lt 1TB)}{
$DataValue = "{0:N2}" -f ($DataSize/1GB) + " GiB"
}
{($_ -ge 1TB) -and ($_ -lt 1PB)}{
$DataValue = "{0:N2}" -f ($DataSize/1TB) + " TiB"
}
Default{
$DataValue = "{0:N2}" -f ($DataSize/1PB) + " PiB"
}
}
$DataValue
}
# Function: Set column count for spreadsheet output
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
}
# Function: format and output to Excel
Function Invoke-ExcelOutput{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[DfsrInfo[]]
$WorksheetData,
[Parameter(Mandatory = $true)]
[string]
$WorksheetName,
[Parameter(Mandatory = $true)]
[string]
$WorkbookName
)
$ExcelProps = @{
Autosize = $true;
FreezeTopRow = $true;
BoldTopRow = $true;
}
$ExcelProps.Path = $WorkbookName
$DataLastRow = ($WorksheetData | Measure-Object).Count + 1
If($DataLastRow -gt 1){
$DataConditionalText = @()
$DataHeaderCount = Get-ColumnName ($WorksheetData | Get-Member | Where-Object{$_.MemberType -match "Property"} | Measure-Object).Count
$DataHeaderRow = "`$$WorksheetName!`$A`$1:`$$DataHeaderCount`$1"
$StateRange = "`$$WorksheetName!`$G`$2:`$G`$$DataLastRow"
$DataStyle = New-ExcelStyle -Range $DataHeaderRow -HorizontalAlignment Center
$DataConditionalText += New-ConditionalText -Range $StateRange -ConditionalType ContainsText "Unknown" -ConditionalTextColor Maroon -BackgroundColor Pink
$DataConditionalText += New-ConditionalText -Range $StateRange -ConditionalType ContainsText "In Error" -ConditionalTextColor Maroon -BackgroundColor Pink
$DataConditionalText += New-ConditionalText -Range $StateRange -ConditionalType ContainsText "Auto Recovery" -ConditionalTextColor Brown -BackgroundColor Wheat
$WorksheetData | Export-Excel @ExcelProps -WorkSheetname $WorksheetName -Style $DataStyle
}
}
# Convert DFSR status to legible output
$DFSRStatus = @{
[byte]0="Uninitialized";
[byte]1="Initialized";
[byte]2="Initial Sync";
[byte]3="Auto Recovery";
[byte]4="Normal";
[byte]5="In Error"
}
## Script below
If(Test-Path $LogFile){
Write-Warning "The file $LogFile already exists. Script terminated."
}
Else{
ForEach($RepServer in $RepServers){
# Prep output array
$DfsrInfo = @()
If(-Not(Test-Connection -ComputerName $RepServer -Quiet -Count 2)){
$Errors += [DfsrInfo]@{
"Server" = $RepServer
"Error" = "Server $RepServer is not responding to ping."
}
Continue
}
Else{
Try{
$DfsFldrs = Get-CimInstance -Namespace "Root\MicrosoftDFS" DfsrReplicatedFolderInfo -ComputerName $RepServer -ErrorAction Stop
$DfsrMembershipGroups = Get-DfsrMembership -ComputerName $RepServer -ErrorAction Stop
ForEach($DfsrGroup in $DfsrMembershipGroups){
$DfsFldr = $null
$DfsFldr = $DfsFldrs | Where-Object{$_.ReplicationGroupName -eq $DfsrGroup.GroupName}
If($DfsFldr){
$DFSRState = $DFSRStatus[$DfsFldr.State]
}
Else{
$DFSRState = "Unknown"
}
$DfsrInfo += [DfsrInfo]@{
"Server" = $DfsrGroup.ComputerName
"GroupName" = $DfsrGroup.GroupName
"FolderName" = $DfsrGroup.FolderName
"ContentPath" = $DfsrGroup.ContentPath
"PrimaryMember" = $DfsrGroup.PrimaryMember
"StagingQuota" = Get-Size ($DfsrGroup.StagingPathQuotaInMB*1MB)
"State" = $DFSRState
}
}
Invoke-ExcelOutput -WorksheetData ($DfsrInfo | Sort-Object "GroupName") -WorksheetName $RepServer -WorkbookName $LogFile
}
Catch{
$Errors += [DfsrInfo]@{
"Server" = $RepServer
"Error" = $_.Exception.Message
}
}
}
}
# Create error worksheet and add to Excel workbook if necessary
If($Errors){
Invoke-ExcelOutput -WorksheetData $Errors -WorksheetName "Errors" -WorkbookName $LogFile
}
}