This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReset.ps1
155 lines (116 loc) · 5.03 KB
/
Reset.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
param(
[Parameter(Mandatory = $True)][string]$ResourceGroupName,
[Parameter(Mandatory = $True)][string]$Name)
$ErrorActionPreference = "Stop"
function ResetSearch {
param(
[Parameter(Mandatory = $True)][string]$ResourceGroupName,
[Parameter(Mandatory = $True)][string]$ServiceName)
Write-Host "Resetting Search"
$resource = Get-AzResource `
-ResourceType "Microsoft.Search/searchServices" `
-ResourceGroupName $ResourceGroupName `
-ResourceName $ServiceName `
-ApiVersion 2015-08-19
# Get the primary admin API key
$primaryKey = (Invoke-AzResourceAction `
-Action listAdminKeys `
-ResourceId $resource.ResourceId `
-ApiVersion 2015-08-19 `
-Force).PrimaryKey
$headers = @{ "api-key" = $primaryKey }
$url = "https://$ServiceName.search.windows.net/indexes?api-version=2019-05-06"
$response = Invoke-WebRequest -Method GET -Uri $url -ContentType "application/json" -Headers $headers | ConvertFrom-Json
$names = $response.value | Select-Object -Property name
Write-Host "Removing searches..."
$names | ForEach-Object {
$name = $_.name
Write-Host "Deleting $name"
$url = "https://$ServiceName.search.windows.net/indexes/" + $name + "?api-version=2017-11-11"
Write-Host "Invoking $url"
Invoke-WebRequest -Method DELETE -Uri $url -Headers $headers
}
}
function ResetDocumentDb {
param(
[Parameter(Mandatory = $True)][string]$ResourceGroupName,
[Parameter(Mandatory = $True)][string]$AccountName)
function GenerateMasterKeyAuthorizationSignature {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)][String]$verb,
[Parameter(Mandatory = $true)][String]$resourceLink,
[Parameter(Mandatory = $true)][String]$resourceType,
[Parameter(Mandatory = $true)][String]$dateTime,
[Parameter(Mandatory = $true)][String]$key,
[Parameter(Mandatory = $true)][String]$keyType,
[Parameter(Mandatory = $true)][String]$tokenVersion
)
If ($resourceLink -eq $resourceType) {
$resourceLink = ""
}
$hmacSha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacSha256.Key = [System.Convert]::FromBase64String($key)
$payLoad = "$($verb.ToLowerInvariant())`n$($resourceType.ToLowerInvariant())`n$resourceLink`n$($dateTime.ToLowerInvariant())`n`n"
$hashPayLoad = $hmacSha256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($payLoad))
$signature = [System.Convert]::ToBase64String($hashPayLoad);
[System.Web.HttpUtility]::UrlEncode("type=$keyType&ver=$tokenVersion&sig=$signature")
}
Write-Host "Resetting Document DB"
Add-Type -AssemblyName System.Web
$resource = Get-AzResource `
-ResourceType "Microsoft.DocumentDb/databaseAccounts" `
-ResourceGroupName $ResourceGroupName `
-ResourceName $AccountName `
-ApiVersion 2015-04-08
$primaryMasterKey = (Invoke-AzResourceAction `
-Action listKeys `
-ResourceId $resource.ResourceId `
-ApiVersion 2015-04-08 `
-Force).primaryMasterKey
$Verb = "GET"
$ResourceType = "dbs";
$ResourceLink = "dbs"
$dateTime = [DateTime]::UtcNow.ToString("r")
$authHeader = GenerateMasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $primaryMasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
$headers = @{authorization = $authHeader; "x-ms-version" = "2017-02-22"; "x-ms-documentdb-isquery" = "True"; "x-ms-date" = $dateTime }
$contentType = "application/query+json"
$url = "https://$AccountName.documents.azure.com/$ResourceLink"
$response = Invoke-WebRequest -Method $Verb -Uri $url -ContentType $contentType -Headers $headers | ConvertFrom-Json
$ids = $response.Databases | Select-Object -Property id
Write-Host "Removing document databases..."
$ids | ForEach-Object {
$id = $_.id
Write-Host "Deleting $id"
$Verb = "DELETE"
$ResourceType = "dbs";
$ResourceLink = "dbs/$id"
$dateTime = [DateTime]::UtcNow.ToString("r")
$authHeader = GenerateMasterKeyAuthorizationSignature -verb $Verb -resourceLink $ResourceLink -resourceType $ResourceType -key $primaryMasterKey -keyType "master" -tokenVersion "1.0" -dateTime $dateTime
$headers = @{authorization = $authHeader; "x-ms-version" = "2015-08-06"; "x-ms-date" = $dateTime }
$url = "https://$AccountName.documents.azure.com/$ResourceLink"
$response = Invoke-WebRequest -Method $Verb -Uri $url -Headers $headers
$response
}
}
function ResetTableStorage {
param(
[Parameter(Mandatory = $True)][string]$ResourceGroupName,
[Parameter(Mandatory = $True)][string]$AccountName)
Write-Host "Resetting Table Storage"
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $ResourceGroupName `
-Name $AccountName
$ctx = $storageAccount.Context
$names = Get-AzStorageTable -Context $ctx | Select-Object Name
Write-Host "Removing tables..."
$names | ForEach-Object {
$name = $_.Name
Write-Host "Removing $name"
Remove-AzStorageTable -Name $name -Context $ctx -Force
}
}
ResetSearch -ResourceGroupName $ResourceGroupName -ServiceName $Name
ResetDocumentDb -ResourceGroupName $ResourceGroupName -AccountName $Name
ResetTableStorage -ResourceGroupName $ResourceGroupName -AccountName $Name