question

MuralidharKumar-6346 avatar image
0 Votes"
MuralidharKumar-6346 asked LimitlessTechnology-2700 answered

how to read cosmos db table entities from powershell function

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.
![129399-image.png][1]
[1]: /answers/storage/attachments/129399-image.png

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

windows-server-powershellazure-functionsazure-cosmos-db
image.png (25.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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:

https://docs.microsoft.com/bs-latn-ba/azure/storage/tables/table-storage-how-to-use-powershell#reference-cloudtable-property-of-a-specific-table

Hope this helps in your case,
Best regards,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @MuralidharKumar-6346

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--

Thanks,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.