PS: Write To Table Storage
Description
Azure Storage Accounts can host table storage that scripts can use for temp or long term placement. In the move to replace VRO with Azure Automation, we decided to treat table storage similar to that of Resource Elements in VRO.
To Resolve
-
Here are functions you can use:
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 156 157
Function Get-StorageAccountName { $cred = Get-AutomationPSCredential -Name 'storage info' $val = $cred.UserName return $val } Function Get-StorageAccountKey { $cred = Get-AutomationPSCredential -Name 'storage info' $val = $cred.GetNetworkCredential().Password return $val } $storageAccount = (Get-StorageAccountName) $accesskey = (Get-StorageAccountKey) function Get-AllRows($TableName) { $version = "2017-04-17" $resource = "$tableName" $table_url = "https://$storageAccount.table.core.windows.net/$resource" $GMTTime = (Get-Date).ToUniversalTime().toString('R') $stringToSign = "$GMTTime`n/$storageAccount/$resource" $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Convert]::FromBase64String($accesskey) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)) $signature = [Convert]::ToBase64String($signature) $headers = @{ 'x-ms-date' = $GMTTime Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature "x-ms-version" = $version Accept = "application/json;odata=fullmetadata" } $item = Invoke-RestMethod -Method GET -Uri $table_url -Headers $headers -ContentType application/json return $item.value } # Write-Output "Getting all rows" # $tableItems = Get-AllRows -TableName "Workflows" function Get-SingleRow($TableName, $PartitionKey, $RowKey) { $version = "2017-04-17" $resource = "$tableName(PartitionKey='$PartitionKey',RowKey='$Rowkey')" $table_url = "https://$storageAccount.table.core.windows.net/$resource" $GMTTime = (Get-Date).ToUniversalTime().toString('R') $stringToSign = "$GMTTime`n/$storageAccount/$resource" $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Convert]::FromBase64String($accesskey) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)) $signature = [Convert]::ToBase64String($signature) $headers = @{ 'x-ms-date' = $GMTTime Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature "x-ms-version" = $version Accept = "application/json;odata=fullmetadata" } $item = Invoke-RestMethod -Method GET -Uri $table_url -Headers $headers -ContentType application/json return $item.value } # Write-Output "Getting single row" # $tableItem = Get-SingleRow -TableName "Workflows" -RowKey "example-service-now-info" -PartitionKey "REQ00000346" function New-Row($TableName, $PartitionKey, $RowKey, $Entity) { $version = "2017-04-17" $resource = "$tableName(PartitionKey='$PartitionKey',RowKey='$Rowkey')" $table_url = "https://$storageAccount.table.core.windows.net/$resource" $GMTTime = (Get-Date).ToUniversalTime().toString('R') $stringToSign = "$GMTTime`n/$storageAccount/$resource" $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Convert]::FromBase64String($accesskey) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)) $signature = [Convert]::ToBase64String($signature) $headers = @{ 'x-ms-date' = $GMTTime Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature "x-ms-version" = $version Accept = "application/json;odata=fullmetadata" } $body = $entity | ConvertTo-Json $item = Invoke-RestMethod -Method PUT -Uri $table_url -Headers $headers -Body $body -ContentType application/json } <# Write-Output "Creating a new table entity" $body = @{ RowKey = "example-service-now-info" PartitionKey = "REQ00000346" Status = "waiting" Value = '{ "blah" = "bob"}' } New-Row -TableName "Workflows" -RowKey "example-service-now-info" -PartitionKey "REQ00000346" -entity $body #> function Update-Row($TableName, $PartitionKey, $RowKey, $entity) { $version = "2017-04-17" $resource = "$tableName(PartitionKey='$PartitionKey',RowKey='$Rowkey')" $table_url = "https://$storageAccount.table.core.windows.net/$resource" $GMTTime = (Get-Date).ToUniversalTime().toString('R') $stringToSign = "$GMTTime`n/$storageAccount/$resource" $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Convert]::FromBase64String($accesskey) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)) $signature = [Convert]::ToBase64String($signature) $body = $entity | ConvertTo-Json $headers = @{ 'x-ms-date' = $GMTTime Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature "x-ms-version" = $version Accept = "application/json;odata=minimalmetadata" 'If-Match' = "*" 'Content-Length' = $body.length } $item = Invoke-RestMethod -Method MERGE -Uri $table_url -Headers $headers -ContentType application/json -Body $body } <# Write-Output "Merging with an existing table entity" $body = @{ RowKey = "example-service-now-info" PartitionKey = "REQ00000346" Status = "waiting" Value = '{ "blah" = "bob"}' } Update-Row -TableName "Workflows" -RowKey "example-service-now-info" -PartitionKey "REQ00000346" -entity $body #> function Remove-Row($TableName, $PartitionKey, $RowKey) { $version = "2017-04-17" $resource = "$tableName(PartitionKey='$PartitionKey',RowKey='$Rowkey')" $table_url = "https://$storageAccount.table.core.windows.net/$resource" $GMTTime = (Get-Date).ToUniversalTime().toString('R') $stringToSign = "$GMTTime`n/$storageAccount/$resource" $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Convert]::FromBase64String($accesskey) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign)) $signature = [Convert]::ToBase64String($signature) $headers = @{ 'x-ms-date' = $GMTTime Authorization = "SharedKeyLite " + $storageAccount + ":" + $signature "x-ms-version" = $version Accept = "application/json;odata=minimalmetadata" 'If-Match' = "*" } $item = Invoke-RestMethod -Method DELETE -Uri $table_url -Headers $headers -ContentType application/http } # Write-Output "Deleting an existing table row" # Remove-Row -TableName "Workflows" -RowKey "example-service-now-info" -PartitionKey "REQ00000346"
Comments