无法使用安全密码通过PowerShell连接到SQL Server

发布于 2025-02-10 08:16:17 字数 1198 浏览 2 评论 0原文

我正在尝试使用以下内容通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄情伤 2025-02-17 08:16:17

$pwd = $cred.Password

$pwd = $cred.GetNetworkCredential().Password

但是,我建议不要将纯文本密码存储在此类内存 您的方法要求它作为纯文本中的参数传递,因此您需要更好的方法。

尝试使用此sqlserver模块 > Indoke-SQLCMD函数中的参数。

Change this

$pwd = $cred.Password

to this

$pwd = $cred.GetNetworkCredential().Password

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 the Invoke-Sqlcmd function.

花海 2025-02-17 08:16:17

我能够通过在连接字符串中添加 Integrated Security = true; 参数来传递安全字符串作为密码。

谢谢。

I was able to pass secure string as password by adding Integrated Security = True; parameter in connection string.

Thank you.

谈场末日恋爱 2025-02-17 08:16:17

该代码不是来自Bing的AI,但是AI给出了一个很好的描述:

为了创建一个安全的字符串,用于从PowerShell连接到SQL Server,您可以使用转换converts-securestring cmdlet。
这将创建一个安全的字符串对象,可用于将密码存储在PowerShell脚本中。然后,您可以使用此对象创建一个可以连接到SQL Server的PSCREDential对象。
然后,您可以使用此凭据对象使用SQLConnection类连接到SQL Server。
这将创建一个连接对象,该对象使用指定的凭据连接到SQL Server。然后,您可以使用此连接对象对数据库执行SQL命令。

$cred = Get-Credential

#Initialize connection string
$connString = "Data Source=$SQLServer; Initial Catalog=$SQLDBName"

#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
$conn.Credential = $cred

#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()
} 

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.

$cred = Get-Credential

#Initialize connection string
$connString = "Data Source=$SQLServer; Initial Catalog=$SQLDBName"

#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
$conn.Credential = $cred

#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()
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文