forked from tabs-not-spaces/CodeDump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzTableAPIExample.ps1
More file actions
116 lines (111 loc) · 4.12 KB
/
Copy pathAzTableAPIExample.ps1
File metadata and controls
116 lines (111 loc) · 4.12 KB
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
#region params
$script:storageAccountName = ""
$script:storageAccountkey = "" #Account key - NOT connection string
$script:tableName = ""
#endregion
#region Functions
function New-AzTableHeader {
param (
[parameter(Mandatory = $false)]
$StorageAccountName = $script:storageAccountName,
[parameter(Mandatory = $false)]
$TableName = $script:tableName,
[parameter(Mandatory = $false)]
$StorageAccountkey = $script:storageAccountkey
)
$apiVersion = "2017-04-17"
$GMTime = (Get-Date).ToUniversalTime().toString('R')
$string = "$($GMTime)`n/$($storageAccountName)/$($tableName)"
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Convert]::FromBase64String($storageAccountkey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($string))
$signature = [Convert]::ToBase64String($signature)
@{
Authorization = "SharedKeyLite " + $storageAccountName + ":" + $signature
Accept = "application/json;odata=fullmetadata"
'x-ms-date' = $GMTime
"x-ms-version" = $apiVersion
}
}
function Get-AzTableRowByRowKey {
[cmdletbinding()]
param (
[parameter(Mandatory = $false)]
$StorageAccountName = $script:storageAccountName,
[parameter(Mandatory = $false)]
$StorageAccountKey = $script:storageAccountkey,
[parameter(Mandatory = $false)]
$TableName = $script:tableName,
[parameter(Mandatory = $true)]
$PartitionKey,
[parameter(Mandatory = $true)]
$RowKey
)
$headers = New-AzTableHeader -StorageAccountName $StorageAccountName -TableName $TableName -StorageAccountkey $StorageAccountKey
$tableURL = "https://$($storageAccountName).table.core.windows.net/$($tableName)"
$queryURL = "$($tableURL)?`$filter=(PartitionKey eq '$PartitionKey' and RowKey eq '$RowKey')"
$item = Invoke-RestMethod -Method GET -Uri $queryURL -Headers $headers -ContentType 'application/json'
$item.value
}
function Get-AzTableRowByPartitionKey {
[cmdletbinding()]
param (
[parameter(Mandatory = $false)]
$StorageAccountName = $script:storageAccountName,
[parameter(Mandatory = $false)]
$StorageAccountKey = $script:storageAccountkey,
[parameter(Mandatory = $false)]
$TableName = $script:tableName,
[parameter(Mandatory = $true)]
$PartitionKey
)
$headers = New-AzTableHeader -StorageAccountName $StorageAccountName -TableName $TableName -StorageAccountkey $StorageAccountKey
$tableURL = "https://$($storageAccountName).table.core.windows.net/$($tableName)"
$queryURL = "$($tableURL)?`$filter=(PartitionKey eq '$PartitionKey')"
$item = Invoke-RestMethod -Method GET -Uri $queryURL -Headers $headers -ContentType 'application/json'
$item.value
}
function Merge-AzTableRow {
[cmdletbinding()]
param (
[parameter(Mandatory = $false)]
$StorageAccount = $script:storageAccountName,
[parameter(Mandatory = $false)]
$StorageKey = $script:storageAccountkey,
[parameter(Mandatory = $false)]
$TableName = $script:tableName,
[parameter(Mandatory = $true)]
$PartitionKey,
[parameter(Mandatory = $true)]
$RowKey,
[parameter(Mandatory = $true)]
$Entity
)
$query = "$($TableName)(PartitionKey='$PartitionKey',RowKey='$Rowkey')"
$queryURL = "https://$($storageAccountName).table.core.windows.net/$($query)"
$headers = New-AzTableHeader -StorageAccountName $StorageAccountName -TableName $query -StorageAccountkey $StorageAccountKey
$body = $Entity | ConvertTo-Json
$item = Invoke-RestMethod -Method Merge -Uri $queryURL -Headers $headers -Body $body -ContentType 'application/json'
$item
}
#endregion
#region Example entity
$entity = @{
PropertyA = "ValueA"
PropertyB = "ValueB"
PropertyC = "ValueC"
PropertyD = "ValueD"
PropertyE = "ValueE"
}
#endregion
#region Example API calls
#add row to table (even if it exists)
Merge-AzTableRow -PartitionKey 'Example' -RowKey $entity.PropertyA -Entity $entity
#get all partition results
$items = Get-AzTableRowByPartitionKey -PartitionKey 'Example'
# get single row
$item = Get-AzTableRowByRowKey -PartitionKey 'Example' -RowKey $items[0].RowKey
#update / merge values to existing row
$change = @{PropertyE = "ValueChanged" }
Merge-AzTableRow -PartitionKey 'Example' -RowKey $item.PropertyA -Entity $change
#endregion