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))
2
Answers
You can use the following PowerShell script to add a row without key-based authentication.
Script:
Output:
Portal:
Reference:
Add-AzTableRow command is not available in Azure Cloud Shell – Stack Overflow by Jim Xu.
The issue caused by the
CloudTable
you get, do not use-UseConnectedAccount
if thecloudTable
is used inAdd-AzTableRow
, when using-UseConnectedAccount
thecloudTable
not contain thekey
andkeyName
cloudTable
print-out: sas token, and key is empty but the method2 cloudTable carry the key.My explanation: (just personal understanding)
when using Add-AzTableRow, actually invoke below .net class, reference
And the
TabelClient
class relies on thestorage account sas token
orstorage account key
orconnection string
. which can be find here