将行插入 azure table 时出现错误

Getting the error while inserting rows into azure table

我正在尝试使用 PowerShell 上传 azure table 行,但我收到以下错误。这可能是由于错误的 Azure 存储 powershell 模块造成的吗?我正在使用 Azure.Storage 4.0.2 模块。

代码如下:

# Getting all the resource group
$resource_group_list = Get-AzureRmResourceGroup

# Iterating through the resource group
foreach($resource_group_list_iterator in $resource_group_list){

    # Since the solution applies for virtual machines,
    # obtain the list of virtual machines for the resource group
    $virtual_machine_list = get-azurermvm -ResourceGroupName $resource_group_list_iterator.ResourceGroupName

    # Proceed only when resource group contains virtual machines
    if(!($virtual_machine_list -eq $null)){

        # Iterate through the virtual machine list
        foreach($virtual_machine_list_iterator in $virtual_machine_list){

            # Creat an unique ID by concatinating 'Resource Group name' and 'Virtual Machine name'
            $unique_id = $resource_group_list_iterator.ResourceGroupName + $virtual_machine_list_iterator.name
            #Write-Host $unique_id
            $tag_list = $virtual_machine_list_iterator.Tags

            $tag_list.GetEnumerator() | foreach {
            #write-host $_.key
            #Write-Host $_.value
            #write-host "" 

            $partitionKey1 = $unique_id

            if($_.key -eq 'owner' -and $_.value -eq 'ibm') {
                #write-host "true"
                $virtual_machine_name = $virtual_machine_list_iterator.Name.ToString()
                $virtual_machine_resource_group_name = $resource_group_list_iterator.ResourceGroupName.ToString()
                $virtual_machine_location = $virtual_machine_list_iterator.Location.ToString()
                $virtual_machine_size = $virtual_machine_list_iterator.HardwareProfile.VmSize.ToString()
                $virtual_machine_operating_system = $virtual_machine_list_iterator.StorageProfile.ImageReference.Offer.ToString()



                $hash = @{}
                #$hash.add('currentDate', $current_date)
                $hash.Add('VM Name','VM')
                $hash.Add('Resource Group',$virtual_machine_resource_group_name)
                $hash.add('Location',$virtual_machine_location)
                $hash.add('VM Size',$virtual_machine_size)
                $hash.add('Operating System',$virtual_machine_operating_system)

                Add-StorageTableRow -table $azure_table_object -partitionKey ("CA") -rowKey $unique_id -property $hash

            }
           }

        }

    }

}

以下是我收到的例外情况:

Exception calling "Execute" with "1" argument(s): "The remote server returned an error: (400) 
Bad Request."
At C:\Program Files\WindowsPowerShell\Modules\AzureRmStorageTable.0.0.21\AzureRmStorageTableCor
eHelper.psm1:267 char:16
+ ...      return ($table.CloudTable.Execute((invoke-expression "[Microsoft ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : StorageException

我浏览了一些关于如何进行故障排除的在线资源,但没有找到任何解决方案。

我使用 fiddler 来捕获我这边的添加实体请求和你的代码。然后我可以得到详细信息 属性 name is invalid.

所以请尝试使用以下代码更改 属性 名称。

$hash.Add('VM_Name','VM')  #VM Name is invalid
$hash.Add('Resource_Group',$virtual_machine_resource_group_name) #Resource Group is invalid
$hash.add('Location',$virtual_machine_location)
$hash.add('VM_Size',$virtual_machine_size) #VM size is invalid
$hash.add('Operating_System',"windows")     
Add-StorageTableRow -table $table -partitionKey ("CA") -rowKey $unique_id -property $hash

更多关于azuretable属性名字的信息请参考这个document.

poperty names are case-sensitive strings up to 255 characters in size. Property names should follow naming rules for C# identifiers.

测试结果: