Export-PSSession
從另一個會話導出命令,並將其儲存在 PowerShell 模組中。
Syntax
Export-PSSession
[-Session] <PSSession>
[-OutputModule] <string>
[[-CommandName] <string[]>]
[[-FormatTypeName] <string[]>]
[-Force]
[-Encoding <string>]
[-AllowClobber]
[-ArgumentList <Object[]>]
[-CommandType <CommandTypes>]
[-Module <string[]>]
[-FullyQualifiedModule <ModuleSpecification[]>]
[-Certificate <X509Certificate2>]
[<CommonParameters>]
Description
Cmdlet Export-PSSession
會從另一個 PowerShell 工作階段取得 Cmdlet、函式、別名和其他命令類型, (本機或遠端電腦上的 PSSession) ,並將其儲存在 PowerShell 模組中。 若要將模組中的命令新增至目前的會話,請使用 Import-Module
Cmdlet。
不同於 Import-PSSession
將命令從另一個 PSSession 匯入目前的工作階段, Export-PSSession
請將命令儲存在模組中。 這些命令不會匯入目前的工作階段。
若要匯出命令,請使用 New-PSSession
Cmdlet 來建立具有您要匯出之命令的 PSSession。 Export-PSSession
然後使用 Cmdlet 匯出命令。
若要避免命令名稱衝突,預設值 Export-PSSession
為 匯出所有命令,但目前會話中存在的命令除外。 您可以使用 CommandName 參數來指定要匯出的命令。
Cmdlet Export-PSSession
使用 PowerShell 的隱含遠端功能。 當您將命令匯入目前的工作階段時,它們會在原始工作階段或原始電腦上的類似工作階段中隱含執行。
範例
範例 1:從 PSSession 導出命令
本範例會從本機計算機建立新的 PSSession 到 Server01 計算機。 除了存在於目前會話中的所有命令之外,所有命令都會匯出至本機計算機上的名為 Server01 的模組。 匯出包含命令的格式數據。
$S = New-PSSession -ComputerName Server01
Export-PSSession -Session $S -OutputModule Server01
New-PSSession
此命令會在 Server01 計算機上建立 PSSession。 PSSession 會儲存在變數中 $S
。 命令會將Export-PSSession
$S
變數的命令和格式化數據匯出至 Server01 模組。
範例 2:匯出 Get 和 Set 命令
此範例會從伺服器匯出所有 Get
和 Set
命令。
$S = New-PSSession -ConnectionUri http://exchange.microsoft.com/mailbox -Credential exchangeadmin01@hotmail.com -Authentication Negotiate
Export-PSSession -Session $R -Module exch* -CommandName Get-*, Set-* -FormatTypeName * -OutputModule $pshome\Modules\Exchange -Encoding ASCII
這些命令會將和 Set
命令從遠端電腦上的 Microsoft Exchange Server 嵌入式管理單元匯出Get
至本機電腦上的 \Modules 目錄中的 Exchange 模組$pshome
。
將模組放在 $pshome
\Modules 目錄中,可讓計算機的所有使用者存取該模組。
範例 3:從遠端計算機匯出命令
本範例會從遠端電腦上的 PSSession 匯出 Cmdlet,並將其儲存在本機電腦上的模組中。 模組中的 Cmdlet 會新增至目前的工作階段,以便使用它們。
$S = New-PSSession -ComputerName Server01 -Credential Server01\User01
Export-PSSession -Session $S -OutputModule TestCmdlets -Type Cmdlet -CommandName *test* -FormatTypeName *
Remove-PSSession $S
Import-Module TestCmdlets
Get-Help Test*
Test-Files
此命令 New-PSSession
會在 Server01 計算機上建立 PSSession,並將它儲存在變數中 $S
。 此命令會將 Export-PSSession
名稱開頭為 Test 的 Cmdlet,從中的 $S
PSSession 匯出至本機電腦上的 TestCmdlets 模組。
Cmdlet Remove-PSSession
會從目前的會話中刪除 中的 $S
PSSession。 此命令顯示 PSSession 不需要使用從工作階段匯入的命令。 Cmdlet 會將 Import-Module
TestCmdlet 模組中的 Cmdlet 新增至目前的作業階段。 命令可以隨時在任何會話中執行。
Cmdlet Get-Help
會取得名稱開頭為 Test 之 Cmdlet 的說明。 在模組中的命令新增至目前的會話之後,您可以使用 Get-Help
和 Get-Command
Cmdlet 來了解匯入的命令。 Cmdlet Test-Files
是從 Server01 計算機導出,並新增至會話。 Cmdlet 會在 Test-Files
匯入命令之電腦上的遠端會話中執行。 PowerShell 會從儲存在 TestCmdlet 模組中的資訊建立會話。
範例 4:目前會話中的導出和 clobber 命令
本範例會將儲存在變數中的命令導出至目前的會話。
Export-PSSession -Session $S -AllowClobber -OutputModule AllCommands
此命令 Export-PSSession
會將變數中 $S
PSSession 的所有命令和所有格式化數據匯出至目前的會話。 AllowClobber 參數包含與目前會話中命令同名的命令。
範例 5:從關閉的 PSSession 導出命令
這個範例示範如何在關閉建立匯出命令的 PSSession 關閉時,以特殊選項執行導出的命令。
如果在匯入模組時關閉原始遠端會話,模組將會使用任何連線到原始計算機的開啟遠端會話。 如果原始計算機沒有目前的會話,模組將會重新建立會話。
若要在遠端會話中執行具有特殊選項的匯出命令,您必須先使用這些選項建立遠端會話,才能匯入模組。 搭配 New-PSSession
SessionOption 參數使用 Cmdlet
$Options = New-PSSessionOption -NoMachineProfile
$S = New-PSSession -ComputerName Server01 -SessionOption $Options
Export-PSSession -Session $S -OutputModule Server01
Remove-PSSession $S
New-PSSession -ComputerName Server01 -SessionOption $Options
Import-Module Server01
Cmdlet New-PSSessionOption
會建立 PSSessionOption 物件,並將物件儲存在變數中 $Options
。 New-PSSession
此命令會在 Server01 計算機上建立 PSSession。
SessionOption 參數會使用儲存在 中的$Options
物件。 會話會儲存在變數中 $S
。
Cmdlet 會將 Export-PSSession
中的 PSSession $S
命令導出至 Server01 模組。
Cmdlet 會 Remove-PSSession
刪除變數中的 $S
PSSession。
Cmdlet New-PSSession
會建立新的 PSSession,以連線到 Server01 計算機。 SessionOption 參數會使用儲存在 中的$Options
物件。 Cmdlet 會 Import-Module
從 Server01 模組匯入命令。 模組中的命令會在 Server01 計算機上的 PSSession 中執行。
參數
-AllowClobber
匯出指定的命令,即使它們的名稱與目前工作階段中的命令相同。
如果您匯出的命令名稱與目前會話中的命令相同,則導出的命令會隱藏或取代原始命令。 如需詳細資訊,請參閱 about_Command_Precedence。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-ArgumentList
匯出因使用指定的引數 (參數值) 而產生之命令的變體。
例如,若要在 憑證中匯出 命令的Get-Item
變體,請在 的 PSSession $S
export-pssession -session $S -command get-item -argumentlist cert:
中,輸入 (Cert:) 磁碟驅動器。
Type: | Object[] |
Aliases: | Args |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Certificate
指定用來簽署格式檔案 (*的客戶端憑證。Format.ps1xml) 或腳本模組檔案, (.psm1) 建立 Export-PSSession
的模組。 輸入包含憑證的變數,或可取得憑證的命令或運算式。
若要尋找憑證,請使用 Get-PfxCertificate
Cmdlet 或使用 Get-ChildItem
憑證 (Cert:) 磁碟驅動器中的 Cmdlet。 若憑證無效或權限不足,則命令會失敗。
Type: | X509Certificate2 |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-CommandName
只匯出具有指定的名稱或名稱模式的命令。 允許使用萬用字元。 使用 CommandName 或其別名 Name。
根據預設,從 PSSession 導出所有命令, Export-PSSession
但與目前會話中命令具有相同名稱的命令除外。 這可防止目前會話中的命令隱藏或取代命令。 若要匯出所有命令,甚至是隱藏或取代其他命令的命令,請使用 AllowClobber 參數。
如果您使用 CommandName 參數,除非您使用 FormatTypeName 參數,否則不會匯出命令的格式檔案。 同樣地,如果您使用 FormatTypeName 參數,除非您使用 CommandName 參數,否則不會匯出任何命令。
Type: | String[] |
Aliases: | Name |
Position: | 2 |
Default value: | All commands in the session. |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | True |
-CommandType
只匯出指定類型的命令物件。 使用 CommandType 或它的別名 Type。
此參數可接受的值如下所示:
- 別名。 目前會話中的所有 PowerShell 別名。
- 全部。 所有命令類型。 它相當於
Get-Command -Name *
。 - 應用程式。 Path 環境變數
$env:path
() 所列路徑中 PowerShell 檔案以外的所有檔案,包括 .txt、.exe 和 .dll 檔案。 - Cmdlet。 目前工作階段中的 Cmdlet。 Cmdlet 是預設值。
- 設定。 PowerShell 設定。 如需詳細資訊,請參閱 about_Session_Configurations。
- ExternalScript。 路徑環境變數中列出的所有 .ps1 檔案 ()
$env:path
。 - 篩選和函式。 所有 PowerShell 函式。
- 指令碼: 目前工作階段中的指令碼區塊。
- 工作流程。 PowerShell 工作流程。 如需詳細資訊,請參閱 about_Workflows。
Type: | CommandTypes |
Aliases: | Type |
Accepted values: | Alias, All, Application, Cmdlet, Configuration, ExternalScript, Filter, Function, Script, Workflow |
Position: | Named |
Default value: | All commands in the session. |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Encoding
指定目標檔案的編碼類型。 預設值為 UTF8。
此參數可接受的值如下所示:
- Ascii 使用 ASCII (7 位) 字元集。
- BigEndianUnicode 使用UTF-16搭配位元組位元組順序。
- 預設 使用對應至系統使用中代碼頁的編碼方式。
- Oem 使用對應至系統目前 OEM 代碼頁的編碼方式。
- Unicode 使用UTF-16搭配位元組位元組順序。
- UTF7 使用UTF-7。
- UTF8 使用UTF-8。
- UTF32 使用UTF-32搭配位元組位元組順序。
Type: | String |
Accepted values: | ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32 |
Position: | Named |
Default value: | UTF8 |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Force
即使一或多個現有的輸出檔案具有唯讀屬性,仍覆寫檔案。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-FormatTypeName
只匯出指定之 Microsoft .NET Framework 類型的格式設定指示。 輸入類型名稱。 根據預設,Export-PSSession
導出所有不在 System.Management.Automation 命名空間中之 .NET Framework 類型的格式化指示。
此參數的值必須是匯入命令所在會話中命令所 Get-FormatData
傳回的類型名稱。 若要取得遠端工作階段中的所有格式化資料, 請輸入 *
。
如果您使用 FormatTypeName 參數,除非您使用 CommandName 參數,否則不會匯出任何命令。
如果您使用 CommandName 參數,除非您使用 FormatTypeName 參數,否則不會匯出命令的格式檔案。
Type: | String[] |
Position: | 3 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-FullyQualifiedModule
以 ModuleSpecification 物件的形式指定名稱的模組。 請參閱 ModuleSpecification 建構函式 (哈希表) 的一節。
例如, FullyQualifiedModule 參數接受以下列其中一種格式指定的模組名稱:
@{ModuleName = "modulename"; ModuleVersion = "version_number"}
@{ModuleName = "modulename"; ModuleVersion = "version_number"; Guid = "GUID"}
ModuleName 和 ModuleVersion 是必要參數,但 Guid 是選擇性參數。 您無法在與 Module 參數相同的命令中指定 FullyQualifiedModule 參數;這兩個參數互斥。
Type: | ModuleSpecification[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Module
僅匯出指定 PowerShell 嵌入式管理單元和模組中的命令。 請輸入嵌入式管理單元和模組名稱。 不允許使用萬用字元。
如需詳細資訊,請參閱 Import-Module
和 about_PSSnapins。
Type: | String[] |
Aliases: | PSSnapin |
Position: | Named |
Default value: | All commands in the session. |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-OutputModule
指定 所 Export-PSSession
建立模組的選擇性路徑和名稱。 預設路徑為 $home\Documents\WindowsPowerShell\Modules
。 此為必要參數。
如果模組子目錄或任何建立的檔案 Export-PSSession
已經存在,命令就會失敗。 若要覆寫現有的檔案,請使用 Force 參數。
Type: | String |
Aliases: | PSPath, ModuleName |
Position: | 1 |
Default value: | $home\Documents\WindowsPowerShell\Modules |
Required: | True |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Session
指定要從中匯出命令的 PSSession。 輸入包含會話物件的變數,或取得會話物件的命令,例如 Get-PSSession
命令。 此為必要參數。
Type: | PSSession |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
None
您無法使用管線會傳送到 Export-PSSession
。
輸出
Export-PSSession
會傳回組成所建立模組的檔案清單。
備註
Export-PSSession
依賴PowerShell遠端基礎結構。 若要使用此 Cmdlet,電腦必須針對遠端功能做設定。 如需詳細資訊,請參閱about_Remote_Requirements。
您無法使用 Export-PSSession
匯出 PowerShell 提供者。
匯出的命令會在從中匯出它們的來源 PSSession 中以隱含的方式執行。 從遠端執行命令的詳細數據會由PowerShell完全處理。 執行匯出之命令的方式與執行本機命令的方式一樣。
Export-ModuleMember
擷取並儲存其匯出模組中 PSSession 的相關信息。 如果您在匯入模組時關閉匯出命令的 PSSession,而且相同計算機沒有作用中的 PSSession,模組中的命令會嘗試重新建立 PSSession。 如果嘗試重新建立 PSSession 失敗,則導出的命令將不會執行。
擷取並儲存在模組中的會話資訊不包含會話選項,例如您在喜好設定變數中指定的$PSSessionOption
會話資訊Export-ModuleMember
,或使用、 Enter-PSSession
或 Invoke-Command
Cmdlet 的 New-PSSession
SessionOption 參數。 在您匯入模組時,如果原始 PSSession 已關閉,模組將會使用另一個連到同一部電腦的 PSSession (如果有的話)。 若要讓匯入的命令在正確設定的工作階段中執行,請在匯入模組之前,先使用您想要的選項來建立 PSSession。
若要尋找要導出的命令, Export-PSSession
請使用 Invoke-Command
Cmdlet 在 PSSession 中執行 Get-Command
命令。 若要取得及儲存命令的格式數據,它會使用 Get-FormatData
和 Export-FormatData
Cmdlet。 當您執行Export-PSSession
命令時,可能會看到 、 Get-FormatData
Get-Command
和 Export-FormatData
的錯誤訊息Invoke-Command
。 此外,Export-PSSession
也無法從不包含 、 Get-FormatData
Select-Object
和 Get-Help
Cmdlet 的Get-Command
工作階段匯出命令。
Export-PSSession
Write-Progress
會使用 Cmdlet 來顯示命令的進度。 當命令執行時,您可以看到進度列。
匯出的命令與其他遠端命令具有相同的限制,包括無法以使用者介面啟動程式,例如「記事本」。
因為 PowerShell 設定檔不會在 PSSessions 中執行,所以設定檔新增至工作階段的命令不適用於 Export-PSSession
。 若要從配置檔匯出命令,請在匯出命令之前,使用 Invoke-Command
命令在 PSSession 中手動執行設定檔。
建立的 Export-PSSession
模組可能包含格式化檔案,即使命令未匯入格式化數據也一樣。 如果命令不會匯入格式設定資料,所建立的任何格式檔案都將不會包含格式設定資料。