找不到类型 Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest

Cannot find type Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest

我正在使用 PowerShell 备份 Azure SQL 数据库。脚本的最后部分运行良好,是这样的:

    Write-Output "Exporting databases"
foreach ($db in $azSqlServerDatabases) {
    $exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $azSqlStageConnContext -StorageContainer $container -DatabaseName $db.Name -BlobName ($db.Name + ".bacpac") 
    $exportRequests.Add($exportRequest)
    Write-Output ($db.Name + ".bacpac") 
}

我正在尝试创建通用列表:

$exportRequests = New-Object 'System.Collections.Generic.List[Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest]'

其中必须包含请求的结果。问题是当我创建通用列表时出现错误:

    New-Object : Cannot find type [System.Collections.Generic.List[Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest]]: verify that the assembly containing this type is lo
aded.
At C:...
+ ... tRequests = New-Object 'System.Collections.Generic.List[Microsoft.Win ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

在哪里可以找到这种类型?为什么不包括它 - 我成功调用了 Start-AzureSqlDatabaseExport 并且我得到了结果对象 - 所以类型已经知道了。

尝试在 Azure PowerShell 控制台中手动创建一个类型的对象会引发相同的异常:

 PS C:\> $x = new-object 'Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest'
new-object : Cannot find type [Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest]: verify that t
he assembly containing this type is loaded.
At line:1 char:6
+ $x = new-object 'Microsoft.WindowsAzure.Commands.SqlDatabase.Services ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

PS C:\>

我从 MS documentation.

得到了输出类型

I 运行 getType()Start-AzureSQLDatabaseExport 返回的对象上。看起来你 运行 遇到了类型问题。 Start-AzureSQLDatabaseExport返回的对象的实际类型是Microsoft.WindowsAzure.Commands.SqlDatabase.Model.SqlDatabaseServerOperationContext

尝试像下面这样声明您的列表:

$exportRequests = New-Object 'System.Collections.Generic.List[Microsoft.WindowsAzure.Commands.SqlDatabase.Model.SqlDatabaseServerOperationContext]'

感谢@elfisher 的帮助。这是我的命令的最终版本,以防万一有人觉得它有用。它帮助我在开发和测试期间从 azure 服务器导出所有数据库。

# The command will prompt only once for most parameters during one session
param(
[string] $azSubscriptionId, 
[string] $azStorageName, 
[string] $azStorageKey, 
[string] $azContainerName,
[string] $azSqlServerName, 
[string] $azSqlServerUserName, 
[string] $azSqlServerPassword)

#if you want to clean up all globally created vars => Remove-Variable -Name 'az*'
 Remove-Variable -Name 'az*'

#Remove-Variable -Name 'azAccount' # clean up acc credentials. Can be removed but once in a while the credentials for the account get expired

#region azAccount
$azAccount = Get-AzureAccount
if(!$azAccount)
{
    Do
    {
       Write-Output "No azure account. Prompting ..."
       $azAccount = Add-AzureAccount
    } While (!$azAccount)
    Set-Variable -Name azAccount -Value $azAccount -Scope Global
    $azAccount = Get-AzureAccount
}
#endregion

#region azSubscriptionId
if(!$azSubscriptionId) 
{
    Do
    {
        Write-Output "No subscription Id. Prompting ..."
        $azSubscriptionId = Read-Host "Subscription Id"
    } While (!$azSubscriptionId)
    Write-Output "Input for subscription Id $azSubscriptionId"
    Set-Variable -Name azSubscriptionId -Value $azSubscriptionId -Scope Global
}
Select-AzureSubscription -SubscriptionId  $azSubscriptionId
Write-Output "Selected subscription Id $azSubscriptionId"
#endregion

#region azStorageName
if(!$azStorageName) 
{
    Do
    {
        Write-Output "No storage name. Prompting ..."
        $azStorageName = Read-Host "storage name"
    } While (!$azStorageName)
    Set-Variable -Name storageName -Value $azStorageName -Scope Global
}
#endregion

#region azStorageKey
if(!$azStorageKey) 
{
    Do
    {
        Write-Output "No storage key. Prompting ..."
        $azStorageKey = Read-Host "storage key"
    } While (!$azStorageKey)
    Set-Variable -Name azStorageKey -Value $azStorageKey -Scope Global
}
#endregion

