適用於:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
分析平台系統(PDW)
Microsoft Fabric 中的 SQL 資料庫
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 ]
引數
-無標誌
指定 sqlps 公用程式在啟動時隱藏著作權橫幅。
-禁止出口
指定 sqlps 公用程式在啟動命令完成之後繼續執行。
-無簡介
指定 sqlps 公用程式不要載入使用者設定檔。 使用者設定檔會記錄在 PowerShell 工作階段之間常用的別名、函數與變數。
-OutPutFormat { 文字 |XML }
指定 sqlps 公用程式輸出的格式化為文字字串 (Text) 或序列化 CLIXML 格式 (XML)。
-InPutFormat { 文字 |XML }
指定 sqlps 公用程式的輸入要格式化為文字字串 (Text) 或序列化的 CLIXML 格式 (XML)。
-令
指定 sqlps 公用程式要執行的命令。
sqlps 公用程式會執行指令,然後結束,除非-NoExit也指定。 請勿在 之後指定任何其他開關。-Command 它們會讀取為命令參數。
-
-Command- 指定 SQLPS 公用程式從標準輸入讀取輸入。
script_block [ -argsargument_array ]
指定要執行的 PowerShell 命令區塊,此區塊必須以大括號括住:{}。 只有當從 PowerShell 或其他 sqlps 公用程式工作階段呼叫 sqlps 公用程式時,才能指定 Script_block。 argument_array 是 PowerShell 變數的陣列,其中包含 script_block 中 PowerShell 命令的引數。
字符串 [ command_parameters ]
指定包含要執行之 PowerShell 命令的字串。 請使用 "{<command>}" 格式。 引號指出字串,呼叫運算子 (&) 會導致 sqlps 公用程式執行指令 (<command>)。
}, |-Help ]
顯示 sqlps 公用程式選項的語法摘要。
備註
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 物件的階層。
根據預設, 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 迷你殼層。 迷你殼層有某些限制,例如不允許使用者載入迷你殼層載入的嵌入式管理單元以外的嵌入式管理單元。 這些限制不適用於 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}"