Add-PSSnapin
新增一或多個 Windows PowerShell 嵌入式管理單元至目前的工作階段。
Syntax
Add-PSSnapin
[-Name] <String[]>
[-PassThru]
[<CommonParameters>]
Description
Add-PSSnapin Cmdlet 會將已註冊的 Windows PowerShell 嵌入式管理單元新增至目前的會話。 新增嵌入式管理單元之後,您就可以在目前的工作階段中使用嵌入式管理單元支援的 Cmdlet 與提供者。
若要將嵌入式管理單元新增至所有未來的 Windows PowerShell 會話,請將 Add-PSSnapin 命令新增至您的 Windows PowerShell 配置檔。 如需詳細資訊,請參閱 about_Profiles。
從 Windows PowerShell 3.0 開始,Windows PowerShell 中包含的核心命令已經封裝成模組。 Microsoft.PowerShell.Core 是一個例外,它是一個嵌入式管理單元 (PSSnapin)。 依照預設,只會新增 Microsoft.PowerShell.Core 嵌入式管理單元至工作階段。 模組會在第一次使用時自動匯入,而您可以使用 Import-Module Cmdlet 來匯入它們。
範例
範例 1:新增嵌入式管理單元
PS C:\> Add-PSSnapIn -Name Microsoft.Exchange, Microsoft.Windows.AD
此命令會將 Microsoft Exchange 與 Active Directory 嵌入式管理單元新增至目前的工作階段。
範例 2:新增所有已註冊的嵌入式管理單元
PS C:\> Get-PSSnapin -Registered | Add-PSSnapin -Passthru
此命令會將所有已註冊的 Windows PowerShell 嵌入式管理單元新增至工作階段。 它會使用 Get-PSSnapin Cmdlet 搭配 Registered 參數來取得代表每個已註冊嵌入式管理單元的物件。管線運算子 (|) 將結果傳遞至 Add-PSSnapin,這會將它們新增至會話。 PassThru 參數會傳回代表每個新增嵌入式管理單元的物件。
範例 3:註冊嵌入式管理單元並加以新增
The first command gets snap-ins that have been added to the current session that include the snap-ins that are installed with Windows PowerShell. In this example, ManagementFeatures is not returned. This indicates that it has not been added to the session.
PS C:\> Get-PSSnapin
The second command gets snap-ins that have been registered on your system, which includes those that have already been added to the session. It does not include the snap-ins that are installed with Windows PowerShell. In this case, the command does not return any snap-ins. This indicates that the ManagementFeatures snapin has not been registered on the system.
PS C:\> Get-PSSnapin -Registered
The third command creates an alias, installutil, for the path of the InstallUtil tool in .NET Framework.
PS C:\> Set-Alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil.exe
The fourth command uses the InstallUtil tool to register the snap-in. The command specifies the path of ManagementCmdlets.dll, the file name or module name of the snap-in.
PS C:\> installutil C:\Dev\Management\ManagementCmdlets.dll
The fifth command is the same as the second command. This time, you use it to verify that the ManagementCmdlets snap-in is registered.
PS C:\> Get-PSSnapin -Registered
The sixth command uses the **Add-PSSnapin** cmdlet to add the ManagementFeatures snap-in to the session. It specifies the name of the snap-in, ManagementFeatures, not the file name.
PS C:\> add-pssnapin ManagementFeatures
To verify that the snap-in is added to the session, the seventh command uses the *Module* parameter of the Get-Command cmdlet. It displays the items that were added to the session by a snap-in or module.
PS C:\> Get-Command -Module ManagementFeatures
You can also use the **PSSnapin** property of the object that the **Get-Command** cmdlet returns to find the snap-in or module in which a cmdlet originated. The eighth command uses dot notation to find the value of the PSSnapin property of the Set-Alias cmdlet.
PS C:\> (Get-Command Set-Alias).pssnapin
此範例示範在您的系統上註冊嵌入式管理單元,然後將它們新增至您的工作階段的處理程序。 它會使用ManagementFeatures,這是在名為 ManagementCmdlets.dll 的檔案中實作的虛構嵌入式管理單元。
參數
-Name
指定嵌入式管理單元的名稱。 這是 Name,而不是 AssemblyName 或 ModuleName。 允許使用萬用字元。
若要在系統上尋找已註冊嵌入式管理單元的名稱,請輸入 Get-PSSnapin -Registered
。
Type: | String[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-PassThru
表示這個 Cmdlet 會傳回代表每個新增嵌入式管理單元的物件。 根據預設,此 Cmdlet 不會產生任何輸出。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
None
您無法使用管線將物件傳送至此 Cmdlet。
輸出
None or System.Management.Automation.PSSnapInInfo
如果您指定 PassThru 參數,此 Cmdlet 會傳回 PSSnapInInfo 物件,代表嵌入式管理單元。 否則,此 Cmdlet 不會產生任何輸出。
備註
從 Windows PowerShell 3.0 開始,與 Windows PowerShell 一起安裝的核心命令已經封裝成模組。 在 Windows PowerShell 2.0 中,以及在更新版本的 Windows PowerShell 中建立較舊樣式會話的主機程式中,核心命令會封裝在 PSSnapins) (嵌入式管理單元中。 Microsoft.PowerShell.Core 是一個例外,它一律是一個嵌入式管理單元。 此外,遠端工作階段 (例如由 New-PSSession cmdlet 所啟動的遠端工作階段) 都是包含核心嵌入式管理單元的舊式工作階段。
如需使用核心模組建立較新樣式會話之 CreateDefault2 方法的相關信息,請參閱 MSDN 連結庫中的 CreateDefault2 方法 。
如需嵌入式管理單元的詳細資訊,請參閱 MSDN 連結庫中about_PSSnapins和如何 Create Windows PowerShell 嵌入式管理單元。
Add-PSSnapin 只會將嵌入式管理單元新增至目前的會話。 若要將嵌入式管理單元新增至所有 Windows PowerShell 工作階段,請將它新增至您的 Windows PowerShell 設定檔。 如需詳細資訊,請參閱 about_Profiles。
您可以使用 Microsoft .NET Framework 安裝公用程式,新增任何已註冊的嵌入式管理單元。 如需詳細資訊,請參閱 MSDN 連結庫中 如何註冊 Cmdlet、提供者和主應用程式 。
若要取得電腦上註冊的嵌入式管理單元清單,請輸入
Get-PSSnapin -Registered
。新增嵌入式管理單元之前,Add-PSSnapin 會檢查嵌入式管理單元的版本,以確認它是否與目前版本的 Windows PowerShell 相容。 如果嵌入式管理單元版本檢查失敗,Windows PowerShell 會回報錯誤。