sqlps 公用程式

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse AnalyticsAnalytics Platform System (PDW)

Sqlps 公用程式會啟動 Windows PowerShell 工作階段並且載入和註冊 SQL Server PowerShell 提供者與 Cmdlet。 您可以輸入 PowerShell 命令或指令碼,以便使用 SQL Server PowerShell 元件來處理 SQL Server 執行個體及其物件。

重要

SQL Server 的未來版本將移除此功能。 請避免在新的開發工作中使用這項功能,並規劃修改目前使用這項功能的應用程式。 請改用 sqlps PowerShell 模組。 如需有關 sqlps 模組的詳細資訊,請參閱 Import the SQLPS Module

語法

  
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 公用程式要執行的命令。 除非同時指定了 -NoExit ,否則 sqlps 公用程式會執行此命令,然後結束。 請勿在 -Command 之後指定任何其他參數,因為這些參數將會讀取成命令參數。

-
-Command- 會指定 sqlps 公用程式從標準輸入中讀取輸入。

script_block [ -argsargument_array ]
指定要執行的 PowerShell 命令區塊,此區塊必須以大括號括住:{}。 只有當 Script_block 或其他 Script_block 公用程式工作階段呼叫了 Script_block 公用程式時,才可指定 Script_blockargument_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 物件的階層。

根據預設, sqlps 公用程式執行時會將指令碼執行原則設定為 Restricted。 如此即不會執行任何 PowerShell 指令碼。 您可以使用 Set-ExecutionPolicy Cmdlet 來允許執行已簽署的指令碼或任何指令碼。 建議您只執行來自信任來源的指令碼,並且利用適當的 NTFS 權限來保護所有輸入和輸出檔案。 如需有關啟用 PowerShell 指令碼的詳細資訊,請參閱 執行 Windows PowerShell 指令碼

SQL Server 2008 (10.0.x) 和 SQL Server 2008 R2 (10.50.x) 中的 sqlps 公用程式版本已實作為 Windows PowerShell 1.0 迷你殼層。 迷你 Shell 有一些限制,例如不允許使用者載入迷你 Shell 所載入之嵌入式管理單元以外的嵌入式管理單元。 這些限制不適用於 SQL Server 2012 (11.x) 及更高版本的公用程式,這些版本已經變更為使用 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