how to read cosmos db table entities from powershell function

Muralidhar Kumar 1 Reputation point

Hi Team,

I have a cosmos DB table in which I have created few entities. I need to read these entitiy values in Azure Powershell function.
So, I have created a cosmos DB trigger function which created a sample function with the code as below which will print latest modified entity.

Input bindings are passed in via param block.

param($Documents, $TriggerMetadata)

if ($Documents.Count -gt 0) {
Write-Host "Document Id: $($Documents[0].id)"

My requirement is to fetch the values from existing entities. If I give Write-Host "$Documents[0].accountEmail" to fetch the value of accountEmail from cosmos table I didnt get any value. Instead it returned null value.
Earlier we are able to access the envType by using $Documents[0].envType for cosmos DB SQL trigger But now facing issue with cosmos DB which is nosql.

SO, I tried alternate approach by importing module with the below commands.

Import-Module AzTable
Import-Module Az.Storage
$cloudTable = "my-table"
Get-AzTableRow -table $cloudTable | ft

I got below error. Not sure how to access cosmos db table entities from azure funtion.
Also, I am unable to perform any operation like New-AzManagementGroup -GroupName 'Contoso' mentioned in the link after installing all the modules. Please guide us how to fix this and can use powershell commands in Azure function.

2021-09-06T06:08:37.204 [Error] ERROR: Method invocation failed because [System.String] does not contain a method named 'ExecuteQuerySegmentedAsync'.Exception :Type : System.Management.Automation.RuntimeExceptionErrorRecord :Exception :Type : System.Management.Automation.ParentContainsErrorRecordExceptionMessage : Method invocation failed because [System.String] does not contain a method named 'ExecuteQuerySegmentedAsync'.HResult : -2146233087CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordExceptionFullyQualifiedErrorId : MethodNotFoundInvocationInfo :ScriptLineNumber : 56OffsetInLine : 4HistoryId : -1ScriptName : C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1Line : $Results = $Table.ExecuteQuerySegmentedAsync($TableQuery, $token)PositionMessage : At C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:56 char:4+ $Results = $Table.ExecuteQuerySegmentedAsync($TableQuery, …+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PSScriptRoot : C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0PSCommandPath : C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1CommandOrigin : InternalScriptStackTrace : at ExecuteQueryAsync, C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1: line 56at Get-AzTableRow, C:\home\data\ManagedDependencies\210906060508926.r\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1: line 683at <ScriptBlock>, C:\home\site\wwwroot\CosmosTrigger1\run.ps1: line 10TargetSite : System.Object CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object, System.Object, System.Object)StackTrace :at CallSite.Target(Closure , CallSite , Object , Object , Object )at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)at System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)Message : Method invocation failed because [System.String] does not contain a method named 'ExecuteQuerySegmentedAsync'.Data

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,477 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,494 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,427 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,461 Reputation points

    Hello MuralidharKumar-6346,

    Not an expert in Cosmo DB with Azure, but from Powershell perspective seems that some module function is missing, or the Table function is not aligned with Cloudtable to receive the output.

    I would recommend you to check the next article which explains in depth the AzTable operations:

    Hope this helps in your case,
    Best regards,

    0 comments No comments

  2. Limitless Technology 39,461 Reputation points

    Hello @Muralidhar Kumar

    Also, this is a basic question, after installing the "Az Module" did you updated ? Did you try to checked if its working ? If not those errors are common as its not able to read az modules. please follow the below commands from a fresh start.

    To install the Az module with global scope, run the Install-Module cmdlet as shown below in an elevated PowerShell window.

    Install-Module -Name Az -AllowClobber  

    If you don’t have administrator rights or want to install for user scope only, add the -Scope parameter as shown here:

    Install-Module -Name Az -AllowClobber -Scope CurrentUser  

    To update the module, run Update-Module. Older versions of the module are not uninstalled but if you have more than one version of Az, by default module autoload and Import-Module load the latest version.

    Update-Module -Name Az  

    To check which versions of the module you have installed, use Get-Module:

    Get-Module -Name *Az.* -ListAvailable  

    --please don't forget to upvote and Accept as answer if the reply is helpful--


    0 comments No comments