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

问题描述 投票:0回答:1

我试图从powershell连接到SQL服务器并收到此错误

使用“0”参数调用“打开”的异常:“目标主体名称不正确。无法生成SSPI上下文。”在C:\ Users \ Musawwir \ Downloads \远程访问db.ps1:10 char:1 + $ Connection.Open()+ ~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified :([],MethodInvocationException + FullyQualifiedErrorId:SqlException使用“1”参数调用“Fill”的异常:

我可以通过SSMS从其他机器连接到SQL SERVER但无法通过powershell连接,脚本也可以在本地机器上正常运行。

这是我正在使用的代码:

[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"
sql-server powershell
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.