首页 > 解决方案 > 找不到“ExecuteQuery”的重载和参数计数:“1”

问题描述

strong text找不到“ExecuteQuery”和参数计数的重载:“1”。在 C:\Program Files\WindowsPowerShell\Modules\AzureRmStorageTable\1.0.0.23\AzureRmStorageTableCoreHelper.psm1:369 char:2 + $result = $table.CloudTable.ExecuteQuery($tableQuery) + ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (: ) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

找不到“执行”和参数计数的重载:“1”。在 C:\Program Files\WindowsPowerShell\Modules\AzureRmStorageTable\1.0.0.23\AzureRmStorageTableCoreHelper.psm1:191 char:11 + ... return ($table.CloudTable.Execute((invoke-expression "[Microsoft ... + ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

找不到“执行”和参数计数的重载:“1”。在 line:1 char:1 + [Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,Microsoft.Wi ... + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

这就是 AzureRmStorageTableCoreHelper 的样子 <# .SYNOPSIS AzureRmStorageTableCoreHelper.psm1 - PowerShell 模块,其中包含与操作 Azure 存储表行/实体相关的所有函数。.DESCRIPTION AzureRmStorageTableCoreHelper.psm1 - PowerShell 模块,包含与操作 Azure 存储表行/实体相关的所有函数。.NOTES 确保安装了最新的 Azure PowerShell 模块,因为我们依赖于 Microsoft.WindowsAzure.Storage.dll 和 Microsoft.WindowsAzure.Commands.Common.Storage.dll。

If running this module from Azure Automation, please make sure you check out this blog post for more information:
https://blogs.technet.microsoft.com/paulomarques/2017/01/17/working-with-azure-storage-tables-from-powershell/

>

需要-模块 Azure.Storage、AzureRm.Profile、AzureRm.Storage、AzureRM.Resources

模块功能

函数 GetLatestFullAssemblyName { 参数 ( [string]$dllName )

# getting list of all assemblies
$assemblies = [appdomain]::currentdomain.getassemblies() | Where-Object {$_.location -like "*$dllName"} 
if ($assemblies -eq $null)
{
    throw "Could not identify any assembly related to DLL named $dllName"
}

$sanitazedAssemblyList = @()
foreach ($assembly in $assemblies)
{
    [version]$version = $assembly.fullname.split(",")[1].split("=")[1]
    $sanitazedAssemblyList += New-Object -TypeName psobject -Property @{"version"=$version;"fullName"=$assembly.fullname}
}

return ($sanitazedAssemblyList | Sort-Object version -Descending)[0]

}

获取最新的 Microsoft.WindowsAzure.Storage.dll 完整程序集名称

$assemblySN = (GetLatestFullAssemblyName -dllName "Microsoft.WindowsAzure.Storage.dll").fullname

函数 Test-AzureStorageTableEmptyKeys { [CmdletBinding()] 参数 ( [string]$partitionKey, [String]$rowKey )

$cosmosDBEmptyKeysErrorMessage = "Cosmos DB table API does not accept empty partition or row keys when using CloudTable.Execute operation, because of this we are disabling this capability in this module and it will not proceed." 

if ([string]::IsNullOrEmpty($partitionKey) -or [string]::IsNullOrEmpty($rowKey))
{
    Throw $cosmosDBEmptyKeysErrorMessage
}

}

function Get-AzureStorageTableTable { <# .SYNOPSIS 获取一个 Table 对象,它可以来自 Azure 存储表或 Cosmos DB 预览支持。.DESCRIPTION 获取一个 Table 对象,它可以来自 Azure Storage Table 或预览支持的 Cosmos DB。.PARAMETER resourceGroup Azure 存储帐户或 Cosmos DB 所在的资源组 .PARAMETER tableName 要检索的表的名称 .PARAMETER storageAccountName 表所在的存储帐户名称 .EXAMPLE # 获取存储表对象 $resourceGroup = "myResourceGroup" $storageAccount = "myStorageAccountName" $tableName = "table01"

    [Parameter(Mandatory=$true)]
    [String]$tableName,

    [Parameter(ParameterSetName="AzureRmTableStorage",Mandatory=$true)]
    [Parameter(ParameterSetName="AzureTableStorage",Mandatory=$true)]
    [String]$storageAccountName
)

$nullTableErrorMessage = [string]::Empty

switch ($PSCmdlet.ParameterSetName)
{
    "AzureRmTableStorage"
        {
            $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName).Context    
            $nullTableErrorMessage = "Table $tableName could not be retrieved from Storage Account $storageAccountName on resource group $resourceGroupName"
        }
    "AzureTableStorage"
        {
            $saContext = (Get-AzureStorageAccount -StorageAccountName $storageAccountName).Context
            $nullTableErrorMessage = "Table $tableName could not be retrieved from Classic Storage Account $storageAccountName"
        }
}

[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable]$table = Get-AzureStorageTable -Name $tableName -Context $saContext -ErrorAction SilentlyContinue

# Creating a new table if one does not exist
if ($table -eq $null)
{
    [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable]$table = New-AzureStorageTable -Name $tableName -Context $saContext
}

# Checking if there a table got returned
if ($table -eq $null)
{
    throw $nullTableErrorMessage
}

# Returns the table object
return [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable]$table

}

function Add-StorageTableRow { <# .SYNOPSIS 将行/实体添加到指定表 .DESCRIPTION 将行/实体添加到指定表 .PARAMETER Table Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable 类型的表对象其中实体将被添加 .PARAMETER PartitionKey 标识表分区 .PARAMETER RowKey 标识分区中的一行 .PARAMETER 属性哈希表以及将成为实体一部分的列。例如@{"firstName"="Paulo";"lastName"="Marques"} .PARAMETER UpdateExisting 表示该命令应该更新现有行,如果partitionKey 和rowKey 找到的话。如果未找到,则添加新行。.

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [String]$partitionKey,

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [String]$rowKey,

    [Parameter(Mandatory=$false)]
    [hashtable]$property,
[Switch]$UpdateExisting
)

# Creates the table entity with mandatory partitionKey and rowKey arguments
$entity = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,$assemblySN" -ArgumentList $partitionKey, $rowKey

# Adding the additional columns to the table entity
foreach ($prop in $property.Keys)
{
    if ($prop -ne "TableTimestamp")
    {
        $entity.Properties.Add($prop, $property.Item($prop))
    }
}
    if($UpdateExisting)
{
    return ($table.CloudTable.Execute((invoke-expression "[Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::insertorreplace(`$entity)")))
}
else
{
    return ($table.CloudTable.Execute((invoke-expression "[Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::insert(`$entity)")))
}

}

function Get-PSObjectFromEntity { # Internal function # 将表的 ExecuteQuery 方法输出的实体转换为 PowerShell 对象数组

[CmdletBinding()]
param
(
    [Parameter(Mandatory=$true)]
    $entityList
)

$returnObjects = @()

if (-not [string]::IsNullOrEmpty($entityList))
{
    foreach ($entity in $entityList)
    {
        $entityNewObj = New-Object -TypeName psobject
        $entity.Properties.Keys | ForEach-Object {Add-Member -InputObject $entityNewObj -Name $_ -Value $entity.Properties[$_].PropertyAsObject -MemberType NoteProperty}

        # Adding table entity other attributes
        Add-Member -InputObject $entityNewObj -Name "PartitionKey" -Value $entity.PartitionKey -MemberType NoteProperty
        Add-Member -InputObject $entityNewObj -Name "RowKey" -Value $entity.RowKey -MemberType NoteProperty
        Add-Member -InputObject $entityNewObj -Name "TableTimestamp" -Value $entity.Timestamp -MemberType NoteProperty
        Add-Member -InputObject $entityNewObj -Name "Etag" -Value $entity.Etag -MemberType NoteProperty

        $returnObjects += $entityNewObj
    }
}

return $returnObjects

}

function Get-AzureStorageTableRowAll { <# .SYNOPSIS 返回存储表中的所有行/实体 - 无过滤 .DESCRIPTION 返回存储表中的所有行/实体 - 无过滤 .PARAMETER 表 Microsoft.WindowsAzure.Commands.Common 类型的表对象。 Storage.ResourceModel.AzureStorageTable 检索实体 .EXAMPLE # 获取所有行 $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context $table = Get-AzureStorageTable -Name $tableName -Context $saContext Get-AzureStorageTableRowAll -表 $table #> [CmdletBinding()] 参数 ( [Parameter(Mandatory=$true)] $table )

# No filtering

$tableQuery = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.TableQuery,$assemblySN"
$result = $table.CloudTable.ExecuteQuery($tableQuery)

if (-not [string]::IsNullOrEmpty($result))
{
    return (Get-PSObjectFromEntity -entityList $result)
}

}

function Get-AzureStorageTableRowByPartitionKey { <# .SYNOPSIS 基于分区键返回一个或多个行/实体 .DESCRIPTION 基于分区键返回一个或多个行/实体 .PARAMETER 表 Microsoft.WindowsAzure.Commands.Common.Storage 类型的表对象。 ResourceModel.AzureStorageTable 检索实体 .PARAMETER PartitionKey 标识表分区 .EXAMPLE # 通过分区键获取行 $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context $table = Get-AzureStorageTable -Name $tableName -上下文 $saContext Get-AzureStorageTableRowByPartitionKey -table $table -partitionKey $newPartitionKey #>[CmdletBinding()] 参数 ([Parameter(Mandatory=$true)] $table,

    [Parameter(Mandatory=$true)]
    [AllowEmptyString()]
    [string]$partitionKey
)

# Filtering by Partition Key


$tableQuery = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.TableQuery,$assemblySN"

[string]$filter = `
    [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterCondition("PartitionKey",`
    [Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::Equal,$partitionKey)

$tableQuery.FilterString = $filter

$result = $table.CloudTable.ExecuteQuery($tableQuery)

if (-not [string]::IsNullOrEmpty($result))
{
    return (Get-PSObjectFromEntity -entityList $result)
}

}

