Skip to content
Azure Functions - Using PowerShell To retrieve WordPress blog data using API, storing in Azure Table Storage and randomly Tweeting
Branch: master
Clone or download
KAINOS\thomast Initial Push
Latest commit 8cda227 Mar 18, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
AddBlogPosts/AddBlogPosts Initial Push Mar 18, 2019
BlogPostsToTwitter/BlogPostToTwitter Initial Push Mar 18, 2019
Pre-Requisties.ps1 Initial Push Mar 18, 2019 Initial Push Mar 18, 2019
env_variables.ps1 Initial Push Mar 18, 2019

Azure Functions - Using PowerShell To retrieve WordPress blog data using API, storing in Azure Table Storage and randomly Tweeting

I have created a couple of Azure Functions to Tweet a random Twitter update consisting of one of my blog posts & URLs stored in Azure Table Storage.

Function1: AddBlogPosts - A weekly job that will review the BlogPosts rows in Azure Table Storage and any new Blog Posts that I may have created

Function2: BlogPostToTwitter - Will be ran three times a week, taking a row from Azure Table Storage and using that data to post a Tweet

Getting started

Before deploying the Azure Functions, some pre-requisites are required, these are found in Pre-Requisties.ps1

  1. Create a Resource Group and Storage Account table that will be used by Function1: AddBlogPosts to store the relevant WordPress data
#Create Resource Group & Storage Account Table
    $ResourceGroupName = "tamopsFunctionsTwitter"
    $StorageAccountName = "tamopsfunctionstwittersa"
    $AzureTableName = "tamopsFuncTableBlog"
        az group create --name $ResourceGroupName --location eastus
        az storage account create --name $StorageAccountName --resource-group $ResourceGroupName --location eastus --sku Standard_LRS

        $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName).Context
        New-AzureStorageTable –Name $AzureTableName –Context $saContext
  1. Create a service principle within AzureAD, used in both Functions
    #Create a service principle within AzureAD
    $AzureAdAppPassword = ConvertTo-SecureString '<PASSWORD>' -AsPlainText -Force
    $AzureAdApp = New-AzureRmADApplication -DisplayName "TamOpsFuncADApp" -Password $AzureAdAppPassword -HomePage "" -IdentifierUris ""

    # Create new service principle and apply permissions

        New-AzureRmADServicePrincipal -ApplicationId $AzureAdApp.ApplicationId | New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $AzureAdApp.ApplicationId.Guid
  1. Create the Function App and Service plan (Consumption) along with changing the App service to version 1.0 to allow the use of PowerShell to be deployed by a Function

If you require further Output data from the used api, you can run

# Create Function App and change to version 1
    $FunctionName = "tamopsfunctwitter"
    $ResourceGroupName = "tamopsFunctionsTwitter"
    $StorageAccountName = "tamopsfunctionstwittersa"
    $region = "eastus"

        az functionapp create -n $FunctionName -g $ResourceGroupName --consumption-plan-location $region --storage-account "$StorageAccountName"

        az functionapp config appsettings set -n $FunctionName -g $ResourceGroupName  --settings FUNCTIONS_EXTENSION_VERSION=~1

Deploying Application Settings

Application Settings are encrypted at rest and transmitted over an encrypted channel.

    $ResourceGroupName = "tamopsFunctionsTwitter"
    $FunctionName = "tamopsfunctwitter"

    $AppSettingsHash = @{
    ResourceGroupName = $ResourceGroupName
    StorageAccountName = "tamopsfunctionstwittersa"
    AzureTableName = "tamopsFuncTable"
    partitionKey = "twitterPartKey"
    BlogSiteName = ""
    twitterID= "tamstar1234"
    twitterAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAccessTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAPIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAPISecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    AzureADTenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    AzureADAppUser = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    AzureADAppPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

    foreach ($AppSettingAdd in $AppSettingsHash.GetEnumerator()) {
    az functionapp config appsettings set --resource-group $ResourceGroupName --name $FunctionName --settings "$($AppSettingAdd.Name) = $($AppSettingAdd.Value)"

Twitter access for the below variables are generated by created by Twitter Access Tokens, I used this tutorial to create an app within the Twitter API

    twitterAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAccessTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAPIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitterAPISecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

The AzureAD References are:-

  • AzureADTenantId :- The Azure AD Tenant ID of the subscription
  • AzureADAppUser :- Created above -IdentifierUris
  • AzureADAppPassword :- Created above
    AzureADTenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    AzureADAppUser = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    AzureADAppPassword = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Azure Storage Tables use Partition Keys to assist with uniquely identifying every entity within a table. For this, I have used "twitterPartKey"

	AzureTableAzureTablePartitionKey = "twitterPartKey"

Deploying the Functions

The functions will be deployed in the format

| -AddBlogPosts
| | -function.json
| | -AddBlogPosts.ps1
| | -Modules
| -BlogPostToTwitter
| | -function.json
| | -BlogPostToTwitter.ps1
| | -Modules

What the Functions will do

Function1: AddBlogPosts

  • Function will be ran weekly
  • Uses WordPress API to take blog data and store in Azure Table Storage
  • Blog data will be both URL and Title, allowing the second function to take this data and upload a random Tweet to Twitter
  • Additional columns will be populated with each new blog post being updated to a row "Tweeted = No" and "PartitionKey = twitterPartKey"
  • On a weekly basis reviews what is on each TableRow against current about of WordPress blogs, if there is a difference it will add in the missing.

Function2: BlogPostToTwitter

  • Function will be ran three times a week (Monday, Wednesday and Friday)
  • Takes a random row from the Table Storage and uses this to post a blog to Twitter
  • Each time it does this, it updates the column in row Tweeted to Yes, if no available rows left with No - it will update all rows to No and continue with Tweeting
  • Posts Tweet in format: "From the Blog:"WordPress Blog Post Title,
    WordPress Blog Post URL,"#Microsoft #Azure #AzureFamily #Blog"

Compress folder and deploy to Function App

You should now have two folders "AddBlogPosts" and "BlogPostsToTwitter", create a .zip with both these folders inside

Please note that when you upload this to a function using below CLI, whatever is not in the folder will be removed.

	$FunctionName = "tamopsfunctwitter"
	$ResourceGroupName = "tamopsFunctionsTwitter"

		az functionapp deployment source config-zip -n $FunctionName -g $ResourceGroupName --src ./

Once upload has been completed, these functions will now be viewable within Azure Portal

The output should look..

Lets run a test!

Running Function: AddBlogPosts....
Select AddBlogPosts Function and select Run and view Log Output:-

Review the Azure Storage Table, you can see it is now populated

That's it, you now have Two Azure Functions to post a random Blog post from your WordPress Blog to Twitter!

Running Function: BlogPostToTwitter....
Select BlogPostToTwitter Function select Run and view Log Output:-

Lets check Twitter

Now check the Table Row to see if Tweeted has been updated to Yes

All working :)


You will have two Functions to assist you in posting your WordPress Blogs!

No editing is required for the Functions to work, all you need to ensure is all your Application settings are correct

Read the full blog for further details!

Thanks for MeshkDevs for the Twitter PowerShell Module

You can’t perform that action at this time.