将PowerShell连接到SQL Server –使用其他帐户

介绍 (Introduction)

In a previous article on Connecting PowerShell to SQL Server I went over how you use various methods in PowerShell to connect to SQL Server. In those examples though I only touched on using the current user that is running “PowerShell.exe”. In this article I want to pick up and go over how you would use the same methods, but as a different account. I will cover using both Windows Authentication (where supported) and SQL Login authentication.

在上一篇有关将PowerShell连接到SQL Server的文章中,我介绍了如何在PowerShell中使用各种方法连接到SQL Server。 在这些示例中,尽管我仅涉及使用正在运行“ PowerShell.exe”的当前用户。 在本文中,我想介绍一下如何使用相同的方法,但使用不同的帐户。 我将介绍如何使用Windows身份验证(在支持的地方)和SQL登录身份验证。

There is one object in PowerShell utilized for making connections as a separate account: PSCredential. This object is used to capture the new credentials. In most cases it allows you to securely pass in those credentials without making the account information visible in your script. In the examples below I am just going to use the “Get-Credential” command to build this object. The article referenced at the end of this article for securing your passwords in PowerShell goes over this command and others in more detail.

PowerShell中有一个对象用于作为独立帐户进行连接:PSCredential。 该对象用于捕获新的凭证。 在大多数情况下,它使您可以安全地传递这些凭据,而无需在脚本中显示帐户信息。 在下面的示例中,我将使用“ Get-Credential”命令来构建此对象。 本文末尾引用的有关在PowerShell中保护密码的文章更详细地介绍了此命令和其他命令。

选件 (Options)

As the previous article, the following are the options we will cover:

与上一篇文章一样,以下是我们将介绍的选项:

  • SQL Server PowerShell (SQLPS)

    SQL Server PowerShell(SQLPS)
  • SQL Server Management Objects (SMO)

    SQL Server管理对象(SMO)
  • .NET (System.Data.SqlClient)

    .NET(System.Data.SqlClient)

SQL Server PowerShell(SQLPS) (SQL Server PowerShell (SQLPS))

While SQLPS is still around a better module that Microsoft released with SSMS 2016 was renamed to “SqlServer”. This module is being expanded upon as SSMS updates are released so is more robust that SQLPS module. The methods shown below would work with either module though.

尽管SQLPS仍然围绕着一个更好的模块,但微软随SSMS 2016发布的模块被重命名为“ SqlServer”。 随着SSMS更新的发布,此模块正在扩展,因此比SQLPS模块更强大。 不过,以下所示的方法可与任一模块一起使用。

SqlServer提供程序 (SqlServer Provider)

SQLPS.exe, the utility, is not going to be touched on because it does not support changing the account before you connect. When you open that utility you are

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值