I tried to query my syslogins with this script in powershell. It gives me this error:
"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.
The error shown:
provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server
Remote Access is enabled on the Database I try to connect to.
This is the code im working with:
$conn = New-Object System.Data.SqlClient.SqlConnection
$cmd = New-Object System.Data.SqlClient.SqlCommand
$conn.ConnectionString = "server=servername.hsr.ch;database=mydb;Integrated Security=true;"
$conn.open()
$cmd.Connection = $conn
$cmd.CommandText = "SELECT * FROM syslogins"
$out = $cmd.ExecuteNonQuery()
$conn.close()
print $out
Related
Hi I am trying to run a SQL query on one of my databases using an Azure Runbook. My setup is listed below. When I test this, I get the error The target principal name is incorrect. Cannot generate SSPI context." I have looked around and have not found anything related to an Azure setup. Does anyone have any ideas?
$myCred = Get-AutomationPSCredential -Name 'my-cred'
$userName = $myCred.UserName
$securePassword = $myCred.Password
$password = $myCred.GetNetworkCredential().Password
#Query to execute
# Invoke to Azure SQL DB
invoke-sqlcmd2 -ServerInstance $ServerName -Database $DatabaseName -Credential $SQLServerCred -Query $SqlQuery -Encrypt
As mentioned in the comment, looks you did not set $SQLServerCred before.
Try:
$myCred = Get-AutomationPSCredential -Name 'my-cred'
$userName = $myCred.UserName
$securePassword = $myCred.Password
$SQLServerCred = New-Object System.Management.Automation.PSCredential($userName , $securePassword)
#Query to execute
# Invoke to Azure SQL DB
invoke-sqlcmd2 -ServerInstance $ServerName -Database $DatabaseName -Credential $SQLServerCred -Query $SqlQuery -Encrypt
On my Skype For Business Server, when I run the following command, my Queue get's changed as it should:
Set-CsRgsQueue -Instance $queue
On my linked server however (one hop away from the Lync server), i get the following error:
network-related or instance-specific error occurred while
establishing a connection to SQL Server. Set-CsRgsQueue : 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I am however able to set up ODBC to the remote server (+ opened FW 1433 port, restarted the service ,....)
Where could my problem be?
EDIT:
My scriptblock looks like:
import-module Lync
$queue = Get-CsRgsQueue -Name $toChange
$action = New-CsRgsCallAction -Action TransferToPSTN -Uri $forwarduri -QueueID $queue.Identity
$queue.OverflowAction = $action
$queue.OverflowThreshold = 0
Set-CsRgsQueue -Instance $queue
If i'm not able to solve the issue, how could I implement invoke-command as this works on the server itself?
I tried by doing:
Invoke-Command -ComputerName SQLSERVER -ScriptBlock {Set-CsRgsQueue -Instance $queue}
but this throws:
Cannot validate argument on parameter 'Instance'. The argument is null. Provide a valid value for the argument, and then try running the command again.
I use the following command to connect to remote database:
$SQLServer = "Servername"
$SQLDBName = "DBname"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID= username; Password= password"
$SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "INSERT INTO dbo.Tablename(ReceivedTime, Subject)VALUES($time, $subject)"
$SqlCmd.Connection = $SqlConnection
But have the following err message:
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: Named Pipes Provider, error:
40 - Could not open a connection to SQL Server)" At line:10 char:1
$conn.Open()
~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : SqlException
I need to connect to the remote server and online solutions mainly focus on the local servers, so they are not suitable for me. (Company server/database and network, maybe different from local server?)
Hope you can help me improve the code or provide with some solutions. Thanks all.
I'm having an issue connecting to a SQL Server using SMO when using a ServerConnection. For the last few months this has been working, but now failing. Here is a snippet of the script:
$svrname = "Server"
#Establish Server connection
Write-ColorOutput "Establishing SQL Server Connection to $svrName" "White"
$mysvrConn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$mysvrConn.ServerInstance=$svrName
$mysvrConn.LoginSecure = $false
$mysvrConn.Login = "Admin"
$mysvrConn.Password = "Password"
$svr = new-object 'Microsoft.SqlServer.Management.SMO.Server' $mysvrConn
However, the following works.
$svrname = "Server"
$svr = new-object ('Microsoft.SQLServer.Management.SMO.Server') $svrname
And the assemblies:
Write-ColorOutput "Loading assemblies" "White"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.Smo") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
You almost certainly have permission issues with your SQL login. You are either using the wrong password, or the login does not have the proper creds.
Embarrassingly, when I had this issue, it turned out to be that I had entered the login password incorrectly.
I had expected a specific error stating the login failed. Instead it was the more generic: Failed to connect to server localhost., which threw me off.
Like you, I ran a test with the $LoginSecure = $True which worked, telling me that the connection to the server was fine. Only then did I spot my typo and was on my way to SMO bliss.
I need to create a format file from a local database table but I get a connection error when I try to run the command in cmd. Here is the command I am using:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T
I get the following errors:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-
specific error has occurred while establishing a connection to SQL Server.
Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
I have checked Services.msc and SQL Server is indeed "Started". I have also checked Sql Server Configuration Manager and the SQL Server state is "Running" and Start Mode is "Automatic".
The connection string to the local database is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Tools_TextileMagazine\CSharp_Template\Tools_TextileMagazine\OrderDatabase.mdf;Integrated Security=True
Any ideas to what could be wrong?
Add the -S parameter like specified here:
http://msdn.microsoft.com/it-it/library/ms162802.aspx
So your connection string will be:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T -S localhost\V11.0
I assume from your connection string that you named istance: V11.0