0

I need to execute a query against an on-premise SQL Server 2016 instance from our Azure automation account for the purpose of sending emails based on the content in the database.

The client is unable to establish a session, it is a networking issue. I think I might be missing a middle step or have not given the SQL Server module enough information to target our on premises server.

  1. I am using the sqlserver module
  2. I have checked the TCP/IP settings are enabled for the on premises instance
  3. I turned on SQL Server Browser (i do not think this is required)
  4. I tested the code on my local Powershell ISE and it worked
  5. I tried using Windows Auth and SQL Server auth

Code:

Import-Module sqlserver

$from       = "x.y@dom.com"
$to         = "email@gmail.com"
$SMTPServer = "smtp.sendgrid.net"
$SMTPPort   = "587"
$username   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$password   = "xxxxxxxxxxx"

$subject    = "Email test from Azure SMTP server"
$body       = "Test email body, no content to see here!"

#$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
#$smtp.Send($From, $To, $subject, $body)



function Get-EmailBody{

}

<#
    Get all emails which have been merged into the emailChangeHistory table with the flag emailSent = 0.
#>
function Get-ChangeHistory($sqlServer, $database, $emailSent = 0){
    Invoke-Sqlcmd -Query "select 1" -ServerInstance "servername" 
}

Get-ChangeHistory -sqlServer $null, -database $null, -emailSent $null

I get this error:

Invoke-Sqlcmd : 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

At line:30 char:5
+ Invoke-Sqlcmd -Query "select 1" -ServerInstance "servername"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExceptionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GettingItDone
  • 523
  • 8
  • 26
  • Have you tested what "servername" resolves to from a runbook? Is there any network path between where the runbook execuates and the SQL Server? – Persistent13 Apr 01 '19 at 14:22
  • @Persistent13 I executed Resolve-DNSName -name "servername" which returned servername.domain.local. – GettingItDone Apr 01 '19 at 14:37

0 Answers0