sqlps 实用工具

sqlps 实用工具会启动一个 Windows PowerShell 2.0 会话,并加载和注册 SQL Server PowerShell 提供程序和 cmdlet。 您可以输入 PowerShell 命令或脚本,它们使用 SQL Server PowerShell 组件来处理 SQL Server 的实例及其对象。

重要说明重要提示

后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。请改用 sqlps PowerShell 模块。 有关 sqlps 模块的详细信息,请参阅导入 SQLPS 模块

语法

sqlps 
[ [ [ -NoLogo ][ -NoExit ][ -NoProfile ]
    [ -OutPutFormat { Text | XML } ] [ -InPutFormat { Text | XML } ]
  ]
  [ -Command { -
             | script_block [ -args argument_array ]
             | string [ command_parameters ]
             }
  ]
]
[ -? | -Help ]

参数

  • -NoLogo
    指定 sqlps 实用工具在启动时隐藏版权标志。

  • -NoExit
    指定 sqlps 实用工具在完成启动命令后仍继续运行。

  • -NoProfile
    指定 sqlps 实用工具不加载用户配置文件。 用户配置文件记录 PowerShell 会话期间常用的别名、函数和变量。

  • -OutPutFormat { Text | XML }
    指定 sqlps 实用工具输出应为文本字符串格式 (Text) 或序列化的 CLIXML 格式 (XML)。

  • -InPutFormat { Text | XML }
    指定 sqlps 实用工具输入应为文本字符串格式 (Text) 或序列化的 CLIXML 格式 (XML)。

  • -Command
    指定要使 sqlps 实用工具运行的命令。 sqlps 实用工具运行命令,然后退出,除非也指定了 -NoExit。 请不要在 -Command 后指定任何其他开关,如果指定,它们将被读作命令参数。

  • -
    -Command - 指定 sqlps 实用工具从标准输入读取输入。

  • script_block [ -argsargument_array ]
    指定要运行的 PowerShell 命令块,块必须用大括号 {} 括起来。 仅当从 PowerShell 或其他 sqlps 实用工具会话调用 sqlps 实用工具时,才能指定 Script_block。 argument_array 是 PowerShell 变量的数组,包含 script_block 中 PowerShell 命令的参数。

  • string [ command_parameters ]
    指定包含要运行的 PowerShell 命令的字符串。 使用格式**“&{command}”**。 引号指示一个字符串,调用运算符 (&) 使 sqlps 实用工具运行命令。

  • [ -?| -Help ]
    显示 sqlps 实用工具选项的语法摘要。

注释

sqlps 实用工具启动 PowerShell 环境 (PowerShell.exe) 并加载 SQL Server PowerShell 模块。 该模块也命名为 sqlps,它将加载并注册以下 SQL Server PowerShell 管理单元:

  • Microsoft.SqlServer.Management.PSProvider.dll

    实现 SQL Server PowerShell 提供程序和关联的 cmdlet,如 Encode-SqlNameDecode-SqlName

  • Microsoft.SqlServer.Management.PSSnapin.dll

    实现 Invoke-SqlcmdInvoke-PolicyEvaluation cmdlet。

可以使用 sqlps 实用工具执行下列操作:

  • 以交互方式运行 PowerShell 命令。

  • 运行 PowerShell 脚本文件。

  • 运行 SQL Server cmdlet。

  • 使用 SQL Server 提供程序路径可以浏览 SQL Server 对象的层次结构。

默认情况下,将脚本执行策略设置为 Restricted 来运行 sqlps 实用工具。 这样可以防止运行任何 PowerShell 脚本。 您可以使用 Set-ExecutionPolicy cmdlet 来允许运行签名的脚本或任意脚本。 请仅运行来自受信任源的脚本,并通过使用适当的 NTFS 权限来保证所有输入和输出文件的安全。 有关启用 PowerShell 脚本的详细信息,请参阅 Running Windows PowerShell Scripts(运行 Windows PowerShell 脚本)。

在 SQL Server 2008 和 SQL Server 2008 R2 中,此 sqlps 实用工具版本已作为 Windows PowerShell 1.0 微型 shell 实现。 微型外壳程序具有某些限制,例如不允许用户加载不是由微型外壳程序所加载的管理单元。 这些限制并不适用于该实用工具的 SQL Server 2012 版本,此版本已更改为使用 sqlps 模块。

示例

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