function Get-AzureStorageTableRowByColumnName { <# .SYNOPSIS 根据指定的列及其值返回一个或多个行/实体 .DESCRIPTION 根据指定的列及其值返回一个或多个行/实体 .PARAMETER Table Microsoft 类型的表对象。 WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable 检索实体 .PARAMETER ColumnName 列名,用于将值与 .PARAMETER 值进行比较 将在定义的列中查找的值 .PARAMETER 运算符 支持的比较运算符。有效值为 "Equal","GreaterThan","GreaterThanOrEqual","LessThan" ,"LessThanOrEqual" ,"NotEqual" 。

    [Parameter(Mandatory=$true)]
    [string]$columnName,

    [Parameter(ParameterSetName="byString",Mandatory=$true)]
    [AllowEmptyString()]
    [string]$value,

    [Parameter(ParameterSetName="byGuid",Mandatory=$true)]
    [guid]$guidValue,

    [Parameter(Mandatory=$true)]
    [validateSet("Equal","GreaterThan","GreaterThanOrEqual","LessThan" ,"LessThanOrEqual" ,"NotEqual")]
    [string]$operator
)

# Filtering by Partition Key

$tableQuery = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.TableQuery,$assemblySN"

if ($PSCmdlet.ParameterSetName -eq "byString") {
    [string]$filter = `
        [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterCondition($columnName,[Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::$operator,$value)
}

if ($PSCmdlet.ParameterSetName -eq "byGuid") {
    [string]$filter = `
        [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterConditionForGuid($columnName,[Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::$operator,$guidValue)
}

$tableQuery.FilterString = $filter

$result = $table.CloudTable.ExecuteQuery($tableQuery)


if (-not [string]::IsNullOrEmpty($result))
{
    return (Get-PSObjectFromEntity -entityList $result)
}

}

function Get-AzureStorageTableRowByCustomFilter { <# .SYNOPSIS 根据自定义筛选器返回一个或多个行/实体。.DESCRIPTION 根据自定义过滤器返回一个或多个行/实体。可以使用 Microsoft.WindowsAzure.Storage.Table.TableQuery 类或直接文本来构建此自定义筛选器。.PARAMETER Table Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable 类型的表对象检索实体 .PARAMETER customFilter 自定义过滤器字符串。.EXAMPLE # 使用类 Microsoft.WindowsAzure.Storage.Table.TableQuery $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount) 按名字获取行。

    [Parameter(Mandatory=$true)]
    [string]$customFilter
)

# Filtering by Partition Key
$tableQuery = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.TableQuery,$assemblySN"

$tableQuery.FilterString = $customFilter

$result = $table.CloudTable.ExecuteQuery($tableQuery)

if (-not [string]::IsNullOrEmpty($result))
{
    return (Get-PSObjectFromEntity -entityList $result)
}

}

