通过 Powershell 连接到 Azure SQL 数据库

Connecting to Azure SQL Database via Powershell

我试图让 powershell 从本地连接到 Azure SQL 数据库。我尝试了以下代码片段

$params = @{
  'Database' = 'dbsitenamehere'
  'ServerInstance' =  'instancename.database.windows.net'
  'Username' = 'abcites'
  'Password' = 'atbasIcpr0d'
  'OutputSqlErrors' = $true
  'Query' = 'SELECT * FROM Users'
 }
 Invoke-Sqlcmd  @params

但是我收到了这个警告:

WARNING: Could not obtain SQL Server Service information. An attempt to connect to WMI on 'Microsoft.WindowsAzure.Commands.SqlDatabase.Types.ps1xml' failed with the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

我认为,通过 Hodentek @ http://hodentekmsss.blogspot.in/2014/11/how-to-overcome-error-while-importing.html >> "How to overcome error while importing the SQLPS module into Powershell?" [=16= 共享的组件服务 link 手动打开 RPC 定位器服务,解决了上述问题]

PS SQLSERVER:\> Import-Module “sqlps” -DisableNameChecking
PS SQLSERVER:\> cd SQL
PS SQLSERVER:\SQL> dir
  MachineName                     
  -----------                     
  PC181578                        

PS SQLSERVER:\SQL> cd LocalHost
PS SQLSERVER:\SQL\LocalHost> dir

但是我收到以下错误并卡住了。我想我想念一些愚蠢的东西。 .筋疲力尽,:(

Invoke-Sqlcmd : Invalid object name 'Users'.
At line:17 char:3
+   Invoke-Sqlcmd  @params
+   ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

请求一些可以让我继续前进的建议。我觉得在我建立联系的方法中遗漏了一些东西。

谢谢

H巴拉

RPC 定位器服务启动绝对正确step.It 更正了警告和本地查找。

但我得到 "Invalid object error" 的原因是因为导入已完成但尚未创建定义的所有模式。

关闭观察揭示了以下内容:

1. Get-AzureSqlDatabaseImportExportStatus returns 无状态。

Get-AzureSqlDatabaseImportExportStatus -RequestId $ImportRequest.RequestGuid            
  -ServerName $deployParameters.Item('sqlserverName') 
  -Username $credential.UserName
  1. 尝试以编程方式了解 进度,显示它卡在 5% 然后突然闪烁完成.

        $ImportRequest = Start-AzureSqlDatabaseImport -SqlConnectionContext $SqlCtx -StorageContainer $Container -DatabaseName $deployParameters.Item('databaseName') -BlobName $BlobName
    
                        # Check the status of the import
                    Do
                    {
                        $importStatus = Get-AzureSqlDatabaseImportExportStatus -Request $importRequest
    
                        # Check if import failed
                        if($importStatus.Status -eq "Failed")
                        {
                            Write-Output -Message $importStatus.ErrorMessage
                            $importDone = 1;
                        }
    
                        # Check if import is completed
                        if($importStatus.Status -eq "Completed")
                        {
                            Write-Output "$(Get-Date -f $timeStampFormat) - Restore database complete"
                            $importDone = 1;
                        }
    
                        # If not failed or completed, return the status of the current import
                        if(($importStatus.Status -ne "Completed" -or $importStatus.Status -ne "Failed") -and $importDone -ne 1)
                        {
                            Write-Output "$(Get-Date -f $timeStampFormat) - Import status: $($importStatus.Status)"
                        }
    
                        # Added a sleep so that the Write-Output doesn't get flooded
                        Start-Sleep -s 3
                    }While($importDone -ne 1)
    
  2. 直接尝试 在 PS 提示上让我完成导入 虽然在 SSMS 上,我可以看到 只有 30%创建的表数!!

      PS SQLSERVER:\> Get-AzureSqlDatabaseImportExportStatus -RequestId  
      $ImportRequest.RequestGuid -ServerName   
      $deployParameters.Item('sqlserverName') -Username $credential.UserName
      cmdlet Get-AzureSqlDatabaseImportExportStatus at command pipeline   position 1
      Supply values for the following parameters:
      (Type !? for Help.)
      Password: ABCEfg1pr0
    

BlobUri : https://ABC.blob.core.windows.net/kind-snapshot/kind-snapshot.bacpac

DatabaseName : Pilotdbtrialrun

ErrorMessage :

LastModifiedTime : 6/3/2016 2:29:29 PM

QueuedTime : 6/3/2016 2:27:42 PM

RequestId : 0f76af7b-7452-4cd4-a7a8-d6XXX4dc5923

RequestType : Import

ServerName : pilotsrvrtrialrun.database.windows.net

Status : Completed

ExtensionData :

4.通过门户导入相同的 backpac 会正确创建完整的数据库表。