共用方式為


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 [ -argsargument_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_blockargument_array 是 PowerShell 變數的陣列,其中包含 script_block中 PowerShell 命令的引數。

string [ command_parameters ]
指定包含要執行之 PowerShell 命令的字串。 使用 「&{command}」 格式。 引號表示字串,而invoke運算子 (&) 會導致 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 執行腳本執行原則設定為 [限制]。 如此即不會執行任何 PowerShell 指令碼。 您可以使用 Set-ExecutionPolicy Cmdlet 來允許執行已簽署的指令碼或任何指令碼。 建議您只執行來自信任來源的指令碼,並且利用適當的 NTFS 權限來保護所有輸入和輸出檔案。 如需有關啟用 PowerShell 指令碼的詳細資訊,請參閱 執行 Windows PowerShell 指令碼

SQL Server 2008 和 SQL Server 2008 R2 中的公用程式版本sqlps會實作為 1.0 迷你殼層 Windows PowerShell。 迷你 Shell 有一些限制,例如不允許使用者載入迷你 Shell 所載入之嵌入式管理單元以外的嵌入式管理單元。 這些限制不適用於已變更為使用sqlps模組的 SQL Server 2012 和更新版本的公用程式。

範例

sqlps -NoLogo  

B. 從命令提示字元處執行 SQL Server PowerShell 指令碼

sqlps -Command "&{.\MyFolder.MyScript.ps1}"  

C. 從命令提示字元處執行 SQL Server PowerShell 指令碼,並在指令碼完成之後維持執行狀態

sqlps -NoExit -Command "&{.\MyFolder.MyScript.ps1}"  

另請參閱

啟用或停用伺服器網路通訊協定
SQL Server PowerShell