-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUpdate-CosmosDocument.ps1
93 lines (82 loc) · 3.29 KB
/
Update-CosmosDocument.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
function Update-CosmosDocument {
<#
.SYNOPSIS
Updates a specific Cosmos Document
.DESCRIPTION
Updates a specific Cosmos Document
.PARAMETER DatabaseName
Name of the Database containing the Collection containing the document you want to update
.PARAMETER CollectionName
Name of the Collection containing the document you want to update
.PARAMETER DocumentId
The DocumentId of the document you want to update
.PARAMETER Document
A hashtable containing the new Document
.PARAMETER CosmosDBVariables
This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
.EXAMPLE
$NewData = @{
GivenName = 'Eternal'
Surname = 'Suffering'
}
Update-CosmosDocument -DatabaseName MyPrivateCosmos -CollectionName Chaos -DocumentId "c3210778-0ac2-4bc8-b0dd-d465192bf2c8" -Document $NewData
.NOTES
https://docs.microsoft.com/en-us/rest/api/documentdb/replace-a-document
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,
HelpMessage='Name of the Database containing the Document')]
[string]$DatabaseName,
[Parameter(Mandatory=$true,
HelpMessage='Name of the Collection containing the Document')]
[string]$CollectionName,
[Parameter(Mandatory=$true,
HelpMessage='Id of the Document')]
[string]$DocumentId,
[Parameter(Mandatory=$true,
HelpMessage='The document formatted as a hashtable')]
[hashtable]$Document,
[Parameter(Mandatory=$false,
HelpMessage="Use Connect-CosmosDB to create this Variable collection")]
[hashtable]$CosmosDBVariables=$Script:CosmosDBVariables
)
begin {
Test-CosmosDBVariable $CosmosDBVariables
$Database = $Script:CosmosDBConnection[($DatabaseName + '_db')]
if (-not $Database) {
Write-Warning "$DatabaseName not found"
continue
}
$Collection = $Script:CosmosDBConnection[$DatabaseName][$CollectionName]
if (-not $Collection) {
Write-Warning "$CollectionName not found"
continue
}
}
process {
$CurrentDocument = Get-CosmosDocument -DatabaseName $DatabaseName -CollectionName $CollectionName -DocumentId $DocumentId
if (-not $CurrentDocument) {
Write-Warning "Document $DocumentID not found in collection $CollectionName in database $DatabaseName"
continue
}
$Verb = 'PUT'
$Url = '{0}/{1}' -f $CosmosDBVariables['URI'],$CurrentDocument._self
$ResourceType = 'docs'
$Header = New-CosmosDBHeader -resourceId $CurrentDocument._rid -resourceType $ResourceType -Verb $Verb
if ($Document.Keys -notcontains 'id'){
$Document['id'] = $DocumentID
Write-Verbose "Adding id: $DocumentID to Document body"
}
$CosmosBody = $Document | ConvertTo-Json
try {
$Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -Body $CosmosBody -ErrorAction Stop
Write-Verbose "$($Document['id']) has been updated in $CollectionName"
}
catch {
Write-Warning -Message $_.Exception.Message
}
}
end {
}
}