function Update-AzureStorageTableRow { <# .SYNOPSIS 更新表实体 .DESCRIPTION 更新表实体。要使用此 cmdlet,您需要首先检索具有可用 Get-AzureStorageTableRow cmdlet 之一的实体并将其存储在对象中,更改必要的属性,然后执行更新,通过管道或作为参数将此修改后的实体传回。请注意,此 cmdlet 每次执行仅接受一个实体。此 cmdlet 无法更新 Partition Key 和/或 RowKey,因为它使用这两个值来定位要更新它的实体,如果需要此操作,请删除旧实体并添加具有更新值的新实体。.PARAMETER 表 Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel 类型的表对象。实体所在的 AzureStorageTable .PARAMETER Entity 具有新值的实体/行以执行更新。.EXAMPLE # 更新实体 $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context $table = Get-AzureStorageTable -Name $tableName -Context $saContext [string]$filter = [Microsoft.WindowsAzure.Storage .Table.TableQuery]::GenerateFilterCondition("firstName",[Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::Equal,"User1") $person = Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $filter $person.lastName = "新姓氏" $person | 更新 AzureStorageTableRow -table $table #>

    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    $entity
)

# Only one entity at a time can be updated
$updatedEntityList = @()
$updatedEntityList += $entity

if ($updatedEntityList.Count -gt 1)
{
    throw "Update operation can happen on only one entity at a time, not in a list/array of entities."
}

