Read and Write into Azure Data Storage Table from PowerShell Azure Function App

Hi folks!

Today I want to share with you simple solution how you could use Azure Data Storage Table inside of your Azure Function App with PowerShell language support.

So we want to read data from Data Storage Table and we want to write data back to Data Storage Table too.

First of all you have to create simple Azure Function App with PowerShell language support. As I mentioned in one of my previous post you have to choose Function App with Runtime version 1.0.

After that you have to create new Azure Data Storage Table. For that you have to go into Storage Accounts section inside of your Azure Portal and create new one or use existing one which is created with your Azure Function App.

After that you could go into Table section inside of it and create new table. I named it UserPropertiesCache. You must remember this name because you will have to use it a lil’bit later in Azure Function App.

After that you have to go into Access keys section of you Storage Account and copy Connection string from it.

Then you can go back into your Azure Function App. Open Application Settings inside of it.

Go to Application Settings section and create new settings with connection string as value. I named it AzureWebJobsStorage. You don’t have to create new one if you want to use Storage Account, which is created automatically with your Azure Function App.

Then you have to go to your Azure Function App function (in my example named GetIPPhone), open View files view and select function.json file.

In that file you have to add two settings -> one for reading and one for writing into Azure Data Storage Table:

{
  "type": "table",
  "name": "outputTable",
  "tableName": "UserPropertiesCache",
  "connection": "AzureWebJobsStorage",
  "direction": "out"
},
{
  "type": "table"
  "name": "inputTable",   
  "tableName": "UserPropertiesCache",
  "connection": "AzureWebJobsStorage",
  "direction": "in"
}

As you can see I have to specify name of Application Settings in which I define connection string to my Azure Data Storage Table – AzureWebJobsStorage. And I have to define name of table inside of Azure Data Storage in which I want to write and read – UserPropertiesCache.

I have to define direction for each of this settings and name – inputTable, outputTable.

After that you can go into PowerShell file of you Azure Function App – run.ps1.

You can write into Azure Data Storage Table with this lines of code:

$tableStorageItems = @()

$tableStorageItems += [PSObject]@{
  PartitionKey = "Prod"  
  RowKey = $id
  IPPhone = $currIPPhone
}

$tableStorageItems | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable

And you can read Azure Data Storage Table with this lines of code:

$tableStorageCurr = $in = Get-Content $inputTable -Raw | ConvertFrom-Json

$currStorageItem = $tableStorageCurr | where RowKey -eq $id

Thats all.

Happy coding folks and see you next time!

Cheers!
Gašper Rupnik

{End.}

Advertisements

One thought on “Read and Write into Azure Data Storage Table from PowerShell Azure Function App

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: