简体   繁体   English

可以从其他计算机的 SSMS 连接到 SQL Server,但无法从 Powershell 连接到网络中的 SQL Server

[英]Can connect to SQL Server from SSMS from other computer but Can't connect to SQL Server in a network from powershell

i am trying to connect to a sql server from powershell and getting this error我正在尝试从 powershell 连接到 sql server 并收到此错误

Exception calling "Open" with "0" argument(s): "The target principal name is incorrect. Cannot generate SSPI context."使用“0”个参数调用“Open”时出现异常:“目标主体名称不正确。无法生成 SSPI 上下文。” At C:\\Users\\Musawwir\\Downloads\\remotely access db.ps1:10 char:1 + $Connection.Open() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : SqlException Exception calling "Fill" with "1" argument(s):在 C:\\Users\\Musawwir\\Downloads\\远程访问 db.ps1:10 char:1 + $Connection.Open() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified :( [], MethodInvocationException +fullyQualifiedErrorId : SqlException 异常调用“Fill”与“1”参数:

I can connect to SQL SERVER from other machines through SSMS but can not connect through powershell, also the script runs fine on local machine.我可以通过 SSMS 从其他机器连接到 SQL SERVER,但无法通过 powershell 连接,脚本在本地机器上也运行良好。

Here is the code i am using :这是我正在使用的代码:

[string] $Server= "tcp:DESKTOP-J9UQ90E,1433"
[string] $Database = "Eshop"


[string] $SQLQuery= "[dbo].[usp_getCustomerAge]"


$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';Connection Timeout=300;Integrated Security=TRUE;UID=ali;PWD=1234"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Command.CommandTimeout=500

$adp = New-Object System.Data.SqlClient.SqlDataAdapter $Command
$data = New-Object System.Data.DataSet 
$adp.Fill($data) | Out-Null


$data2 = ''
$emp = ''

foreach($Row in $data.Tables.Rows){
 [string]$C_name = $Row[0]

  $C_name
  $data2= $data2+ "|"+$C_name
  $C_name = $C_name+"|"
$C_name | out-file "d:\\test4.txt" -Append


}
$Connection.Close()

#$data2 | out-file "d:\\test4.txt"

用户 ali 无权访问存储过程.. 将用户从 ali 更改为 sa 并将集成安全性更改为 false 解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM