共用方式為


關於 PSSnapins

簡短描述

描述 Windows PowerShell 嵌入式管理單元,並示範如何使用和管理它們。

詳細描述

Windows PowerShell 嵌入式管理單元是包含 Windows PowerShell 提供者和/或 Cmdlet 的 Microsoft .NET Framework 元件。 Windows PowerShell 包含一組基本嵌入式管理單元,但您可以藉由新增包含您建立或從其他人取得之提供者和 Cmdlet 的嵌入式管理單元,來擴充 Windows PowerShell 的功能和價值。

當您新增嵌入式管理單元時,它所包含的 Cmdlet 和提供者會立即可用於目前的會話,但變更只會影響目前的會話。

若要將嵌入式管理單元新增至所有未來的會話,請將它儲存在 Windows PowerShell 配置檔中。 您也可以使用 Export-Console Cmdlet 將嵌入式管理單元名稱儲存到主控台檔案,然後在未來的工作階段中使用它。 您甚至可以儲存多個控制台檔案,每個檔案都有一組不同的嵌入式管理單元。

注意: (PSSnapins) Windows PowerShell 嵌入式管理單元可用於 Windows PowerShell 3.0 和 Windows PowerShell 2.0。 後續版本可能會變更或無法使用。 若要封裝 Windows PowerShell Cmdlet 和提供者,請使用模組。 如需建立模組並將嵌入式管理單元轉換成模組的相關信息,請參閱撰寫 Windows PowerShell 模組

尋找嵌入式管理單元

若要取得電腦上的 Windows PowerShell 嵌入式管理單元清單,請輸入:

Get-PSSnapin

若要取得每個 Windows PowerShell 提供者的嵌入式管理單元,請輸入:

Get-PSProvider | Format-List name, pssnapin

若要取得 Windows PowerShell 嵌入式管理單元中的 Cmdlet 清單,請輸入:

Get-Command -Module <snap-in_name>

安裝嵌入式管理單元

內建嵌入式管理單元會在系統中註冊,並在您啟動 Windows PowerShell 時新增至預設會話。 不過,您必須註冊您建立或從其他人取得的嵌入式管理單元,然後將嵌入式管理單元新增至您的會話。

註冊嵌入式管理單元

Windows PowerShell 嵌入式管理單元是以編譯成 .dll 檔案 .NET Framework 語言所撰寫的程式。 若要在嵌入式管理單元中使用提供者和 Cmdlet,您必須先註冊嵌入式管理單元, (將其新增至登錄) 。

大部分嵌入式管理單元都包含安裝程式 (.exe 或 .msi 檔案) ,為您註冊 .dll 檔案。 不過,如果您收到嵌入式管理單元作為 .dll 檔案,您可以在系統上註冊它。 如需詳細資訊,請參閱 MSDN 連結庫中 如何註冊 Cmdlet、提供者和主應用程式

若要取得系統上所有已註冊的嵌入式管理單元,或確認嵌入式管理單元已註冊,請輸入:

Get-PSSnapin -registered

將嵌入式管理單元新增至目前的工作階段

若要將已註冊的嵌入式管理單元新增至目前的會話,請使用 Add-PSSnapin Cmdlet。 例如,若要將 Microsoft SQL Server 嵌入式管理單元新增至會話,請輸入:

Add-PSSnapin sql

完成命令之後,會話中即可使用嵌入式管理單元中的提供者和 Cmdlet。 不過,除非您儲存它們,否則它們只能在目前的會話中使用。

儲存嵌入式管理單元

若要在未來 Windows PowerShell 會話中使用嵌入式管理單元,請將 Add-PSSnapin 命令新增至您的 Windows PowerShell 配置檔。 或者,將嵌入式管理單元名稱導出至主控台檔案。

如果您將 Add-PSSnapin 命令新增至配置檔,則未來所有 Windows PowerShell 工作階段皆可使用。 如果您在工作階段中匯出嵌入式管理單元的名稱,您只能在需要嵌入式管理單元時使用匯出檔案。

若要將 Add-PSSnapin 命令新增至 Windows PowerShell 設定檔,請開啟配置檔、貼上或輸入命令,然後儲存配置檔。 如需詳細資訊,請參閱 about_Profiles。

