PUpskill
PUpskill

Reputation: 51

Add-AzTableRow throws error MethodInvocationException : The specified resource does not exist

We are encountering an error when attempting to add a new row to an Azure Storage Table using the -UseConnectedAccount context rather than a key based authentication in PowerShell.

Error Message is: MethodInvocationException: Exception calling "Execute" with "1" argument(s): "The specified resource does not exist."

Script code:

$StorageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -UseConnectedAccount
$table = Get-AzStorageTable -Name $tableName -Context $storageContext
Add-AzTableRow -Table $table.CloudTable -PartitionKey "PartitionKey1" -RowKey "RowKey1" -Property @{"Property1"="Value1"}

We have provided the Storage Table contributor role. Additionally, we have attempted to use the .NET SDK as shown below. However, we are encountering an error:

Exception calling "Execute" with "1" argument(s):
"The specified resource does not exist."
Microsoft.Azure.Cosmos.Table.StorageException: The specified resource does not exist."

Script Code:

$StorageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -UseConnectedAccount
$table = Get-AzStorageTable -Name $tableName -Context $storageContext
$entity = New-Object -TypeName Microsoft.Azure.Cosmos.Table.DynamicTableEntity -ArgumentList "PartitionKey1", "RowKey1"
$entity.Properties.Add("Property1", "Value1")
$table.CloudTable.Execute([Microsoft.Azure.Cosmos.Table.TableOperation]::InsertOrReplace($entity))

Upvotes: 0

Views: 259

Answers (2)

wenbo
wenbo

Reputation: 1506

$method1 = $false

if($method1){
    $storageAccountName = "wbxxxsa"
    $tableName = "testtable"
    
    $storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount
    $table = Get-AzStorageTable -Name $tableName -Context $storageContext
    $cloudTable = $table.CloudTable

    Write-Host "method1 cloudTable type is $($cloudTable.GetType())"
    $cloudTable | ConvertTo-Json -Depth 5

    Add-AzTableRow -Table $cloudTable -PartitionKey "PartitionKey1" -RowKey "RowKey1" -Property @{"Property1"="Value1"}
    
}else{
    ## method2
    $resourceGroup = "wb-xxx-rg"
    $storageAccountName = "wbxxxsa"
    $tableName = "testtable"

    $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
    $ctx = $storageAccount.Context
    $cloudTable = (Get-AzStorageTable -Name $tableName -Context $ctx).CloudTable

    Write-Host "method2 cloudTable type is $($cloudTable.GetType())"
    $cloudTable | ConvertTo-Json -Depth 5


    Add-AzTableRow -Table $cloudTable -PartitionKey "PartitionKey1" -RowKey "RowKey1" -Property @{"Property1"="Value1"}

}

The issue caused by the CloudTable you get, do not use -UseConnectedAccount if the cloudTable is used in Add-AzTableRow, when using -UseConnectedAccount the cloudTable not contain the key and keyName

cloudTable print-out: sas token, and key is empty but the method2 cloudTable carry the key.

{
    "ServiceClient": {      
      "Credentials": {      
        "SASToken": null,   
        "AccountName": null,
        "Key": null,
        "KeyName": null,
        "IsSharedKey": false,
        "IsAnonymous": true,
        "IsSAS": false,
        "SASSignature": null
      },
      "TableClientConfiguration": {
        "CosmosExecutorConfiguration": {
          "UseConnectionModeDirect": true,
          "UserAgentSuffix": null,
          "CurrentRegion": null,
          "MaxConnectionLimit": 50,
          "MaxRetryAttemptsOnThrottledRequests": null,
          "MaxRetryWaitTimeOnThrottledRequests": null,
          "ConsistencyLevel": null
        },

My explanation: (just personal understanding)

when using Add-AzTableRow, actually invoke below .net class, reference

    if ($UpdateExisting)
    {
        return ($Table.Execute([Microsoft.Azure.Cosmos.Table.TableOperation]::InsertOrReplace($entity)))
    }
    else
    {
        return ($Table.Execute([Microsoft.Azure.Cosmos.Table.TableOperation]::Insert($entity)))
    }

And the TabelClient class relies on the storage account sas token or storage account key or connection string. which can be find here

enter image description here

Upvotes: 0

Venkatesan
Venkatesan

Reputation: 10455

We are encountering an error when attempting to add a new row to an Azure Storage Table using the -UseConnectedAccount context rather than key-based authentication in PowerShell.

You can use the following PowerShell script to add a row without key-based authentication.

Script:

Connect-AzAccount -Subscription "<Your-subscription-id>"

$storageAccountName="venkat123"
$resourceGroup="<resource-group-name>"
$storageAccount=Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
$tableName = "table1"
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $ctx).CloudTable

$partitionKey = "PartitionKey1"
$rowkey="RowKey1"
Add-AzTableRow -table $cloudTable -partitionKey $partitionKey -rowKey $rowkey -property @{"Property1"="Value1"}

Output:

Result         : Microsoft.Azure.Cosmos.Table.DynamicTableEntity
HttpStatusCode : 204
Etag           : W/"datetime'2024-05-27T07%3A39%3A48.6896728Z'"
SessionToken   : 
RequestCharge  : 
ActivityId     : 

enter image description here

Portal: enter image description here

Reference:

Add-AzTableRow command is not available in Azure Cloud Shell - Stack Overflow by Jim Xu.

Upvotes: 0

Related Questions