-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilScript.ps1
239 lines (218 loc) · 9.11 KB
/
UtilScript.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Function Get-PartitionDict
{
[CmdletBinding(PositionalBinding = $false)]
param([Parameter(Mandatory=$true)][System.Collections.ArrayList]$pathsList
)
$partitionDict = New-Object 'system.collections.generic.dictionary[[string],[system.collections.generic.list[string]]]'
foreach($path in $pathsList)
{
$pathList = $path.Split("\",[StringSplitOptions]'RemoveEmptyEntries')
$length = $pathList.Count
$partitionID = $null
if($length -le 1)
{
$pathList = $path.Split("/",[StringSplitOptions]'RemoveEmptyEntries')
$length = $pathList.Count
if($length -le 1)
{
throw "$path is not in correct format."
}
Else
{
$partitionID = $pathList[$length - 2]
}
}
Else {
$partitionID = $pathList[$length - 2]
}
if($partitionID -eq $null)
{
throw "Not able to extract partitionID"
}
if(!$partitionDict.ContainsKey($partitionID))
{
Write-Host "Partition Id extracted is this $partitionID"
$partitionDict.Add($partitionID, $path)
}
else {
$partitionDict[$partitionID].add($path)
}
}
return $partitionDict
}
Function Get-FinalDateTimeBefore
{
[CmdletBinding(PositionalBinding = $false)]
param([Parameter(Mandatory=$true)][string]$DateTimeBefore,
[Parameter(Mandatory=$true)][string]$Partitionid,
[Parameter(Mandatory=$true)][string]$ClusterEndpoint,
[Parameter(Mandatory=$false)][bool]$Force,
[Parameter(Mandatory=$false)][string]$SSLCertificateThumbPrint
)
# DateTime Improvement to be done here.
$dateTimeBeforeObject = [DateTime]::ParseExact($DateTimeBefore,"yyyy-MM-dd HH.mm.ssZ",[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)
$finalDateTimeObject = $dateTimeBeforeObject
$dateTimeBeforeString = $dateTimeBeforeObject.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$url = "http://$ClusterEndpoint/Partitions/$Partitionid/$/GetBackups?api-version=6.2-preview&EndDateTimeFilter=$dateTimeBeforeString"
$backupEnumerations = $null
try {
Write-Host "Querying the URL: $url"
if($SSLCertificateThumbPrint)
{
$pagedBackupEnumeration = Invoke-RestMethod -Uri $url -CertificateThumbprint $SSLCertificateThumbPrint
}
else {
Write-Host "Trying to query without cert thumbprint"
$pagedBackupEnumeration = Invoke-RestMethod -Uri $url
}
Write-Host "Trying to find sorted list of backupEnumerations from paged object."
$backupEnumerations = $pagedBackupEnumeration.Items | Sort-Object -Property @{Expression = {[DateTime]::ParseExact($_.CreationTimeUtc,"yyyy-MM-ddTHH:mm:ssZ",[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)}; Ascending = $false}
}
catch {
$err = $_.ToString() | ConvertFrom-Json
if($err.Error.Code -eq "FABRIC_E_PARTITION_NOT_FOUND")
{
Write-Host "$Partitionid is not found."
if($Force -eq $true)
{
Write-Host "Force flag is enabled so, deleting data all in this partition"
return [DateTime]::MaxValue
}
else {
Write-Host "If you want to delete the data in this partition."
Write-Host "If you want to remove this partition as well, please run the script by enabling force flag."
return [DateTime]::MinValue
}
}
else {
throw $_.Exception.Message
}
}
Write-Host "Finding the finalDateTime in backupEnumerations."
Write-Host "Iterating over backupEnumerations till we find the last full backup"
$fullBackupFound = $false
foreach($backupEnumeration in $backupEnumerations)
{
Write-Host $backupEnumeration.BackupType
if($backupEnumeration.BackupType -eq "Full")
{
Write-Host "Full backup is found."
$finalDateTimeObject = [DateTime]::Parse($backupEnumeration.CreationTimeUtc)
$fullBackupFound = $true
break
}
}
if($backupEnumerations.Count -eq 0)
{
Write-Host "The BackupEnumerations had length equal to 0. So, not deleting anything."
return [DateTime]::MinValue
}
if(!$fullBackupFound)
{
Write-Host "The Backups Before this $dateTimeBeforeString date are corrupt as no full backup is found, So, deleting them."
}
return $finalDateTimeObject
}
Function Get-PartitionIdList
{
[CmdletBinding(PositionalBinding = $false)]
param([Parameter(Mandatory=$false)][string]$ApplicationId,
[Parameter(Mandatory=$false)][string]$ServiceId
)
# need to add continuationToken Logic here.
$serviceIdList = New-Object System.Collections.ArrayList
if($ApplicationId)
{
$serviceIdList = Get-ServiceIdList -ApplicationId $ApplicationId
}
else {
$serviceIdList.Add($ServiceId) | Out-Null
}
$partitionIdList = New-Object System.Collections.ArrayList
Write-Host "Finding partitionID list"
foreach($serviceId in $serviceIdList)
{
Write-Host "$serviceId"
$continuationToken = $null
do
{
$partitionInfoList = Invoke-RestMethod "http://localhost:19080/Services/$serviceId/$/GetPartitions?api-version=6.2&ContinuationToken=$continuationToken"
foreach($partitionInfo in $partitionInfoList.Items)
{
$partitionid = $partitionInfo.PartitionInformation.Id
Write-Host "$partitionid"
$partitionIdList.Add($partitionInfo.PartitionInformation.Id)
}
$continuationToken = $partitionInfoList.ContinuationToken
}while($continuationToken -ne "")
}
$length = $partitionIdList.Count
Write-Host "the total number of partitions found are $length"
return $partitionIdList
}
Function Get-ServiceIdList
{
[CmdletBinding(PositionalBinding = $false)]
param([Parameter(Mandatory=$true)][string]$ApplicationId
)
Write-Host "Trying to find the service ID list."
# need to add continuationToken Logic here.
$continuationToken = $null
$serviceIdList = New-Object System.Collections.ArrayList
do
{
$serviceInfoList = Invoke-RestMethod "http://localhost:19080/Applications/$ApplicationId/$/GetServices?api-version=6.2&ContinuationToken=$continuationToken"
foreach($serviceInfo in $serviceInfoList.Items)
{
$serviceIdList.Add($serviceInfo.Id) | Out-Null
$serviceId = $serviceInfo.Id
Write-Host "$serviceId"
}
$continuationToken = $serviceInfoList.ContinuationToken
}while($continuationToken -ne "")
$length = $serviceIdList.Count
Write-Host "$ApplicationId has $length number of services"
return $serviceIdList
}
Function Start-BackupDataCorruptionTest
{
[CmdletBinding(PositionalBinding = $false)]
param([Parameter(Mandatory=$true)][string]$DateTimeBefore,
[Parameter(Mandatory=$true)][string]$Partitionid,
[Parameter(Mandatory=$true)][string]$ClusterEndpoint,
[Parameter(Mandatory=$false)][string]$SSLCertificateThumbPrint
)
$dateTimeBeforeObject = [DateTime]::ParseExact($DateTimeBefore,"yyyy-MM-dd HH.mm.ssZ",[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)
$finalDateTimeObject = $dateTimeBeforeObject
$dateTimeBeforeString = $dateTimeBeforeObject.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
# DateTime Improvement to be done here.
$url = "http://$ClusterEndpoint/Partitions/$Partitionid/$/GetBackups?api-version=6.2-preview&EndDateTimeFilter=$dateTimeBeforeString"
Write-Host "$url"
$backupEnumerations = $null
try {
Write-Host "Querying the URL: $url"
if($SSLCertificateThumbPrint)
{
$pagedBackupEnumeration = Invoke-RestMethod -Uri $url -CertificateThumbprint $SSLCertificateThumbPrint
}
else {
$pagedBackupEnumeration = Invoke-RestMethod -Uri $url
}
Write-Host "Trying to find sorted list of backupEnumerations from paged object."
$backupEnumerations = $pagedBackupEnumeration.Items | Sort-Object -Property @{Expression = {[DateTime]::ParseExact($_.CreationTimeUtc,"yyyy-MM-ddTHH:mm:ssZ",[System.Globalization.DateTimeFormatInfo]::InvariantInfo,[System.Globalization.DateTimeStyles]::None)}; Ascending = $true}
if($backupEnumerations -ne $null -and $backupEnumerations[0].BackupType -ne "Full")
{
throw "Data is corrupted for this partition : $Partitionid"
}
}
catch {
$err = $_.ToString() | ConvertFrom-Json
if($err.Error.Code -eq "FABRIC_E_PARTITION_NOT_FOUND")
{
Write-Host "Partition not found, so, leaving the partition as it is."
}
else {
throw $_.Exception.Message
}
}
}