将用户名和密码传递给 get-credential 或运行 sql 查询,而无需在 Powershell 中使用 invoke-sqlcmd
我正在尝试连接到远程 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的命令不起作用,因为 -password 属性期望密码为纯文本字符串,而不是您传递给它的 SecureString 值。
尽管看起来您正在尝试使用两种不同的方法来执行 SQL - 即使用程序集以及使用 invoke-sqlcmd。
如果使用 invoke-sqlcmd,则无需加载程序集并执行所有其他操作。
要使 invoke-sqlcmd 正常工作,您所需要做的就是将密码转换为纯文本:
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: