Export-PSSession
從另一個會話導出命令,並將其儲存在 PowerShell 模組中。
語法
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
Export-PSSession
Cmdlet 會從本機或遠端電腦上的另一個 PowerShell 會話 (PSSession) 取得 Cmdlet、函式、別名和其他命令類型,並將其儲存在 PowerShell 模組中。 若要將模組中的命令新增至目前的會話,請使用 Import-Module
Cmdlet。
不同於 Import-PSSession
,它會將命令從另一個 PSSession 匯入目前的工作階段,Export-PSSession
將命令儲存在模組中。 命令不會匯入目前的會話。
若要匯出命令,請使用 New-PSSession
Cmdlet 來建立 PSSession,其中包含您想要導出的命令。 然後使用 Export-PSSession
Cmdlet 導出命令。
若要防止命令名稱衝突,Export-PSSession
的預設值是匯出所有命令,但目前會話中存在的命令除外。 您可以使用 CommandName 參數來指定要匯出的命令。
Export-PSSession
Cmdlet 會使用 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 https://exchange.microsoft.com/mailbox -Credential exchangeadmin01@hotmail.com -Authentication Negotiate
Export-PSSession -Session $S -Module exch* -CommandName Get-*, Set-* -FormatTypeName * -OutputModule $PSHOME\Modules\Exchange -Encoding ASCII
這些命令會將遠端電腦上的 Microsoft Exchange Server 嵌入式管理單元 Get
和 Set
命令導出至本機電腦上的 $PSHOME\Modules
目錄中的 Exchange 模組。
將模組放在 $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 模組。
Remove-PSSession
Cmdlet 會從目前的工作階段中刪除 $S
中的 PSSession。 此命令顯示 PSSession 不需要使用從工作階段匯入的命令。
Import-Module
Cmdlet 會將 TestCmdlet 模組中的 Cmdlet 新增至目前的會話。 命令可以隨時在任何會話中執行。
Get-Help
Cmdlet 會取得名稱開頭為 Test 之 Cmdlet 的說明。 將模組中的命令新增至目前的會話之後,您可以使用 Get-Help
和 Get-Command
Cmdlet 來了解匯入的命令。
Test-Files
Cmdlet 是從 Server01 計算機匯出並新增至工作階段。
Test-Files
Cmdlet 會在匯入命令的電腦遠端會話中執行。 PowerShell 會從儲存在 TestCmdlet 模組中的資訊建立會話。
範例 4:目前會話中的導出和 clobber 命令
此範例會將儲存在變數中的命令導出至目前的會話。
Export-PSSession -Session $S -AllowClobber -OutputModule AllCommands
這個 Export-PSSession
命令會將 $S
變數中 PSSession 的所有命令和所有格式化數據匯出至目前的會話。
AllowClobber 參數包含與目前工作階段中命令名稱相同的命令。
範例 5:從關閉的 PSSession 導出命令
此範例示範如何在關閉建立匯出命令的 PSSession 關閉時,以特殊選項執行導出的命令。
如果匯入模組時關閉原始遠端會話,模組將會使用任何連線到原始計算機的開啟遠端會話。 如果原始計算機沒有目前的會話,模組將會重新建立會話。
若要在遠端會話中執行具有特殊選項的匯出命令,您必須先使用這些選項建立遠端會話,才能匯入模組。 使用 New-PSSession
Cmdlet 搭配 SessionOption 參數
$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
New-PSSessionOption
Cmdlet 會建立 PSSessionOption 物件,並將物件儲存在 $Options
變數中。
New-PSSession
命令會在 Server01 計算機上建立 PSSession。
SessionOption 參數會使用儲存在 $Options
中的物件。 會話會儲存在 $S
變數中。
Export-PSSession
Cmdlet 會將命令從 $S
中的 PSSession 匯出至 Server01 模組。
Remove-PSSession
Cmdlet 會刪除 $S
變數中的 PSSession。
New-PSSession
Cmdlet 會建立連線到 Server01 電腦的新 PSSession。
SessionOption 參數會使用儲存在 $Options
中的物件。
Import-Module
Cmdlet 會從 Server01 模組匯入命令。 模組中的命令會在 Server01 計算機上的 PSSession 中執行。
參數
-AllowClobber
匯出指定的命令,即使它們的名稱與目前會話中的命令相同。
如果您匯出的命令名稱與目前會話中的命令相同,則導出的命令會隱藏或取代原始命令。 如需詳細資訊,請參閱 about_Command_Precedence。
類型: | SwitchParameter |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-ArgumentList
匯出使用指定自變數所產生之命令的變數(參數值)。
例如,若要在 $S
的 PSSession 的憑證 (Cert:) 磁碟驅動器中匯出 Get-Item
命令的變體,請輸入 Export-PSSession -Session $S -Command Get-Item -ArgumentList cert:
。
類型: | Object[] |
別名: | Args |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Certificate
指定用來簽署格式檔案的客戶端憑證 (*。Export-PSSession
建立的模組中的 Format.ps1xml) 或腳本模組檔案 (.psm1)。 輸入包含憑證的變數,或取得憑證的命令或表達式。
若要尋找憑證,請使用 Get-PfxCertificate
Cmdlet,或使用憑證 (Cert:) 磁碟驅動器中的 Get-ChildItem
Cmdlet。 如果憑證無效或沒有足夠的授權單位,則命令會失敗。
類型: | X509Certificate2 |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-CommandName
僅匯出具有指定名稱或名稱模式的命令。 允許通配符。 使用 CommandName 或其別名,Name。
根據預設,Export-PSSession
會從 PSSession 匯出所有命令,但與目前會話中命令名稱相同的命令除外。 這可防止目前會話中的命令隱藏或取代命令。 若要匯出所有命令,即使是隱藏或取代其他命令的命令,請使用allowClobber 參數。
如果您使用 CommandName 參數,除非您使用 FormatTypeName 參數,否則不會匯出命令的格式設定檔案。 同樣地,如果您使用 FormatTypeName 參數,除非您使用 CommandName 參數,否則不會導出任何命令。
類型: | String[] |
別名: | Name |
Position: | 2 |
預設值: | All commands in the session. |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-CommandType
只匯出指定的命令物件類型。 使用 CommandType 或其別名,Type。
此參數可接受的值如下:
-
Alias
:目前會話中的所有PowerShell別名。 -
All
:所有命令類型。 它相當於Get-Command -Name *
。 -
Application
:Path 環境變數 ($env:path
) 所列路徑中 PowerShell 檔案以外的所有檔案,包括 .txt、.exe和 .dll 檔案。 -
Cmdlet
:目前會話中的 Cmdlet。 Cmdlet 是預設值。 -
Configuration
:PowerShell 組態。 如需詳細資訊,請參閱 about_Session_Configurations。 -
ExternalScript
:Path 環境變數中所列路徑中的所有 .ps1 檔案($env:path
)。 -
Filter
和Function
:所有 PowerShell 函式。 -
Script
目前會話中的腳本區塊。 -
Workflow
PowerShell 工作流程。 如需詳細資訊,請參閱 about_Workflows。
這些值會定義為旗標型列舉。 您可以將多個值結合在一起,以使用此參數來設定多個旗標。 這些值可以傳遞至 CommandType 參數做為值的陣列,或做為這些值的逗號分隔字串。 Cmdlet 會使用二進位 OR 作業來合併值。 將值當做數位傳遞是最簡單的選項,也可讓您在值上使用 Tab 鍵自動完成。
類型: | CommandTypes |
別名: | Type |
接受的值: | Alias, All, Application, Cmdlet, Configuration, ExternalScript, Filter, Function, Script, Workflow |
Position: | Named |
預設值: | All commands in the session. |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Encoding
指定目標檔案的編碼類型。 預設值為 UTF8
。
此參數可接受的值如下:
-
ASCII
:使用 ASCII (7 位) 字元集。 -
BigEndianUnicode
:使用UTF-16搭配 big-endian 位元組順序。 -
Default
;使用對應至系統使用中代碼頁的編碼方式。 -
OEM
:使用對應至系統目前 OEM 代碼頁的編碼方式。 -
Unicode
:使用UTF-16搭配位元組順序。 -
UTF7
:使用UTF-7。 -
UTF8
:使用UTF-8。 -
UTF32
:使用UTF-32搭配位元組順序。
類型: | String |
接受的值: | ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32 |
Position: | Named |
預設值: | UTF8 |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Force
覆寫一或多個現有的輸出檔案,即使檔案具有只讀屬性也一樣。
類型: | SwitchParameter |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-FormatTypeName
僅針對指定的 Microsoft .NET Framework 類型導出格式設定指示。 輸入類型名稱。 根據預設,Export-PSSession
匯出所有不在 system.Management.Automation 命名空間 .NET Framework 類型的格式化指示。
此參數的值必須是匯入命令之會話中 Get-FormatData
命令所傳回的類型名稱。 若要取得遠端工作階段中的所有格式化資料,請輸入 *
。
如果您使用 FormatTypeName 參數,除非您使用 CommandName 參數,否則不會導出任何命令。
如果您使用 CommandName 參數,除非您使用 FormatTypeName 參數,否則不會匯出命令的格式設定檔案。
類型: | String[] |
Position: | 3 |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-FullyQualifiedModule
此值可以是模組名稱、完整模組規格或模組檔案的路徑。
當值為路徑時,路徑可以是完整或相對路徑。 相對於包含 using 語句的腳本,會解析相對路徑。
當值是名稱或模組規格時,PowerShell 會搜尋指定模組 PSModulePath。
模組規格是具有下列索引鍵的哈希表。
-
ModuleName
- 必要 指定模組名稱。 -
GUID
- 選擇性 指定模組的 GUID。 - 它也 必要 至少指定下列三個索引鍵的其中一個。
-
ModuleVersion
- 指定模組的最低可接受的版本。 -
MaximumVersion
- 指定模組的最大可接受的版本。 -
RequiredVersion
- 指定模組的確切必要版本。 這無法與其他版本金鑰搭配使用。
-
您無法在與 Module 參數相同的命令中指定 FullyQualifiedModule 參數。 這兩個參數互斥。
類型: | ModuleSpecification[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Module
僅匯出指定 PowerShell 嵌入式管理單元和模組中的命令。 輸入嵌入式管理單元和模組名稱。 不允許通配符。
如需詳細資訊,請參閱 Import-Module
和 about_PSSnapins。
類型: | String[] |
別名: | PSSnapin |
Position: | Named |
預設值: | All commands in the session. |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-OutputModule
指定 Export-PSSession
所建立之模組的選擇性路徑和名稱。 預設路徑為 $HOME\Documents\WindowsPowerShell\Modules
。 這是必要參數。
如果模組子目錄或任何 Export-PSSession
建立的檔案已經存在,命令就會失敗。 若要覆寫現有的檔案,請使用 Force 參數。
類型: | String |
別名: | PSPath, ModuleName |
Position: | 1 |
預設值: | $HOME\Documents\WindowsPowerShell\Modules |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
-Session
指定從中匯出命令的 PSSession。 輸入變數,其中包含會話物件或取得會話物件的命令,例如 Get-PSSession
命令。 這是必要參數。
類型: | PSSession |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
None
您無法使用管線將物件傳送至此 Cmdlet。
輸出
此 Cmdlet 會傳回組成所建立模組的檔案清單。
備註
Windows PowerShell 包含下列 Export-PSSession
別名:
epsn
Export-PSSession
依賴 PowerShell 遠端基礎結構。 若要使用此 Cmdlet,計算機必須設定為遠端處理。 如需詳細資訊,請參閱 about_Remote_Requirements。
您無法使用 Export-PSSession
匯出 PowerShell 提供者。
導出的命令會在導出的 PSSession 中隱含執行。 PowerShell 會完全處理遠端執行命令的詳細數據。 您可以執行導出的命令,就像執行本機命令一樣。
Export-ModuleMember
擷取並儲存其匯出模組中 PSSession 的相關信息。 如果您匯入模組時關閉匯出命令的 PSSession,而且同一部計算機沒有作用中的 PSSession,則模組中的命令會嘗試重新建立 PSSession。 如果嘗試重新建立 PSSession 失敗,則導出的命令將不會執行。
Export-ModuleMember
擷取並儲存在模組中的會話資訊不包含會話選項,例如您在 $PSSessionOption
喜好設定變數中指定的會話資訊,或使用 New-PSSession
、Enter-PSSession
或 Invoke-Command
Cmdlet 的 SessionOption 參數。 如果您在匯入模組時關閉原始 PSSession,如果有另一部 PSSession 可供使用,該模組將會使用另一部 PSSession 至同一部計算機。 若要讓匯入的命令在正確設定的會話中執行,請使用您想要的選項建立 PSSession,再匯入模組。
若要尋找要導出的命令,Export-PSSession
會使用 Invoke-Command
Cmdlet 在 PSSession 中執行 Get-Command
命令。 若要取得及儲存命令的格式設定數據,它會使用 Get-FormatData
和 Export-FormatData
Cmdlet。 當您執行 Export-PSSession
命令時,您可能會看到來自 Invoke-Command
、Get-Command
、Get-FormatData
和 Export-FormatData
的錯誤訊息。 此外,Export-PSSession
無法從不包含 Get-Command
、Get-FormatData
、Select-Object
和 Get-Help
Cmdlet 的會話導出命令。
Export-PSSession
會使用 Write-Progress
Cmdlet 來顯示命令的進度。 當命令執行時,您可能會看到進度列。
導出的命令與其他遠端命令具有相同的限制,包括無法使用使用者介面啟動程式,例如記事本。
由於 PowerShell 配置檔不會在 PSSessions 中執行,因此設定檔新增至會話的命令無法 Export-PSSession
。 若要從配置檔匯出命令,請使用 Invoke-Command
命令,在匯出命令之前手動在 PSSession 中執行配置檔。
Export-PSSession
建立的模組可能包含格式化檔案,即使命令未匯入格式化數據也一樣。 如果命令未匯入格式化數據,所建立的任何格式化檔案都不會包含格式化數據。