#region azContainerName
if(!$azContainerName) 
{
    Do
    {
        Write-Output "No container name. Prompting ..."
        $azContainerName = Read-Host "container name"
    } While (!$azContainerName)
    Set-Variable -Name azContainerName -Value $azContainerName -Scope Global
}
#endregion

#region azSqlServerName
if(!$azSqlServerName) 
{
    Do
    {
        Write-Output "No sql server name. Prompting ..."
        $azSqlServerName = Read-Host "sql server name"
    } While (!$azSqlServerName)
    Set-Variable -Name azSqlServerName -Value $azSqlServerName -Scope Global
}
#endregion

#region azSqlServer
if(!$azSqlServer) 
{   
    Write-Output "No sql server variable stored on this PC. Prompting ..."
    $azSqlServer = Get-AzureSqlDatabaseServer -ServerName $azSqlServerName 
    Set-Variable -Name azSqlServer -Value $azSqlServer -Scope Global
}
#endregion

#region azSqlServerCredenials
if(!$azSqlServerCredenials) 
{
    Do
    {
        Write-Output "No sql server credentials. Prompting ..."
        $azSqlServerCredenials = Get-Credential "Enter Credentials for $azSqlServerName"
    } While (!$azSqlServerCredenials)
    Set-Variable -Name azSqlServerCredenials -Value $azSqlServerCredenials -Scope Global
}
#endregion

#region Sql connection context
if(!$azSqlCtx)
{
    Write-Output "No Sql Server Context. Creating..."
    $azSqlCtx = New-AzureSqlDatabaseServerContext -ServerName $azSqlServer.ServerName -Credential $azSqlServerCredenials
    Set-Variable -Name azSqlCtx -Value $azSqlCtx -Scope Global
     Write-Output "Sql Server Context for $azSqlServer.ServerName created."
}

#endregion

#region Storage connection context
if(!$azStorageCtx)
{
    Write-Output "No Storage Context. Creating..."
    $azStorageCtx = New-AzureStorageContext -StorageAccountName $azStorageName -StorageAccountKey $azStorageKey
    Set-Variable -Name azStorageCtx -Value $azStorageCtx -Scope Global
    Write-Output "Storage context for $azStorageName created."
}
#endregion

#region Storage container ref
if(!$azStorageContainer)
{
    Write-Output "No container ref. Creating for $azContainerName"
    $azStorageContainer = Get-AzureStorageContainer -Name $azContainerName -Context $azStorageCtx
    Set-Variable -Name azStorageContainer -Value $azStorageContainer -Scope Global
    Write-Output "Container ref to $azStorageContainer created."
}
#endregion

#region Sql Databases ref
if(!$azSqlServerDatabases)
{
    Write-Output "No Sql Databases array ref. Creating..."
    $azSqlServerDatabases = Get-AzureSqlDatabase -ConnectionContext $azSqlCtx
    Set-Variable -Name azSqlServerDatabases -Value $azSqlServerDatabases -Scope Global
    Write-Output "Sql Databases array ref created."
}
#endregion

#region actual export of azure databases
$exportRequests = New-Object 'System.Collections.Generic.List[Microsoft.WindowsAzure.Commands.SqlDatabase.Model.SqlDatabaseServerOperationContext]'

Write-Output "Exporting databases"
foreach ($db in $azSqlServerDatabases) {
    $exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $azSqlCtx -StorageContainer $azStorageContainer -DatabaseName $db.Name -BlobName ($db.Name + ".bacpac") 
    $exportRequests.Add($exportRequest)
    Write-Output ($db.Name + ".bacpac") 
}
#endregion

#import dbs back into your local server

#cd [FOLDERPATH]
#$goodlist = dir
# probably the correct path
##C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\SQLDB\DAC0
#cd 'C:\Program Files (x86)\Microsoft SQL Server0\DAC\bin'
#foreach($i in $goodlist){ $name = $i.Name; $namer = $i.Name.Substring(0, $i.Name.length - 7); .\SqlPackage.exe /a:Import /sf:[FOLDERPATH]$name /tdn:$namer /tsn:[SERVERNAME] }