若要從主控台檔案 (.psc1) 中的會話儲存嵌入式管理單元,請使用 Export-Console Cmdlet。 例如,若要將目前會話設定中的嵌入式管理單元儲存至目前目錄中的 NewConsole.psc1 檔案,請輸入:

Export-Console NewConsole

如需詳細資訊,請參閱 Export-Console。

使用主控台檔案開啟 WINDOWS POWERSHELL

若要使用包含嵌入式管理單元的控制台檔案,請從命令提示字元 Cmd.exe 或另一個 Windows PowerShell 會話中啟動 Windows PowerShell (PowerShell.exe) 。 使用 PsConsoleFile 參數來指定包含嵌入式管理單元的主控台檔案。 例如,下列命令會以 NewConsole.psc1 控制台檔案啟動 Windows PowerShell:

PowerShell.exe -psconsolefile NewConsole.psc1

嵌入式管理單元中的提供者和 Cmdlet 現在可用於會話。

拿掉嵌入式管理單元

若要從目前的會話中移除 Windows PowerShell 嵌入式管理單元,請使用 Remove-PSSnapin Cmdlet。 例如,若要從目前的會話中移除 SQL Server 嵌入式管理單元,請輸入:

Remove-PSSnapin sql

此 Cmdlet 會從會話中移除嵌入式管理單元。 嵌入式管理單元仍會載入,但不再提供其支援的提供者和 Cmdlet。

內建命令

在 Windows PowerShell 2.0 和 Windows PowerShell 3.0 和更新版本中的舊版主機程式中,隨 Windows PowerShell 一起安裝的內建命令會封裝在自動新增至每個 Windows PowerShell 會話的嵌入式管理單元中。

從 Windows PowerShell 3.0 開始,在較新的樣式主機程式中 -- 使用 InitialSessionState.CreateDefault2 方法啟動會話的程式 -- 內建命令會封裝在模組中。 例外狀況是 Microsoft.PowerShell.Core,一律會顯示為嵌入式管理單元。 根據預設,核心嵌入式管理單元會包含在每個會話中。 內建模組會在首次使用時自動載入。

注意:遠端會話,包括使用 New-PSSession Cmdlet 啟動的會話,是內建命令封裝在嵌入式管理單元中的較舊樣式會話。

下列嵌入式管理單元 (或模組) 會隨 Windows PowerShell 一起安裝。

  • Microsoft.PowerShell.Core - 包含用來管理 Windows PowerShell 基本功能的提供者和 Cmdlet。 它包含 FileSystem、Registry、Alias、Environment、Function 和 Variable 提供者,以及 Get-Help、Get-Command 和 Get-History 等基本 Cmdlet。

  • Microsoft.PowerShell.Host - 包含 Windows PowerShell 主機所使用的 Cmdlet,例如 Start-Transcript 和停止文字記錄。

  • Microsoft.PowerShell.Management - 包含 Cmdlet,例如用來管理 Windows 功能 Get-Service 和 Get-ChildItem。

  • Microsoft.PowerShell.Security - 包含用來管理 Windows PowerShell 安全性的憑證提供者和 Cmdlet,例如 Get-Acl、Get-AuthenticodeSignature 和 ConvertTo-SecureString。

  • Microsoft.PowerShell.Utility - 包含用來操作對象和數據的 Cmdlet,例如 Get-Member、Write-Host 和 Format-List。

  • Microsoft.WSMan.Management - 包含管理 Windows 遠端管理服務的 WSMan 提供者和 Cmdlet,例如 Connect-WSMan 和 Enable-WSManCredSSP。

記錄嵌入式管理單元事件

從 Windows PowerShell 3.0 開始,您可以將模組的 LogPipelineExecutionDetails 屬性和嵌入式管理單元設定為 TRUE,以記錄 Windows PowerShell 模組和嵌入式管理單元中的 Cmdlet 執行事件。 如需詳細資訊,請參閱 about_EventLogs

另請參閱

Add-PSSnapin

Get-PSSnapin

Remove-PSSnapin

Export-Console

Get-Command

about_Profiles

about_Modules

關鍵 字

about_Snapins、about_Snap_ins、about_Snap