Export-PSSession

從另一個會話導出命令,並將其儲存在 PowerShell 模組中。

Syntax

Export-PSSession
      [-OutputModule] <String>
      [-Force]
      [-Encoding <Encoding>]
      [[-CommandName] <String[]>]
      [-AllowClobber]
      [-ArgumentList <Object[]>]
      [-CommandType <CommandTypes>]
      [-Module <String[]>]
      [-FullyQualifiedModule <ModuleSpecification[]>]
      [[-FormatTypeName] <String[]>]
      [-Certificate <X509Certificate2>]
      [-Session] <PSSession>
      [<CommonParameters>]

Description

Cmdlet 會 Export-PSSession 從本機或遠端電腦上的另一個 PowerShell 會話 (PSSession) 取得 Cmdlet、函式、別名和其他命令類型,並將其儲存在 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 命令

此範例會從伺服器匯出所有 GetSet 命令。

$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 嵌入式管理單元導出GetSet至本機電腦上的目錄中的 Exchange 模組$PSHOME\Modules。 將模組放在 $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,從中的 PSSession $S 匯出至本機電腦上的 TestCmdlets 模組。

Cmdlet Remove-PSSession 會從目前的會話中刪除 中的 $S PSSession。 此命令顯示 PSSession 不需要使用從工作階段匯入的命令。 Cmdlet 會將 Import-Module TestCmdlet 模組中的 Cmdlet 新增至目前的作業階段。 命令可以隨時在任何會話中執行。

Cmdlet Get-Help 會取得名稱開頭為 Test 之 Cmdlet 的說明。 將模組中的命令新增至目前的會話之後,您可以使用 Get-HelpGet-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 關閉時,以特殊選項執行導出的命令。

如果匯入模組時關閉原始遠端會話,模組將會使用任何連線到原始計算機的開啟遠端會話。 如果原始計算機沒有目前的會話,模組將會重新建立會話。

若要在遠端會話中執行具有特殊選項的匯出命令,您必須先使用這些選項建立遠端會話,才能匯入模組。 搭配 SessionOption 參數使用 New-PSSession 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 命令從 中的 $S PSSession 匯出至 Server01 模組。 Cmdlet Remove-PSSession 會刪除變數中的 $S PSSession。

Cmdlet New-PSSession 會建立連線到 Server01 計算機的新 PSSession。 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

匯出使用指定自變數所產生之命令的變數(參數值)。

例如,若要在的 PSSession $S中匯出憑證 (Cert:) 磁碟驅動器中的 命令變體Get-Item,請輸入 Export-PSSession -Session $S -Command Get-Item -ArgumentList 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 Certificate (Cert:) 磁碟驅動器中的 Cmdlet。 如果憑證無效或沒有足夠的授權單位,則命令會失敗。

Type:X509Certificate2
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CommandName

僅匯出具有指定名稱或名稱模式的命令。 允許通配符。 使用 CommandName 或其別名 Name

根據預設, Export-PSSession 會從 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 或其別名類型

此參數可接受的值如下:

  • Alias:目前會話中的所有PowerShell別名。
  • All:所有命令類型。 它相當於 Get-Command -Name *
  • Application:P ath環境變數中$env:path所列路徑中 PowerShell 檔案以外的所有檔案,包括.txt、.exe和.dll檔案。
  • Cmdlet:目前會話中的 Cmdlet。 Cmdlet 是預設值。
  • Configuration:P owerShell 組態。 如需詳細資訊,請參閱 about_Session_Configurations
  • ExternalScript:P ath 環境變數 ($env:path) 所列路徑中的所有 .ps1 檔案。
  • FilterFunction:所有 PowerShell 函式。
  • Script 目前會話中的腳本區塊。
  • Workflow PowerShell 工作流程。 如需詳細資訊,請參閱 about_Workflows

這些值會定義為旗標型列舉。 您可以將多個值結合在一起,以使用此參數來設定多個旗標。 這些值可以傳遞至 CommandType 參數做為值的陣列,或是這些值的逗號分隔字串。 Cmdlet 會使用二進位 OR 作業來合併值。 將值當做數位傳遞是最簡單的選項,也可讓您在值上使用 Tab 鍵自動完成。

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

指定目標檔案的編碼類型。 預設值是 utf8NoBOM

此參數可接受的值如下:

  • ascii:使用 ASCII (7 位) 字元集的編碼方式。
  • ansi:針對目前文化特性的 ANSI 代碼頁,使用 的編碼方式。 此選項已在PowerShell 7.4中新增。
  • bigendianunicode:使用 big-endian 位元組順序以 UTF-16 格式編碼。
  • bigendianutf32:使用 big-endian 位元組順序以 UTF-32 格式編碼。
  • oem:使用 MS-DOS 和控制台程式的預設編碼方式。
  • unicode:使用位元組順序以UTF-16格式編碼。
  • utf7:以 UTF-7 格式編碼。
  • utf8:以 UTF-8 格式編碼。
  • utf8BOM:使用位元節順序標記以 UTF-8 格式編碼 (BOM)
  • utf8NoBOM:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)
  • utf32:以 UTF-32 格式編碼。

