将用户名和密码传递给 get-credential 或运行 sql 查询,而无需在 Powershell 中使用 invoke-sqlcmd

发布于 2024-08-25 16:35:58 字数 1646 浏览 14 评论 0原文

我正在尝试连接到远程 SQL 数据库并在 Powershell 中运行“select @@servername”查询。我试图在不使用集成安全性的情况下做到这一点。我一直在努力解决“get-credential”和“invoke-sqlcmd”,结果发现(我认为),您无法将密码从“get-credential”传递到另一个Powershell cmdlet。

这是我正在使用的代码:

add-pssnapin sqlserverprovidersnapin100
add-pssnapin sqlservercmdletsnapin100

# load assemblies
[Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.SqlEnum, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.SmoEnum, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.ConnectionInfo, `
      Version=9.0.242.0, Culture=neutral, `PublicKeyToken=89845dcd8080cc91")

# connect to SQL Server

$serverName = "HLSQLSRV03"
$server = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server -argumentList $serverName

# login using SQL authentication

$server.ConnectionContext.LoginSecure=$false;
$credential = Get-Credential
$userName = $credential.UserName -replace("\\","")
$pass = $credential.Password
$server.ConnectionContext.set_Login($userName)
$server.ConnectionContext.set_SecurePassword($credential.Password)
$DB = "Master"

invoke-sqlcmd -query "select @@Servername" -database $DB -serverinstance $servername -username $username -password $pass

如果只是在“invoke-sqlcmd”行末尾对密码进行硬编码,那么它就可以工作。这是因为您不能将“get-credential”与“invoke-sqlcmd”一起使用吗?

如果是这样...我的选择是什么?

非常感谢你对

Emo 的帮助

I am trying to connect to a remote sql database and simply run the "select @@servername" query in Powershell. I'm trying to do this without using integrated security. I've been struggling with "get-credential" and "invoke-sqlcmd", only to find (I think), that you can't pass the password from "get-credential" to another Powershell cmdlets.

Here's the code I'm using:

add-pssnapin sqlserverprovidersnapin100
add-pssnapin sqlservercmdletsnapin100

# load assemblies
[Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.SqlEnum, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.SmoEnum, `
      Version=9.0.242.0, Culture=neutral, `
      PublicKeyToken=89845dcd8080cc91")
[Reflection.Assembly]::Load("Microsoft.SqlServer.ConnectionInfo, `
      Version=9.0.242.0, Culture=neutral, `PublicKeyToken=89845dcd8080cc91")

# connect to SQL Server

$serverName = "HLSQLSRV03"
$server = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server -argumentList $serverName

# login using SQL authentication

$server.ConnectionContext.LoginSecure=$false;
$credential = Get-Credential
$userName = $credential.UserName -replace("\\","")
$pass = $credential.Password
$server.ConnectionContext.set_Login($userName)
$server.ConnectionContext.set_SecurePassword($credential.Password)
$DB = "Master"

invoke-sqlcmd -query "select @@Servername" -database $DB -serverinstance $servername -username $username -password $pass

If if just hardcode the password in at the end of the "invoke-sqlcmd" line, it works. Is this because you can't use "get-credential" with "invoke-sqlcmd"?

If so...what are my alternatives?

Thanks so much for you help

Emo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眼前雾蒙蒙 2024-09-01 16:35:58

您的命令不起作用,因为 -password 属性期望密码为纯文本字符串,而不是您传递给它的 SecureString 值。

尽管看起来您正在尝试使用两种不同的方法来执行 SQL - 即使用程序集以及使用 invoke-sqlcmd。

如果使用 invoke-sqlcmd,则无需加载程序集并执行所有其他操作。

要使 invoke-sqlcmd 正常工作,您所需要做的就是将密码转换为纯文本:

$serverName = "HLSQLSRV03"
$DB = "Master" 
$credential = Get-Credential 
$userName = $credential.UserName.Replace("\","")  
$pass = $credential.GetNetworkCredential().password  

invoke-sqlcmd -query "select @@Servername" -database $DB -serverinstance $servername -username $username -password $pass

Your command isn't working because the -password attribute is expecting the password as a plain-text string, not the SecureString value you're passing it.

Although it looks like you're trying to use two different methods for executing the SQL - ie using the assemblies as well as using invoke-sqlcmd.

If using invoke-sqlcmd, you don't need to load the assemblies and do all the other stuff.

All you need to do to get your invoke-sqlcmd to work is to convert your password to plain-text:

$serverName = "HLSQLSRV03"
$DB = "Master" 
$credential = Get-Credential 
$userName = $credential.UserName.Replace("\","")  
$pass = $credential.GetNetworkCredential().password  

invoke-sqlcmd -query "select @@Servername" -database $DB -serverinstance $servername -username $username -password $pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文