-
Notifications
You must be signed in to change notification settings - Fork 4
/
Get-CosmosCollection.ps1
82 lines (69 loc) · 2.58 KB
/
Get-CosmosCollection.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
function Get-CosmosCollection {
<#
.SYNOPSIS
Get a CosmosDB Collection
.DESCRIPTION
Gets a Cosmos Collection in the desired Database
.PARAMETER DatabaseName
The name of the Database containing your Collection
.PARAMETER CollectionName
The name of the Collection you want to retrieve if you are looking for a specific Collection
.PARAMETER All
This switch returns all the Collections in the Database
.PARAMETER CosmosDBVariables
This is the Script variable generated by Connect-CosmosDB - no need to supply this variable, unless you get really creative
.EXAMPLE
Get-CosmosCollection -DatabaseName MyPrivateCosmos -All
Returns all Collections in MyPrivateCosmos
.EXAMPLE
Get-CosmosCollection -DatabaseName MyPrivateCosmos -CollectionName Chaos
Returns the Chaos Collection from MyPrivateCosmos
.NOTES
https://docs.microsoft.com/en-us/rest/api/documentdb/list-collections
https://docs.microsoft.com/en-us/rest/api/documentdb/get-a-collection
#>
[CmdletBinding(DefaultParameterSetName='Named')]
param (
[Parameter(Mandatory=$true,
HelpMessage='Name of the Database containing the Collection')]
[string]$DatabaseName,
[Parameter(ParameterSetName='Named',
Mandatory=$true,
HelpMessage='Name of the Collection')]
[string]$CollectionName,
[Parameter(ParameterSetName='All')]
[switch]$All,
[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
}
$Verb = 'GET'
$Url = '{0}/{1}/colls' -f $CosmosDBVariables['URI'],$Database._self
$ResourceType = 'colls'
$Header = New-CosmosDBHeader -resourceId $Database._rid -resourceType $ResourceType -Verb $Verb
try {
$Return = Invoke-RestMethod -Uri $Url -Headers $Header -Method $Verb -ErrorAction Stop
}
catch {
Write-Warning -Message $_.Exception.Message
continue
}
if ($All) {
$Return.DocumentCollections
}
if ($CollectionName){
$Return.DocumentCollections | Where-Object {$_.id -match $CollectionName}
}
}
process {
}
end {
}
}