无法使用安全密码通过PowerShell连接到SQL Server
我正在尝试使用以下内容通过PowerShell连接到SQL Server(我是新手)。当我使用安全密码(从文件中存储的get-credential或密码)时,我总是会出现错误“登录失败”。但是,如果我将密码作为明文而不是安全字符串传递,则可以成功连接。可以建议任何人提出一种传递安全密码的方法,理想地存储在外部文件中。
我运行的代码和错误如下:
$cred = Get-Credential
$pwd = $cred.Password
$uid = $cred.UserName
$SQLServer = "."
$SQLDBName = "TestDB"
#Initialize connection string
$connString = "Data Source=$SQLServer;Database=$SQLDBName;User ID=$uid;Password=$pwd"
#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
#Attempt to open the connection
$conn.Open()
if($conn.State -eq "Open")
{
# We have a successful connection here
# Notify of successful connection
Write-Host "Test connection successful"
$conn.Close()
}
Exception calling "Open" with "0" argument(s): "Login failed for user 'TestUser'."
At line:18 char:1
+ $conn.Open()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
SQL Server错误的更多详细信息:
Login failed for user 'TestUser'. Reason: Password did not match that for the login provided. [CLIENT: <local machine>]
I'm trying to connect to SQL server via PowerShell using the below (I'm new to this). I always get the error "login failed" when I use secure password (from Get-Credential or password stored in file). But if I pass the password as plaintext instead of secure string, it connects successfully. Could any one please suggest a method to pass secure password, ideally stored in an external file.
The code I ran and the error is below:
$cred = Get-Credential
$pwd = $cred.Password
$uid = $cred.UserName
$SQLServer = "."
$SQLDBName = "TestDB"
#Initialize connection string
$connString = "Data Source=$SQLServer;Database=$SQLDBName;User ID=$uid;Password=$pwd"
#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
#Attempt to open the connection
$conn.Open()
if($conn.State -eq "Open")
{
# We have a successful connection here
# Notify of successful connection
Write-Host "Test connection successful"
$conn.Close()
}
Exception calling "Open" with "0" argument(s): "Login failed for user 'TestUser'."
At line:18 char:1
+ $conn.Open()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Further details of error from SQL server:
Login failed for user 'TestUser'. Reason: Password did not match that for the login provided. [CLIENT: <local machine>]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
。
中
但是,我建议不要将纯文本密码存储在此类内存 您的方法要求它作为纯文本中的参数传递,因此您需要更好的方法。
尝试使用此sqlserver模块 >
Indoke-SQLCMD
函数中的参数。Change this
to this
However, I would advise against storing a plain text password in memory like this. Your method requires it to--at best--be passed as a parameter in plain text, so you need a better method.
Try using this sqlserver module which supports the
-Credential
parameter in theInvoke-Sqlcmd
function.我能够通过在连接字符串中添加 Integrated Security = true; 参数来传递安全字符串作为密码。
谢谢。
I was able to pass secure string as password by adding Integrated Security = True; parameter in connection string.
Thank you.
该代码不是来自Bing的AI,但是AI给出了一个很好的描述:
为了创建一个安全的字符串,用于从PowerShell连接到SQL Server,您可以使用转换converts-securestring cmdlet。
这将创建一个安全的字符串对象,可用于将密码存储在PowerShell脚本中。然后,您可以使用此对象创建一个可以连接到SQL Server的PSCREDential对象。
然后,您可以使用此凭据对象使用SQLConnection类连接到SQL Server。
这将创建一个连接对象,该对象使用指定的凭据连接到SQL Server。然后,您可以使用此连接对象对数据库执行SQL命令。
The code did not come from Bing's AI, but the AI gave a nice description:
To create a secure string for connection to SQL Server from PowerShell, you can use the ConvertTo-SecureString cmdlet.
This will create a secure string object that can be used to store the password in a PowerShell script. You can then use this object to create a PSCredential object, which can be used to connect to SQL Server.
You can then use this credential object to connect to SQL Server using the SqlConnection class.
This will create a connection object that uses the specified credentials to connect to SQL Server. You can then use this connection object to execute SQL commands against the database.