從 PowerShell 6.2 開始,Encoding 參數也允許已註冊代碼頁的數值識別元(例如 -Encoding 1251)或已註冊代碼頁的字串名稱(例如 )。-Encoding "windows-1251" 如需詳細資訊,請參閱 Encoding.CodePage.NET 檔。

從 PowerShell 7.4 開始,您可以使用 Ansi Encoding 參數的值,傳遞目前文化特性 ANSI 代碼頁的數值標識碼,而不需要手動指定它。

注意

不再建議使用UTF-7* 。 自 PowerShell 7.1 起,如果您為 Encoding 參數指定utf7,則會撰寫警告。

Type:Encoding
Accepted values:ASCII, BigEndianUnicode, BigEndianUTF32, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32
Position:Named
Default value:UTF8NoBOM
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

此值可以是模組名稱、完整模組規格或模組檔案的路徑。

當值為路徑時,路徑可以是完整或相對路徑。 相對於包含 using 語句的腳本,會解析相對路徑。

當值是名稱或模組規格時,PowerShell 會 搜尋 PSModulePath 中的指定模組。

模組規格是具有下列索引鍵的哈希表。

  • ModuleName - 必要 指定模組名稱。
  • GUID - 選擇性 指定模組的 GUID。
  • 也必須指定下列三個索引鍵中的至少一個。
    • ModuleVersion - 指定模組的最低可接受的版本。
    • MaximumVersion - 指定模組的最大可接受的版本。
    • RequiredVersion - 指定模組的確切必要版本。 這無法與其他版本金鑰搭配使用。

您無法在與 Module 參數相同的命令中指定 FullyQualifiedModule 參數。 這兩個參數互斥。

Type:ModuleSpecification[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Module

僅匯出指定 PowerShell 嵌入式管理單元和模組中的命令。 輸入嵌入式管理單元和模組名稱。 不允許通配符。

如需詳細資訊,請參閱 Import-Moduleabout_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

您無法使用管線將物件傳送至此 Cmdlet。

輸出

FileInfo

此 Cmdlet 會傳回組成所建立模組的檔案清單。

備註

Export-PSSession 依賴PowerShell遠端基礎結構。 若要使用此 Cmdlet,計算機必須設定為遠端處理。 如需詳細資訊,請參閱 about_Remote_Requirements

您無法使用 Export-PSSession 匯出 PowerShell 提供者。

導出的命令會在導出的 PSSession 中隱含執行。 PowerShell 會完全處理遠端執行命令的詳細數據。 您可以執行導出的命令,就像執行本機命令一樣。

Export-ModuleMember 擷取並儲存其匯出模組中 PSSession 的相關信息。 如果您匯入模組時關閉匯出命令的 PSSession,而且同一部計算機沒有作用中的 PSSession,則模組中的命令會嘗試重新建立 PSSession。 如果嘗試重新建立 PSSession 失敗,則導出的命令將不會執行。

擷取並儲存在模組中的會話資訊不包含會話選項,例如您在$PSSessionOption喜好設定變數中指定的會話資訊Export-ModuleMember,或使用Enter-PSSessionInvoke-Command Cmdlet 的 New-PSSessionSessionOption 參數。 如果您在匯入模組時關閉原始 PSSession,如果有另一部 PSSession 可供使用,該模組將會使用另一部 PSSession 至同一部計算機。 若要讓匯入的命令在正確設定的會話中執行,請使用您想要的選項建立 PSSession,再匯入模組。

若要尋找要導出的命令, Export-PSSession 請使用 Invoke-Command Cmdlet 在 PSSession 中執行 Get-Command 命令。 若要取得及儲存命令的格式數據,它會使用 Get-FormatDataExport-FormatData Cmdlet。 當您執行Export-PSSession命令時,您可能會看到 、Get-CommandInvoke-CommandGet-FormatDataExport-FormatData 的錯誤訊息。 此外, Export-PSSession 無法從不包含 Get-CommandGet-FormatDataSelect-ObjectGet-Help Cmdlet 的工作階段匯出命令。

Export-PSSession 會使用 Write-Progress Cmdlet 來顯示命令的進度。 當命令執行時,您可能會看到進度列。

導出的命令與其他遠端命令具有相同的限制,包括無法使用使用者介面啟動程式,例如 記事本。

由於 PowerShell 設定檔不會在 PSSessions 中執行,因此設定檔新增至工作階段的命令不適用於 Export-PSSession。 若要從配置檔匯出命令,請在匯出命令之前,先使用 Invoke-Command 命令在 PSSession 中手動執行配置檔。

建立的模組 Export-PSSession 可能包含格式化檔案,即使命令未匯入格式化數據也一樣。 如果命令未匯入格式化數據,所建立的任何格式化檔案都不會包含格式化數據。