$updatedEntity = New-Object -TypeName "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,$assemblySN" -ArgumentList $entity.PartitionKey, $entity.RowKey

# Iterating over PS Object properties to add to the updated entity 
foreach ($prop in $entity.psobject.Properties)
{
    if (($prop.name -ne "PartitionKey") -and ($prop.name -ne "RowKey") -and ($prop.name -ne "Timestamp") -and ($prop.name -ne "Etag") -and ($prop.name -ne "TableTimestamp"))
    {
        $updatedEntity.Properties.Add($prop.name, $prop.Value)
    }
}

$updatedEntity.ETag = $entity.Etag

# Updating the dynamic table entity to the table
return ($table.CloudTable.Execute((invoke-expression "[Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::Replace(`$updatedEntity)")))

}

function Remove-AzureStorageTableRow { <# .SYNOPSIS Remove-AzureStorageTableRow - 删除指定的表行 .DESCRIPTION Remove-AzureStorageTableRow - 删除指定的表行。在传递从 Get-AzureStorageTableRow 可用 cmdlet 返回的实体时,它通过管道接受多次删除。它还可以直接使用分区和行键属性删除行/实体。.PARAMETER Table 类型为 Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable 的表对象,其中存在实体 .PARAMETER Entity (ParameterSetName=byEntityPSObjectObject) 具有新值的实体/行以执行删除。.PARAMETER PartitionKey (ParameterSetName=byPartitionandRowKeys) 实体所属的分区键。.
$filter2) $personToDelete = Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $finalFilter $personToDelete | Remove-AzureStorageTableRow -table $table .EXAMPLE # 直接使用 partitionkey 和 row key 删除条目 $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context $table = Get-AzureStorageTable -Name $tableName -Context $saContext Remove-AzureStorageTableRow -table $table -partitionKey "TableEntityDemoFullList" -rowKey "399b58af-4f26-48b4-9b40-e28a8b03e867" .EXAMPLE # 删除所有内容 $saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount)。上下文 $table = Get-AzureStorageTable -Name $tableName -Context $saContext Get-AzureStorageTableRowAll -table $table | 删除 AzureStorageTableRow -table $table #> [CmdletBinding()] 参数 ([Parameter(Mandatory=$true)] $table,

    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName="byEntityPSObjectObject")]
    $entity,

    [Parameter(Mandatory=$true,ParameterSetName="byPartitionandRowKeys")]
    [AllowEmptyString()]
    [string]$partitionKey,

    [Parameter(Mandatory=$true,ParameterSetName="byPartitionandRowKeys")]
    [AllowEmptyString()]
    [string]$rowKey
)

begin
{
    $updatedEntityList = @()
    $updatedEntityList += $entity

    if ($updatedEntityList.Count -gt 1)
    {
        throw "Delete operation cannot happen on an array of entities, altough you can pipe multiple items."
    }

    $results = @()
}

process
{
    if ($PSCmdlet.ParameterSetName -eq "byEntityPSObjectObject")
    {
        $partitionKey = $entity.PartitionKey
        $rowKey = $entity.RowKey
    }

    $entityToDelete = invoke-expression "[Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity,$assemblySN](`$table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::Retrieve(`$partitionKey,`$rowKey))).Result"

    if ($entityToDelete -ne $null)
    {
        $results += $table.CloudTable.Execute((invoke-expression "[Microsoft.WindowsAzure.Storage.Table.TableOperation,$assemblySN]::Delete(`$entityToDelete)"))
    }
}

end
{
    return ,$results
}

}

别名

新别名 -Name Add-AzureStorageTableRow -Value Add-StorageTableRow

标签: azurepowershellazure-table-storage

解决方案


据我了解,您想使用 PowerShell 来管理 Azure 表存储。如果是这样,您可以使用模块AzTable来实现它。

例如

Install-Module -Name AzTable

$groupName=""
$StorageAccountName = ""
$StorageAccountKey = ""
$vaule=" "
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$tables = Get-AzStorageTable -Context $context
Foreach($table in $tables){
    $table = Get-AzTableTable -storageAccountName $StorageAccountName -resourceGroup $groupName="" -TableName
    $entities=Get-AzTableRow -Table $table
    ForEach($e in $entities){
        $entity = New-Object Microsoft.Azure.Cosmos.Table.DynamicTableEntity($e.PartitionKey,$e.RowKey)
        $entity.Properties.Add("Name", $vaue)
        $table.Execute([Microsoft.Azure.Cosmos.Table.TableOperation]::InsertOrMerge($entity))
        Get-AzTableRow -Table $table -PartitionKey $e.PartitionKey -RowKey $e.RowKey
    }

}

有关详细信息,请参阅 https://docs.microsoft.com/en-us/azure/storage/tables/table-storage-how-to-use-powershell


推荐阅读