2016-01-11 3 views
2

Я поддерживаю базу данных Azure SQL с помощью PowerShell. Заключительная часть сценария, который работает нормально, это:Не удается найти тип Microsoft.WindowsAzure.Commands.SqlDatabase.Services.ImportExportRequest

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, и я получаю объект result, поэтому тип уже известен.

Попытка создать только один объект типа вручную в 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.

ответ

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]' 
0

Спасибо @elfisher за помощь. Вот окончательная версия моей команды на случай, если кто-нибудь найдет ее полезной. Он помогает мне экспортировать все dbs с сервера 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\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] } 
Смежные вопросы