2

I am trying to connect to a SQL server running on an Azure VM using the below sample code (having defined the SQL user name and password):

$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset 

# Get the username and password from the SQL Credential 
$SqlUsername = $SqlCredential.UserName 
$SqlPass = $SqlCredential.GetNetworkCredential().Password 

# Define the connection to the SQL Database 
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;")

this code works when I am connecting to Azure SQL Database , but not when connecting to SQL running off Azure VM.

the error thrown is:

Error occured: Exception calling "Open" with "0" argument(s): 
"A network-related or instance-specific error occurred while establishing a 
connection to SQL Server. The server was not found or was not accessible.
 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 
(provider: TCP Provider, error: 0 - No such host is known.)"

I have verified on the VM SQL remote connections are enabled and including TCP port 1433 defined.

nebula_007
  • 171
  • 1
  • 2
  • 13

1 Answers1

0

Probably it's a network related issue. If you have verified on the VM SQL remote connections are enabled and including TCP port 1433 defined. Also, the port 1433 was opened in the inbound port rule of NSG attached to the VM and windows firewall inside the VM. You could use telnet verify this port locally.

Moreover, you could specific $SqlServer= "sqlvmlabel.eastus.cloudapp.azure.com"which is a public DNS name of Azure VM. Here is an example of a connection string for using SQL Authentication.

Server=sqlvmlabel.eastus.cloudapp.azure.com;Integrated Security=false;User ID=<login_name>;Password=<your_password>

Read more information: connect to a SQL Server Virtual Machine on Azure

Nancy
  • 26,865
  • 3
  • 18
  • 34