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 pathResetDocumentDb.ps1
79 lines (62 loc) · 2.82 KB
/
ResetDocumentDb.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
param(
[Parameter(Mandatory=$True)][string]$ResourceGroupName,
[Parameter(Mandatory=$True)][string]$AccountName)
Add-Type -AssemblyName System.Web
# generate authorization key
Function Generate-MasterKeyAuthorizationSignature
{
[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")
}
$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 = Generate-MasterKeyAuthorizationSignature -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 -Property id
Write-Host "Removing document databases..."
$ids | foreach {
$id = $_.id
Write-Host "Deleting $id"
$Verb = "DELETE"
$ResourceType = "dbs";
$ResourceLink = "dbs/$id"
$dateTime = [DateTime]::UtcNow.ToString("r")
$authHeader = Generate-MasterKeyAuthorizationSignature -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
}