Home > Software engineering >  Accessing Azure Storage Table from Azure Function App (PowerShell)
Accessing Azure Storage Table from Azure Function App (PowerShell)

Time:10-17

I want to use the following code in a Azure Function powershell app:

Add-AzTableRow `
        -table outputTable`
        -partitionKey $partitionKey `
        -rowKey ($record.id) -property @{"userId" = "001";}

I'm using this documentation as a guide. However, this guide uses Install-Module AzTable. Since I am using a Function App to run this code on a timer, I can't install the module on run time. I've followed this question/answer. I've added this to `requirements.psd1':

@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. 
    # To use the Az module in your function app, please uncomment the line below.
    #'Az' = '8.*'
    AzTable = '2.*'
}

When I run the code I get the following error:

[Error]   ERROR: The 'Add-AzTableRow' command was found in the module 'AzTable', but the module could not be loaded. For more information, run 'Import-Module AzTable'.

Could someone please give me some insight on what I'm doing wrong? I want to be able to update and query the table from the Function App without any user input.

Edit:

I have added 'Az' = '8.*' and 'AzTable' = '2.*'. I let the function install the resource by running and waiting. I'm now getting the error:

[Error]   ERROR: The term 'Add-AzTableRow' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I'm not sure why I'm getting this error because Add-AzTableRow is apart if the AzTable module.

CodePudding user response:

The AzTable module requires the Az Module:

It requires latest PowerShell Az module installed https://www.powershellgallery.com/packages/AzTable/2.0.1

But currently the install of the Az module is disabled in your requirements.psd1:

Remove # from #'Az' = '8.*' = 'Az' = '8.*'

After that the machine behind the function will install the required module and the code will be able to load it/acces the functions.

Note if you "activate" a module the first time, run your code and maybe you still get the error messge -> script got started before module install completed... so simply wait some minutes and retry.

CodePudding user response:

  1. Run VS Code as an Administrator. Open the Azure PowerShell Functions Project in the VS Code.
  2. In the VS Code Terminal of project Workspace/Path, run the below cmdlets one by one:
Install-Module Az
Import-Module Az
Install-Module AzTable -Force
Import-Module AzTable
  1. Created Azure Functions PowerShell HTTP Trigger Function and written the code with the reference of this MS Doc:

run.ps1

using  namespace  System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host  "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
Connect-AzAccount -Tenant '<Tenant-Id>' -SubscriptionId '<Subscription-Id>'
Set-AzContext -Subscription "<Subscription-Id>"

$resourceGroup = "HariTestRG"
$storageAccountName ="store365rvi7b3lmoq"
$storageAccount=Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName

$ctx = $storageAccount.Context
Write-Host  $ctx.ConnectionString

$tableName = "pshkrishtesttable"
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $ctx).CloudTable
Write-Host  $cloudTable.Name

$partitionKey1 = "partition2"
Write-Host  "Partition Key"

# add a row
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey1 `
-rowKey ("India") -property @{"username"="Jashu";"userid"=598}

Write-Host  "Table Row Added"

$TableRows = Get-AzTableRow -table $cloudTable
Write-Host  $TableRows | Format-Table

$body = "Hello Krishna, This HTTP triggered function executed successfully."

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})

requirements.psd1:

@{
'Az' = '8.*'
'Az.Storage' = '4.10.0'
'AzTable' = '2.1.0'
}

Result:

enter image description here

Note: If you are running the PowerShell Function with these modules for the first time, it will take some time during the runtime/execution.

  • Related