sqlps [ [ [ -NoLogo ][ -NoExit ][ -NoProfile ] [ -OutPutFormat { Text | XML } ] [ -InPutFormat { Text | XML } ] ] [ -Command { - | script_block [ -args argument_array ] | string [ command_parameters ] } ]][ -? | -Help ]
sqlps 实用工具启动 PowerShell 环境 (PowerShell.exe) 并加载 SQL Server PowerShell 模块。 该模块也命名为 sqlps,它将加载并注册以下 SQL Server PowerShell 管理单元:
-
Microsoft.SqlServer.Management.PSProvider.dll
实现 SQL Server PowerShell 提供程序和关联的 cmdlet,如 Encode-SqlName 和 Decode-SqlName。
-
Microsoft.SqlServer.Management.PSSnapin.dll
实现 Invoke-Sqlcmd 和 Invoke-PolicyEvaluation cmdlet。
可以使用 sqlps 实用工具执行下列操作:
-
以交互方式运行 PowerShell 命令。
-
运行 PowerShell 脚本文件。
-
运行 SQL Server cmdlet。
-
使用 SQL Server 提供程序路径可以浏览 SQL Server 对象的层次结构
A. 以默认的交互模式运行 sqlps 实用工具,并且无版权标志
sqlps -NoLogo
B. 从命令提示符下运行 SQL Server PowerShell 脚本
sqlps -Command "&{.\MyFolder.MyScript.ps1}"
C. 从命令提示符下运行 SQL Server PowerShell 脚本,并在脚本完成后继续运行
sqlps -NoExit -Command "&{.\MyFolder.MyScript.ps1}"
微软对SQL Server提供了如下几种PowerShell扩展,可以使用Get-PSSnapin -Registered查看
当添加了微软的PSSnapin后,就会添加SQL Server驱动器:
Add-PSSnapin -Name SqlServerProviderSnapin100Add-PSSnapin -Name SqlServerCmdletSnapin100 Get-PSDrive
Set-Location SQLSERVER:\SQL\BrooksPC$svr = Get-ChildItem | Select Edition, VersionStringswitch ($svr.VersionString){"8.194.0" {Write-Host $svr.VersionString "SQL Server 2000 RTM" $svr.Edition; break}"8.384.0" {Write-Host $svr.VersionString "SQL Server 2000 SP1" $svr.Edition; break}"8.534.0" {Write-Host $svr.VersionString "SQL Server 2000 SP2" $svr.Edition; break}"8.760.0" {Write-Host $svr.VersionString "SQL Server 2000 SP3" $svr.Edition; break}"8.00.2039" {Write-Host $svr.VersionString "SQL Server 2000 SP4" $svr.Edition; break}"9.00.1399" {Write-Host $svr.VersionString "SQL Server 2005 RTM" $svr.Edition; break}"9.00.2047" {Write-Host $svr.VersionString "SQL Server 2005 SP1" $svr.Edition; break}"9.00.3042" {Write-Host $svr.VersionString "SQL Server 2005 SP2" $svr.Edition; break}"9.00.4035" {Write-Host $svr.VersionString "SQL Server 2005 SP3" $svr.Edition; break}"10.0.1600" {Write-Host $svr.VersionString "SQL Server 2008 RTM" $svr.Edition; break}"10.0.2531" {Write-Host $svr.VersionString "SQL Server 2008 SP1" $svr.Edition; break}"10.50.1600.1" {Write-Host $svr.VersionString "SQL Server 2008 R2 RTM" $svr.Edition; break}default {Write-Host $svr.VersionString "version cannot be determined" $svr.Edition; break}}