在 VSTS azure powershell 上 运行 Get-AzTableRow 命令时找不到类型 [Microsoft.Azure.Cosmos.Table.TableQuery] 错误显示

Cannot find type [Microsoft.Azure.Cosmos.Table.TableQuery] error display when running Get-AzTableRow command on VSTS azure powershell

我正在尝试 运行 使用 Get-AzTableRow 命令在 VSTS 上使用 azure powershell。

Powershell :

$ConfigurationPortalName = "xxxx"
$ResourceGroupName = "xxxx"
$Location = "West Europe"
$SubscriptionName = "xxxx"
$SubscriptionId = "xxxx"
$AdHocReleaseDefinitionId = "xxxx"
$MultipleEnvReleaseDefId = "xxxx"
$StorageConnectionString = "xxxx"
$StorageContainerName = "xxxx"
$PortalStorageAccountName = "xxxx"
$SkuName = "Standard_LRS"
$tableName = "Subscription"
$partitionKey = "Subscription"
$storageAccountName = $PortalStorageAccountName.ToLower()

write-host "*Install modules*"
Install-Module -Name Az.Accounts -RequiredVersion 1.5.2 -Scope CurrentUser -AllowClobber -Force
Install-Module -Name Az.Resources -RequiredVersion 1.3.1 -Scope CurrentUser -AllowClobber -Force
Install-Module -Name Az.Storage -RequiredVersion 1.3.0 -Scope CurrentUser -AllowClobber -Force
Install-Module -Name AzTable -RequiredVersion 2.0.2 -Scope CurrentUser -AllowClobber -Force 

write-host "*Current version*"
Get-Module Az.Storage
Get-Module Az.Accounts
Get-Module AzTable
Get-Module Az.Resources

write-host "*remove module*"
try
{
    Remove-Module -Name AzureRM.Profile -Force
    Remove-Module -Name Az.Accounts -Force
    Remove-Module -Name Az.Storage -Force
    Remove-Module -Name AzTable -Force
}
catch
{
    $_.Exception.Message
}

write-host "*Import module version*"
Import-Module -Name Az.Accounts -RequiredVersion 1.5.2 -Force
Import-Module -Name Az.Resources -RequiredVersion 1.3.1 -Force
Import-Module -Name Az.Storage -RequiredVersion 1.3.0 -Force
Import-Module -Name AzTable -RequiredVersion 2.0.2 -Force

write-host "*Final version*"
Get-Module Az.Accounts
Get-Module Az.Storage
Get-Module AzTable
Get-Module Az.Resources

$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $storageAccountName
Write-host "strgacc : " $storageAccount

if (!$storageAccount) 
{
    $storageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
        -Name $storageAccountName `
        -Location $Location `
        -SkuName $SkuName `
        -Kind Storage

    Write-host 'Storage Account Created !!!'
}
else 
{
    Write-host 'Storage Account Already Exist !!!'
}

$storageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $storageAccountName
$newStorageConnectionString = [string]::Format('DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};EndpointSuffix=core.windows.net', $storageAccountName, $storageAccountKeys[0].Value);

$webApp = Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $ConfigurationPortalName;
$appSettingList = $webApp.SiteConfig.AppSettings;

$hash = @{};
ForEach ($kvp in $appSettingList) 
{
    $hash[$kvp.Name] = $kvp.Value;
}

if ($hash['Config:Storage:ConnectionString'] -ne $newStorageConnectionString) 
{
    $hash['Config:Storage:ConnectionString'] = $newStorageConnectionString;

    Set-AzWebApp -ResourceGroupName $ResourceGroupName -Name $ConfigurationPortalName -AppSettings $hash;

    Write-host 'WebApp Configured With New Storage Account Key successfully !!!'
}
else 
{
    Write-host 'WebApp Already Configured With Storage Account Key !!!' 
}

$ctx = $storageAccount.Context

#Get Storage Tabel Reference
$cloudTable  = (Get-AzStorageTable -Name $tableName -Context $ctx -ErrorVariable ev -ErrorAction SilentlyContinue).CloudTable

#If no reference then create new table else check for default entry
if ($ev) 
{
    New-AzStorageTable -Name $tableName –Context $ctx;
    $cloudTable  = (Get-AzStorageTable -Name $tableName -Context $ctx -ErrorVariable ev -ErrorAction SilentlyContinue).CloudTable
    Write-host 'Table Created :' $cloudTable
}

Write-Host "Table :" $cloudTable

if ($cloudTable) 
{
    #$rows = Get-AzTableRow -table $cloudTable -partitionKey $partitionKey | ft
    $rows = "aaa"
    write-host "Rows :" $rows
    #if (!$rows)
    if ($rows) 
    {
        Add-AzTableRow `
            -table $cloudTable `
            -partitionKey $partitionKey `
            -rowKey $SubscriptionName `
            -property @{`
                            "SubscriptionId"          = $SubscriptionId; `
                            "AdHocReleaseDefinitionId" = $AdHocReleaseDefinitionId; `
                            "MultipleEnvReleaseDefId"  = $MultipleEnvReleaseDefId; `
                            "StorageConnectionString" = $StorageConnectionString; `
                            "StorageContainerName"    = $StorageContainerName
                        }

        Write-host 'Row successfully inserted !!!'
    }
    else 
    {
        Write-host 'Default rows already configured !!!'
    }
}
else 
{
    Write-host 'Table Storage Reference Not Found !!!'
}
Write-Host 'done'

错误:

2019-05-17T08:13:38.6380364Z ##[error]Cannot find type [Microsoft.Azure.Cosmos.Table.TableQuery]: verify that the assembly containing this type is loaded.

注意: 运行 在本地 powershell ISE 中很好。在 VSTS

中使用代理“托管 VS2017”

