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.