当前位置: 首页 > 知识库问答 >
问题:

找不到“ExecuteQuery”的重载和参数计数:“%1”

郏稳
2023-03-14
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/

函数GetLatestFullAssemblyName{param([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]

}

$assemblysn=(GetLatestFullAssemblyName-DllName“Microsoft.WindowsAzure.Storage.DLL”).FullName

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

函数Get-AzureStorageTableTable{<#.synopsis获取一个表对象,它可以来自Azure Storage Table或预览支持中的Cosmos DB。.description获取一个表对象,它可以来自Azure存储表或预览支持中的Cosmos DB。.参数resourceGroup Azure存储帐户或Cosmos DB所在的资源组。参数tableName要检索的表的名称。参数storageAccountName表所在的存储帐户名称。示例#Geting Storage table object$resourceGroup=“MyResourceGroup”$storageAccountName=“MyStorageAccountName”$tableName=“Table01”$table=Get-AzureStorageTabletable-resourceGroup$resourceGroup-tableName$tableName-storageAccountName

    [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

函数Add-StorageTableRow{<#.Synopsis将行/实体添加到指定的表。Description将行/实体添加到指定的表。Parameter table将在其中添加实体的类型为Microsoft.WindowsAzure.Common.Storage.ResourceModel.AzureStorageTable的表对象。Parameter PartitionKey标识表分区。Parameter RowKey标识分区中的行。Parameter属性Hashtable包含将成为实体一部分的列。例如@{“firstname”=“paulo”;“lastname”=“marques”}。参数UpdateExisting表示如果partitionKey和rowkey发现现有行,则命令应该更新现有行。如果找不到,则添加新行。.示例#添加行$Sacontext=(Get-AzureRmStorageAccount-ResourceGroupName$ResourceGroup-Name$StorageAccount).Context$Table=Get-AzureStorageTable-Name$TableName-Context$Sacontext Add-StorageTableRow-Table$Table-PartitionKey$PartitionKey-RowKey([guid]::newGUID().ToString())-Property@{“firstName”=“paulo”;“lastName”=

    [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)")))
}

}

函数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
# 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)
}
    [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)
}

}

函数Get-AzureStorageTableRowByColumnName{<#。Synopsis基于指定的列及其值返回一个或多个行/实体。Description基于指定的列及其值返回一个或多个行/实体。Parameter Table类型为Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageTable的表对象以检索实体。Parameter ColumnName用于比较值的列名。Parameter value将在定义的列中查找的值。Parameter运算符支持的比较运算符。有效值为“Equal”、“GreaterThan”、“GreaterThanoRequal”、“LessThan”、“LessThanoRequal”、“NoteQual”。示例#按firstname获取行$Sacontext=(Get-AzureStorageAccount-ResourceGroupName$ResourceGroup-Name$StorageAccount)。Context$Table=Get-AzureStorageTable-Name$TableName-Context$Sacontext Get-AzureStorageTable-Name$TableName-Table

    [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)
}

}

    [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)
}
    [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)")))
    [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
}

}

new-alias-name Add-AzureStorageTableRow-value Add-StorageTableRow

共有1个答案

常心水
2023-03-14

根据我的理解,您希望使用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

 类似资料:
  • 问题内容: 我已经转换一个到一个通过使用。然后,我尝试将其乘以0.01,但是出现一个错误,提示这是我的代码: 通过阅读其他文章,似乎答案与类型有关。例如,如果将类型设置为Integer,则它将收到类似的错误。我尝试将类型更改为Int,但这似乎无法解决问题。 我也曾尝试设置“海峡”和“pennyCount”作为类型和和的所有组合,和。我的猜测是问题与函数将a转换为an有关。 有人可以帮忙弄清楚问题可

  • 问题内容: 我得到错误: 我也尝试过 和 但 工作良好。正确的语法是什么? 问题答案: 有一个功能:

  • 问题内容: 我收到一个错误 找不到接受提供的参数的’/’的重载 我试图通过做修复: 但随后将getAverage设置为而不是 问题答案: 在Swift中没有这样的隐式转换,因此您必须自己明确地进行转换: 来自 Swift编程语言 :“永远不会将值隐式转换为其他类型。” (部分:快速浏览) 但是您现在使用的是Swift,而不是Objective-C,因此请尝试以更加面向功能的方式进行思考。您的函数可

  • 问题内容: 渲染时捕获到异常: 找不到带有参数’()’和关键字参数’{}’的’products.views.’filter_by_led’。 我能够从shell成功导入,并且可以正常工作,因此路径应该正确。 这是urls.py: 这是生成错误的地方: 我不明白,因为这可以在同一个文件中正常工作: 这是函数定义: 我不明白为什么Django会认为该函数无法为该函数找到Reverse。 我删除了所有文

  • 我用了这个代码 我得到的错误是“找不到接受所提供参数的”init“的重载”

  • 问题内容: 我有这样的网址格式: 它在浏览器中可以正常工作,但是当我在外壳中执行此操作时,可以进行测试: 我感到恐惧: 我在这里想念什么? 问题答案: 你必须指定