安装 SQL Server PowerShell 模块Install the SQL Server PowerShell module

适用于: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics(SQL 数据仓库)是并行数据仓库 APPLIES TO: YesSQL Server YesAzure SQL Database YesAzure Synapse Analytics (SQL Data Warehouse) YesParallel Data Warehouse

本文提供安装 SqlServer PowerShell 模块的指导。This article provides directions for installing the SqlServer PowerShell module.

适用于 SQL Server 的 PowerShell 模块PowerShell modules for SQL Server

提供两种 SQL Server PowerShell 模块:There are two SQL Server PowerShell modules:

  • SqlServer:SqlServer 模块包括新的 cmdlet,用于支持最新的 SQL 功能。SqlServer: The SqlServer module includes new cmdlets to support the latest SQL features. 该模块还包含 SQLPS 中 cmdlet 的更新版本 。The module also contains updated versions of the cmdlets in SQLPS. 若要下载 SqlServer 模块,请在 PowerShell 库中转到 SqlServer 模块To download the SqlServer module, go to SqlServer module in the PowerShell Gallery.
  • SQLPS:虽然 SQL Server 安装附带了 SQLPS 模块(用于向后兼容),但该模块不再更新。SQLPS: The SQLPS module is included with the SQL Server installation (for backward compatibility), but is no longer being updated. 最新的 PowerShell 模块是 SqlServer 模块 。The most up-to-date PowerShell module is the SqlServer module.

备注

PowerShell 库中这些版本的 SqlServer 模块支持版本控制并且要求 PowerShell 5.0 或更高版本。The versions of the SqlServer module in the PowerShell Gallery support versioning and require PowerShell version 5.0 or greater.

有关帮助主题,请转到:For help topics, go to:

SQL Server Management StudioSQL Server Management Studio

从版本 17.0 开始,SQL Server Management Studio (SSMS) 不会安装任何 PowerShell 模块。SQL Server Management Studio (SSMS), beginning with version 17.0, doesn't install either PowerShell module. 要在 SSMS 中使用 PowerShell,则必须从 PowerShell 库安装 SqlServer 模块。To use PowerShell with SSMS, install the SqlServer module from the PowerShell Gallery.

备注

对于 16.x 版本的 SSMS,SQL Server Management Studio (SSMS) 中包含早期版本的 SqlServer 模块With version 16.x of SSMS, an earlier version of the SqlServer module is included with SQL Server Management Studio (SSMS)

Azure Data StudioAzure Data Studio

Azure Data Studio 不会安装任何一个 PowerShell 模块。Azure Data Studio doesn't install either PowerShell module. 若要将 PowerShell 与 Azure Data Studio 一起使用,请从 PowerShell 库安装 SqlServer 模块。To use PowerShell with Azure Data Studio, install the SqlServer module from the PowerShell Gallery.

可以使用 PowerShell 扩展,此扩展在 Azure Data Studio 中提供丰富的 PowerShell 编辑器支持。You can use the PowerShell extension, which provides rich PowerShell editor support in Azure Data Studio.

安装或更新 SqlServer 模块Installing or updating the SqlServer module

要从 PowerShell 库安装 SqlServer 模块,请以管理员身份启用 PowerShell 会话。To install the SqlServer module from the PowerShell Gallery, start a PowerShell session as an administrator. 还可以以管理员身份启动 Azure Data Studio,并在集成终端的 PowerShell 会话中运行这些命令。You also start Azure Data Studio as an administrator and run these commands in a PowerShell session in the integrated terminal.

安装 SqlServer 模块Install the SqlServer module

在 PowerShell 会话中运行以下命令,为所有用户安装 SqlServer 模块:Run the following command in your PowerShell session to install the SqlServer module for all users:

Install-Module -Name SqlServer

查看已安装的 SqlServer 模块的版本To view the versions of the SqlServer module installed

执行以下命令来查看已安装的 SqlServer 模块的版本Execute the following command to see the versions of the SqlServer module that have been installed

Get-Module SqlServer -ListAvailable