当我运行以下命令检查时,本地和VSTS中安装的模块版本相同:

Get-Module Az.Storage
Get-Module Az.Accounts
Get-Module AzTable
Get-Module Az.Resources

然后我得到的版本:

Az.Accounts 1.5.2
Az.Storage 1.3.0
AzTable 2.0.2
Az.Resources 1.3.1

现在新的错误显示在行 :

$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $storageAccountName

错误:

2019-05-22T10:38:30.2164835Z ##[error]Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.PowerShell.Clients.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.

对于您提供的问题:

2019-05-17T08:13:38.6380364Z ##[error]Cannot find type [Microsoft.Azure.Cosmos.Table.TableQuery]: verify that the assembly containing this type is loaded

原因是您需要使用模块Az.Storage 1.1.0 或更高版本。查看本文档中的 troubling。您可以使用 PowerShell 命令:

Update-Module -Name Az

然后打开一个新的 PowerShell 会话就可以了。

基于Manage organization billing in Azure DevOps - Sprint 150 Update #Support for Azure PowerShell Az module

You need to install the Az module on your agent machine if you are using private agents.

你应该使用私人代理,因为你不能在 hoseed 代理上更新 powershell 模块的版本(例如:Hosted VS2017)。

终于可以 运行 VSTS 上的脚本了:

注意:Azure Powershell 版本必须是 4.*(预览版)

$ConfigurationPortalName = "xxxx"
$ResourceGroupName = "xxxx"
$Location = "West Europe"
$SubscriptionName = "xxxx"
$SubscriptionId = "xxxx"
$AdHocReleaseDefinitionId = "xxxx"
$MultipleEnvReleaseDefId = "xxxx"
$StorageConnectionString = "xxxx"
$StorageContainerName = "xxxx"
$SkuName = "Standard_LRS"
$tableName = "Subscription"
$partitionKey = "Subscription"
$storageAccountName = "xxxx"

write-host "*Install Modules*"
Install-Module -Name Az.Accounts -RequiredVersion 1.5.2 -Scope CurrentUser -AllowClobber -Force
Install-Module -Name Az.Storage -RequiredVersion 1.3.0 -Scope CurrentUser -AllowClobber -Force
Install-Module -Name AzTable -RequiredVersion 2.0.2 -Scope CurrentUser -AllowClobber -Force 

write-host "*remove module*"
Remove-Module -Name Az.Accounts -ErrorAction SilentlyContinue -Force
Remove-Module -Name Az.Storage -ErrorAction SilentlyContinue -Force
Remove-Module -Name AzTable -ErrorAction SilentlyContinue -Force

write-host "*Import Module version*"
Import-Module -Name Az.Accounts -RequiredVersion 1.5.2 -Force
Import-Module -Name Az.Storage -RequiredVersion 1.3.0 -Force
Import-Module -Name AzTable -RequiredVersion 2.0.2 -Force

write-host "*Get Module version currently in use*"
Get-Module Az.Accounts
Get-Module Az.Storage
Get-Module AzTable

$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $storageAccountName

if (!$storageAccount) 
{
    $storageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
        -Name $storageAccountName `
        -Location $Location `
        -SkuName $SkuName `
        -Kind Storage

    Write-host 'Storage Account Created !!!'
}
else 
{
    Write-host 'Storage Account Already Exist !!!'
}

$storageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $storageAccountName
$newStorageConnectionString = [string]::Format('DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};EndpointSuffix=core.windows.net', $storageAccountName, $storageAccountKeys[0].Value);
$webApp = Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $ConfigurationPortalName;
$appSettingList = $webApp.SiteConfig.AppSettings;
$hash = @{};

ForEach ($kvp in $appSettingList) 
{
    $hash[$kvp.Name] = $kvp.Value;
}

if ($hash['Config:Storage:ConnectionString'] -ne $newStorageConnectionString) 
{
    $hash['Config:Storage:ConnectionString'] = $newStorageConnectionString;
    Set-AzWebApp -ResourceGroupName $ResourceGroupName -Name $ConfigurationPortalName -AppSettings $hash;
    Write-host 'WebApp Configured With New Storage Account Key successfully !!!'
}
else 
{
    Write-host 'WebApp Already Configured With Storage Account Key !!!' 
}

$ctx = $storageAccount.Context

#Get Storage Tabel Reference
$cloudTable  = (Get-AzStorageTable -Name $tableName -Context $ctx -ErrorVariable ev -ErrorAction SilentlyContinue).CloudTable

#If no reference then create new table else check for default entry
if($ev)
{
    New-AzStorageTable -Name $tableName –Context $ctx;
    $cloudTable  = (Get-AzStorageTable -Name $tableName -Context $ctx).CloudTable
    Write-host 'Table created successfully !!!'
}

Write-Host "Table name :" $cloudTable

if ($cloudTable) 
{
    $rows = Get-AzTableRow -table $cloudTable -partitionKey $partitionKey | ft
    if (!$rows)
    {
        Add-AzTableRow `
            -table $cloudTable `
            -partitionKey $partitionKey `
            -rowKey $SubscriptionName `
            -property @{`
                            "SubscriptionId"          = $SubscriptionId; `
                            "AdHocReleaseDefinitionId" = $AdHocReleaseDefinitionId; `
                            "MultipleEnvReleaseDefId"  = $MultipleEnvReleaseDefId; `
                            "StorageConnectionString" = $StorageConnectionString; `
                            "StorageContainerName"    = $StorageContainerName
                        }

        Write-host 'Row successfully inserted !!!'
    }
    else 
    {
        Write-host 'Default Row already configured !!!'
    }
}
else 
{
    Write-host 'Table Storage Reference Not Found !!!'
}

Write-Host 'Completed !!!'