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

时间:2016-01-11 14:53:29

标签: azure azure-sql-database azure-powershell

我使用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.

获得了输出类型

2 个答案:

答案 0 :(得分:1)

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

尝试按以下方式声明您的列表:

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

答案 1 :(得分:0)

谢谢@elfisher的帮助。这是我的命令的最终版本,以防有人发现它有用。它可以帮助我在开发和测试期间从azure服务器导出所有dbs。

# 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\DAC\120
#cd 'C:\Program Files (x86)\Microsoft SQL Server\110\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] }