为当前用户而不是以管理员身份安装Install for the current user rather than as an administrator

如果不能以管理员身份运行 PowerShell 会话,则使用以下命令为当前用户安装:If you aren't able to run the PowerShell session as an administrator, install for the current user using the following command:

Install-Module -Name SqlServer -Scope CurrentUser

覆盖先前版本的 SqlServer 模块To overwrite a previous version of the SqlServer module

还可以使用 Install-Module 命令覆盖以前的版本。You can also use the Install-Module command to overwrite a previous version.

Install-Module -Name SqlServer -AllowClobber

备注

PowerShell 始终使用安装的最新模块。PowerShell always uses the latest module installed.

更新 SqlServer 模块的安装版本Update the installed version of the SqlServer module

当 SqlServer 模块的更新版本可用时,可使用以下命令安装更新版本:When updated versions of the SqlServer module are available, you can install the newer version using the following command:

Install-Module -Name SqlServer -AllowClobber

可以使用 Update-Module 命令安装最新版本的 SQLServer PowerShell 模块,但这不会删除旧版本。You can use the Update-Module command to install the newest version of the SQLServer PowerShell module, but that doesn't remove older versions. 它将并行安装更新版本,以使你能够体验最新版本,但仍会安装旧模块。It installs the newer version side by side to allow you the ability to experiment with the latest version, yet still have older modules installed.

但是,如果不想保留旧模块版本,可以使用 Uninstall-Module 命令删除以前的版本。However, if you don’t want to keep older module versions, then you can use the Uninstall-Module command to remove previous versions.

如果安装了多个版本,则可以使用以下命令列出:You can use the following command to list if more than one version is installed:

Get-Module SqlServer -ListAvailable

使用以下命令删除旧版本:You can use the following command to remove older versions:

Uninstall-module -Name SQLServer -RequiredVersion "<version number>" -AllowClobber

故障排除Troubleshooting

如果在安装过程中出现问题,请参阅安装模块文档安装模块参考If you run into problems installing, see the Install-Module documentation and Install-Module reference.

使用 SqlServer 模块的特定版本Using a specific version of the SqlServer module

要使用特定版本的模块,请使用类似以下命令的特定版本号将其导入:To use a specific version of the module, import it with a specific version number similar to the following command:

Import-Module SqlServer -Version 21.1.18080

SqlServer 模块的预发行版本Pre-release versions of the SqlServer module

PowerShell 库中可能提供 SqlServer 模块的预发行(或“预览”)版本。Pre-release (or "preview") versions of the SqlServer module may be available in the PowerShell Gallery.

重要

可通过传递 -AllowPrerelease 切换,使用更新后的 Find-Module 和 Install-Module cmdlet(属于 PowerShellGet 模块)找到并安装这些版本。 These versions may be discovered and installed by using the updated Find-Module and Install-Module cmdlets that are part of the PowerShellGet module by passing the -AllowPrerelease switch. 若要使用这些 cmdlet,请安装 PowerShellGet 模块,然后打开一个新的会话。To use these cmdlets, install the PowerShellGet module and then open a new session.

发现 SqlServer 模块的预发行版本To discover pre-release versions of the SqlServer module

若要找到 SqlServer 模块的预发行(预览)版本,可运行以下命令:To discover the pre-release (preview) versions of the SqlServer module, run the following command:

Find-Module SqlServer -AllowPrerelease

安装 SqlServer 模块的特定预发行版本To install a specific pre-release version of the SqlServer module

若要安装模块的特定预发行版本,请使用特定版本号安装该模块。To install a specific pre-release version of the module, install it with a specific version number.

可以尝试使用以下命令:You can try to use the following command:

Install-Module SqlServer -RequiredVersion 21.1.18040-preview -AllowPrerelease

Linux 上的 SQL Server PowerShellSQL Server PowerShell on Linux

若要了解如何在 Linux 上安装 SQL Server PowerShell,请访问通过 PowerShell Core 管理 Linux 上的 SQL ServerVisit Manage SQL Server on Linux with PowerShell Core to see how to install SQL Server PowerShell on Linux.