Connecting to Azure SQL Database via Powershell

2

I was trying to get powershell connect to Azure SQL Database from local. I tried the following snippet

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

But I was getting this warning:

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)

I think, resolved the above with the solution by turning on RPC Locator service from component services link manually as shared by 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?"

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

But I get the following error and stuck. I am missing something silly I guess. . Exhausted, :(

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

Request some suggestions that could get me going. I feel missing missing something in my approach to get connected.

Thanks

H Bala

sql-server
powershell
azure
arm-template
asked on Stack Overflow Jun 2, 2016 by H Bala

1 Answer

0

The RPC Locator service start was definitely correct step.It got the warning and local lookup corrected.

But the reason that I got the "Invalid object error" was because import was completing yet not creating all the schema as defined.

Close Observations revealed the following:

1. Get-AzureSqlDatabaseImportExportStatus returns no status.

Get-AzureSqlDatabaseImportExportStatus -RequestId $ImportRequest.RequestGuid            
  -ServerName $deployParameters.Item('sqlserverName') 
  -Username $credential.UserName
  1. Attempted programmatically as below to know the progress, reveals it gets stuck at 5% and then suddenly flashes complete.

        $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. Attempted directly on PS prompt gets me the import as completed though on SSMS, i can see only 30% of tables created!!

      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. Import via portal for same backpac creates full database tables properly.

answered on Stack Overflow Jun 3, 2016 by H Bala

User contributions licensed under CC BY-SA 3.0