Powershell SQLServer Modules

时间:2016-07-11 20:11:27

标签: sql-server powershell

I am developing an automation system that will grant access to SQL Server databases through Powershell by running a query. I am able to do this just fine, but my question is, do I need to install SQL Server Management Studio in order to import the modules

For example:

import-module sqlserver

When I attempted this before I installed studio I was unable to install because Powershell could not find the module.

Once I had the module imported, I was able to run the follow command:

Invoke-sqlcmd -query "grant select, insert, update on test to sqlsupport;" -serverinstance "test\test"

Is there a way to have the modules available without studio?

2 个答案:

答案 0 :(得分:1)

Yes, in the same installer for SSMS, by selecting the components to install, you can choose to install only the PowerShell cmdlets and not the rest of SSMS.

You should also make sure you're using the latest module (they changed the name from SQLPS to SqlServer); as of this writing it's the July 2016 update.

答案 1 :(得分:1)

您可以将其安装为独立的PowerShell模块。

请以管理员身份打开Powershell并执行以下操作:

  1. 运行Set-ExecutionPolicy unrestricted
  2. 运行Install-Module sqlserver
  3. 运行Import-Module sqlserver

这应该允许您通过Powershell运行Sql命令,而无需安装SQL Server Management Studio。