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 會話取得 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 命令

此範例會從伺服器匯出所有 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

這些命令會將 和 Set 命令從遠端電腦上的Microsoft Exchange Server嵌入式管理單元匯出 Get 至本機電腦上的目錄中的 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,從 中的 $S PSSession 匯出至本機電腦上的 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 關閉時,以特殊選項執行匯出的命令。

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

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

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

此參數可接受的值如下所示:

  • ascii:使用 ASCII (7 位) 字元集的編碼方式。
  • bigendianunicode:使用大位元組位元組順序以 UTF-16 格式編碼。
  • oem:使用 MS-DOS 和主控台程式的預設編碼方式。
  • unicode:使用位元組由小到大的順序,以 UTF-16 格式編碼。
  • utf7:以 UTF-7 格式編碼。
  • utf8:以 UTF-8 格式編碼。
  • utf8BOM:使用位元組順序標記 (BOM) 以 UTF-8 格式編碼
  • utf8NoBOM:以 UTF-8 格式編碼,不含位元組順序標記 (BOM)
  • utf32:以 UTF-32 格式編碼。

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

Type:Encoding
Accepted values:ASCII, BigEndianUnicode, 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

ModuleSpecification 物件的形式指定名稱的模組。 請參閱 ModuleSpecification 建構函式 (雜湊表) 的一節。

例如, FullyQualifiedModule 參數接受以下列其中一種格式指定的模組名稱:

  • @{ModuleName = "modulename"; ModuleVersion = "version_number"}
  • @{ModuleName = "modulename"; ModuleVersion = "version_number"; Guid = "GUID"}

ModuleNameModuleVersion 是必要參數,但 Guid 是選擇性參數。 您無法在與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

您無法使用管線將 物件傳送至 Export-PSSession

輸出

FileInfo

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-PSSessionInvoke-Command Cmdlet 的 New-PSSessionSessionOption參數。 在您匯入模組時,如果原始 PSSession 已關閉,模組將會使用另一個連到同一部電腦的 PSSession (如果有的話)。 若要讓匯入的命令在正確設定的工作階段中執行,請在匯入模組之前,先使用您想要的選項來建立 PSSession。

若要尋找要匯出的命令, Export-PSSession 請使用 Invoke-Command Cmdlet 在 PSSession 中執行 Get-Command 命令。 若要取得及儲存命令的格式資料,它會使用 Get-FormatDataExport-FormatData Cmdlet。 當您執行 Export-PSSession 命令時,可能會看到 、 Get-FormatDataGet-CommandExport-FormatData 的錯誤訊息 Invoke-Command 。 此外, Export-PSSession 也無法從不包含 、 Get-FormatDataSelect-ObjectGet-Help Cmdlet 的 Get-Command 會話匯出命令。

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

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

因為 PowerShell 設定檔不會在 PSSessions 中執行,所以設定檔新增至會話的命令不適用於 Export-PSSession 。 若要從設定檔匯出命令,請在匯出命令之前,使用 Invoke-Command 命令在 PSSession 中手動執行設定檔。

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