將另一個工作階段的命令匯入目前的工作階段。

語法

Import-PSSession [-Session] <PSSession> [[-CommandName] <string[]>] [[-FormatTypeName] <string[]>] [-AllowClobber] [-ArgumentList <Object[]>] [-CommandType {<Alias> | <Function> | <Filter> | <Cmdlet> | <ExternalScript> | <Application> | <Script> | <All>}] [-Module <string[]>] [-Prefix <string>] [<CommonParameters>]

描述

Import-PSSession Cmdlet 會從本機或遠端電腦上的 PSSession 將 Cmdlet、函數和別名等命令匯入目前的工作階段。您可以匯入 Get-Command 能在 PSSession 中找到的任何命令。

Import-PSSession 命令可用來從自訂殼層 (如 Microsoft Exchange Server 殼層) 或是從工作階段來匯入命令,該工作階段包含了 Windows PowerShell 模組和嵌入式管理單元或者不在目前工作階段的其他元素。

若要匯入命令,請先使用 New-PSSession Cmdlet 建立 PSSession。然後,使用 Import-PSSession Cmdlet 匯入命令。根據預設,Import-PSSession 會匯入所有命令,但與目前工作階段中的命令同名的命令除外。若要匯入所有命令,請使用 AllowClobber 參數。

您可以像在工作階段中使用任何命令一樣使用匯入的命令。當您使用匯入的命令時,命令的匯入部分會以隱含方式在其匯入來源的工作階段中執行,而遠端操作則完全由 Windows PowerShell 處理。您甚至不需要知道這些遠端操作,但是必須讓與其他工作階段 (PSSession) 的連線保持在開啟狀態。如果關閉連線,匯入的命令就不能再使用。

因為匯入的命令可能要花比本機命令還要久的執行時間,Import-PSSession 會在每個匯入的命令中新增 AsJob 參數。此參數可讓您以 Windows PowerShell 背景工作的方式執行命令。如需詳細資訊,請參閱 about_Jobs。

當您使用 Import-PSSession 時,Windows PowerShell 會將匯入的命令加入至只存在於工作階段內的暫存模組,並傳回代表該模組的物件。若要建立您可以在未來工作階段使用的持續模組,請使用 Export-PSSession Cmdlet。

Import-PSSession Cmdlet 會使用 Windows PowerShell 的隱含遠端功能。當您將命令匯入目前的工作階段時,這些命令就會以隱含方式執行於原始工作階段,或是執行於原始電腦的類似工作階段。

參數

-AllowClobber

匯入指定的命令,即使這些命令與目前工作階段中的命令同名亦然。

如果您匯入與目前工作階段中的命令同名的命令,則匯入的命令會隱藏或取代原始命令。如需詳細資訊,請參閱 about_Command_Precedence。

根據預設,Import-PSSession 不會匯入與目前工作階段中的命令同名的命令。

必要?

false

位置?

named

預設值

False

接受管線輸入?

false

接受萬用字元?

false

-ArgumentList <Object[]>

匯入因使用指定的引數 (參數值) 所產生的命令變化。

例如,若要從 $s 的 PSSession 匯入憑證 (Cert:) 磁碟機中 Get-Item 命令的變化,請輸入 "import-pssession -session $s -command Get-Item -argumentlist cert:".

必要?

false

位置?

named

預設值

All command in the PSSession, except for commands with the same names as commands in the current session.

接受管線輸入?

false

接受萬用字元?

false

-CommandName <string[]>

只匯入有指定的名稱或名稱模式的命令。允許使用萬用字元。請使用 "CommandName" 或其別名 "Name"。

根據預設,Import-PSSession 會從工作階段匯入所有命令,但與目前工作階段中的命令同名的命令則除外。這樣可防止匯入的命令隱藏或取代工作階段中的命令。若要匯入所有命令,即使是隱藏或取代其他命令的那些命令,請使用 AllowClobber 參數。

如果您使用 CommandName 參數,則除非您使用 FormatTypeName 參數,否則不會匯入命令的格式化檔案。同樣地,如果您使用 FormatTypeName 參數,則除非您使用 CommandName 參數,否則不會匯入命令。

必要?

false

位置?

3

預設值

All commands in the PSSession, except for commands with the same names as commands in the current session.

接受管線輸入?

false

接受萬用字元?

true

-CommandType <CommandTypes>

只匯入指定類型的命令物件。預設值為 Cmdlet。請使用 "CommandType" 或其別名 "Type"。

有效的值包括:

-- Alias:遠端工作階段中的 Windows PowerShell 別名。

-- All:遠端工作階段中的 Cmdlet 和函數。

-- Application:Path 環境變數 ($env:path) 所列出之路徑中所有非 Windows PowerShell 的檔案,包括 .txt、.exe 和 .dll 檔案。

-- Cmdlet:遠端工作階段中的 Cmdlet。預設為 "Cmdlet"。

-- ExternalScript:遠端工作階段中 Path 環境變數 ($env:path) 所列出之路徑中的 .ps1 檔案。

-- Filter 和 Function:遠端工作階段中的 Windows PowerShell 函數。

-- Script:遠端工作階段中的指令碼區塊。

必要?

false

位置?

named

預設值

All command in the PSSession, except for commands with the same names as commands in the current session.

接受管線輸入?

false

接受萬用字元?

false

-FormatTypeName <string[]>

匯入指定之 Microsoft .NET Framework 型別的格式化指示。請輸入型別名稱。允許使用萬用字元。

這個參數的值必須是匯入命令之來源工作階段中的 Get-FormatData 命令所傳回的型別名稱。若要取得遠端工作階段中的所有格式化資料,請輸入 *。

如果此命令不包含 CommandName 或 FormatTypeName 參數,Import-PSSession

會匯入遠端工作階段中 Get-FormatData 命令所傳回之所有 .NET Framework 型別的格式化指示。

如果您使用 FormatTypeName 參數,則除非您使用 CommandName 參數,否則不會匯入命令。

同樣地,如果您使用 CommandName 參數,則除非您使用 FormatTypeName 參數,否則不會匯入命令的格式化檔案。

必要?

false

位置?

4

預設值

Types in the System.Management.Automation namespace

接受管線輸入?

false

接受萬用字元?

true

-Module <string[]>

只匯入指定的 Windows PowerShell 嵌入式管理單元和模組中的命令。請輸入嵌入式管理單元和模組的名稱。不允許使用萬用字元。

如需詳細資訊,請參閱 about_Pssnapins 和 Import-Module。

必要?

false

位置?

named

預設值

All command in the PSSession, except for commands with the same names as commands in the current session.

接受管線輸入?

false

接受萬用字元?

false

-Prefix <string>

將指定的首碼加入至已匯入之命令名稱中的名詞。

請使用此參數避免名稱衝突,當工作階段中不同命令具有相同名稱時,便可能發生此衝突。

例如,如果您指定首碼 "Remote",然後匯入 Get-Date Cmdlet,工作階段會將此 Cmdlet 識別為 Get-RemoteDate,以免與原始的 Get-Date Cmdlet 混淆。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-Session <PSSession>

指定匯入 Cmdlet 的來源 PSSession。請輸入包含工作階段物件的變數或可取得工作階段物件的命令,例如 New-PSSessionGet-PSSession 命令。您只能指定一個工作階段。此參數為必要項。

必要?

true

位置?

1

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

您無法經由管道將物件輸出至這個 Cmdlet。

輸出

System.Management.Automation.PSModuleInfo

Import-PSSession 會傳回 New-Module 和 Get-Module 傳回的相同模組物件。但是傳回的模組是暫存的,而且只存在於目前的工作階段中。若要在磁碟上建立永久的模組,請使用 Export-PSSession Cmdlet。

附註

Import-PSSession 依存於 Windows PowerShell 遠端基礎結構。若要使用此 Cmdlet,電腦必須有設定 WS-Management 遠端功能。如需詳細資訊,請參閱 about_Remote 和 about_Remote_Requirements。

您不能使用 Import-PSSession 匯入變數或 Windows PowerShell 提供者。

當您匯入的命令與目前工作階段的命令同名時,匯入的命令可以隱藏工作階段中的別名、函數和 Cmdlet,而且可以取代工作階段中的函數和變數。如需詳細資訊,請參閱 about_Command_Precedence。

Import-PSSession 會將所有命令轉換成函數,然後再加以匯入。因此,匯入之命令的行為會與它們保留其原始命令類型時的行為有些不同。例如,如果您從 PSSession 匯入 Cmdlet,然後從模組或嵌入式管理單元匯入同名的 Cmdlet,預設一定會執行從 PSSession 匯入的 Cmdlet,因為函數優先於 Cmdlet。相反地,如果您將別名匯入具有同名之別名的工作階段中,一定會使用原始別名,因為別名優先於函數。如需詳細資訊,請參閱 about_Command_Precedence。

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

為了尋找要匯入的命令,Import-PSSession 會使用 Invoke-Command Cmdlet 在 PSSession 中執行 Get-Command 命令。為了取得命令的格式化資料,它會使用 Get-FormatData Cmdlet。當您執行 Import-PSSession 命令時,您可能會看到來自 Invoke-Command、Get-Command 和 Get-FormatData 的錯誤訊息。此外,Import-PSSession 無法從未包含 Get-Command、Get-FormatData、Select-Object 和 Get-Help Cmdlet 的 PSSession 中匯入命令。

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

因為 Windows PowerShell 設定檔無法在 PSSessions 中執行,設定檔新增到工作階段中的命令將不會提供給 Import-PSSession。若要從設定檔匯入命令,請在匯入命令之前使用 Invoke-Command 命令,以手動方式在 PSSession 中執行設定檔。

Import-PSSession 所建立的暫時性模組可能包含格式化檔案,即使此命令未匯入格式化資料亦然。如果此命令未匯入格式化資料,所有建立的格式化檔案都不會包含格式化資料。

若要使用 Import-PSSession,目前工作階段中的執行原則不得為 Restricted 或 AllSigned,因為 Import-PSSession 所建立的模組包含這些原則禁止使用的未經簽署指令碼檔案。若要使用 Import-PSSession 而不變更本機電腦的執行原則,請使用 Set-ExecutionPolicy 的 Scope 參數,針對單一處理序設定限制較少的執行原則。

範例 1

C:\PS>$s = new-pssession -computername Server01

C:\PS> import-pssession -session $s

描述
-----------
這個命令會從 Server01 電腦上的 PSSession 將所有命令匯入目前的工作階段,但與目前工作階段中的命令同名的命令則除外。

因為這個命令不使用 CommandName 參數,所以它也會匯入匯入的命令所需的所有格式化資料。






範例 2

C:\PS>$s = new-pssession https://ps.testlabs.com/powershell

C:\PS> import-pssession -session $s -commandname *-test -formatTypeName *

C:\PS> new-test -name test1

C:\PS> get-test test1 | run-test

描述
-----------
這些命令會從 PSSession 將名稱結尾為 "-test" 的命令匯入本機工作階段,然後示範如何使用匯入的 Cmdlet。

第一個命令會使用 New-PSSession Cmdlet 建立 PSSession,並將這個 PSSession 儲存在 $s 變數中。

第二個命令會使用 Import-PSSession Cmdlet 從 $s 中的 PSSession 將命令匯入本機工作階段。它會使用 CommandName 參數指定具有 Test 名詞的命令,並使用 FormatTypeName 參數匯入 Test 命令的格式化資料。

第三個和第四個命令會使用目前工作階段中匯入的命令。因為匯入的命令實際上是新增到目前的工作階段,您可以使用本機語法加以執行,並不需要使用 Invoke-Command Cmdlet 執行匯入的命令。






範例 3

C:\PS>$s1 = new-pssession -computername s1 

C:\PS> $s2 = new-pssession -computername s2

C:\PS> import-pssession -session s1 -type cmdlet -name New-Test, Get-Test -FormatTypeName *

C:\PS> import-pssession -session s2 -type cmdlet -name Set-Test -FormatTypeName *

C:\PS> new-test Test1 | set-test -runtype full

描述
-----------
這個範例示範您可以像使用本機 Cmdlet 一樣使用匯入的 Cmdlet。

這些命令會從 Server01 電腦上的 PSSession 匯入 New-Test 和 Get-Test Cmdlet,以及從 Server02 電腦上的 PSSession 匯入 Set-Test Cmdlet。

雖然這些 Cmdlet 是從不同 PSSession 匯入而得,但您仍可經由管道將物件從一個 Cmdlet 匯入至另一個 Cmdlet,而不會有錯誤產生。






範例 4

C:\PS>$s = new-pssession -computername Server01

C:\PS> import-pssession -session $s -commandname *-test* -formattypename *

C:\PS> $batch = new-test -name Batch -asjob

C:\PS> receive-job $batch

描述
-----------
這個範例示範如何以背景工作的方式執行匯入的命令。

因為匯入的命令可能要花比本機命令還要久的執行時間,Import-PSSession 會在每個匯入的命令中新增 AsJob 參數。AsJob 參數可讓您以背景工作的方式執行命令。

第一個命令會在 Server01 電腦上建立 PSSession,然後將 PSSession 物件儲存到 $s 變數。

第二個命令會使用 Import-PSSession 從 $s 中的 PSSession 將 Test Cmdlet 匯入目前工作階段。

第三個命令會使用匯入之 New-Test Cmdlet 的 AsJob 參數,以背景工作的方式執行 New-Test 命令,並將 New-Test 所傳回的工作物件儲存在 $batch 變數中。

第四個命令會使用 Receive-Job Cmdlet 取得 $batch 變數中的工作結果。






範例 5

C:\PS>$s = new-pssession -comp Server01

C:\PS> invoke-command -session $s {import-module TestManagement}

C:\PS> import-pssession -session $s -module TestManagement

描述
-----------
這個範例示範如何從遠端電腦上的 Windows PowerShell 模組將 Cmdlet 和函數匯入目前的工作階段。

第一個命令會在 Server01 電腦上建立 PSSession,然後將其儲存到 $s 變數。

第二個命令會使用 Invoke-Command Cmdlet 在 $s 的 PSSession 中執行 Import-Module 命令。

一般來說,Windows PowerShell 設定檔內的 Import-Module 命令會將模組加入至所有工作階段,但設定檔無法在 PSSessions 中執行。

第三個命令會使用 Import-PSSession 的 Module 參數將模組中的 Cmdlet 和函數匯入目前的工作階段。






範例 6

C:\PS>import-pssession $s -CommandName Get-Date, SearchHelp  -formatTypeName * -AllowClobber


Name              : tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1zunz.ttf
Path              : C:\Users\User01\AppData\Local\Temp\tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1zunz.ttf\tmp_79468106-4e1d-4d90-af97-1154f9317239_
                    tcw1zunz.ttf.psm1
Description       : Implicit remoting for http://server01.corp.fabrikam.com/wsman
Guid              : 79468106-4e1d-4d90-af97-1154f9317239
Version           : 1.0
ModuleBase        : C:\Users\User01\AppData\Local\Temp\tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1zunz.ttf
ModuleType        : Script
PrivateData       : {ImplicitRemoting}
AccessMode        : ReadWrite
ExportedAliases   : {}
ExportedCmdlets   : {}
ExportedFunctions : {[Get-Date, Get-Date], [SearchHelp, SearchHelp]}
ExportedVariables : {}
NestedModules     : {}

描述
-----------
這個範例示範 Import-PSSession 在磁碟上的暫存檔中建立模組,也示範所有命令都會轉換成函數,然後再將其匯入目前工作階段。

此命令會使用 Import-PSSession Cmdlet 將 Get-Date Cmdlet 和 SearchHelp 函數匯入目前工作階段中。

Import-PSSession Cmdlet 會傳回代表暫時性模組的 PSModuleInfo 物件。Path 屬性值顯示 Import-PSSession 已經在暫存位置建立指令碼模組 (.psm1) 檔案。ExportedFunctions 屬性顯示 Get-Date Cmdlet 和 SearchHelp 函數兩者都當做函數匯入。






範例 7

C:\PS>import-pssession $s -CommandName Get-Date -formatTypeName * -AllowClobber

C:\PS> get-command get-date

CommandType   Name       Definition
-----------   ----       ----------
Function      Get-Date   ...
Cmdlet        Get-Date   Get-Date [[-Date] <DateTime>] [-Year <Int32>] [-Month <Int32>]

C:\PS> Get-Date
09074   

C:\PS> (get-command -type cmdlet -name get-date).pssnapin.name
Microsoft.PowerShell.Utility

C:\PS> Microsoft.PowerShell.Utility\get-date

Sunday, March 15, 2009 2:08:26 PM

描述
-----------
這個範例會示範如何執行匯入之命令所隱藏的命令。

第一個命令會從 $s 變數中的 PSSession 匯入 Get-Date Cmdlet。因為目前工作階段包含 Get-Date Cmdlet,所以此命令中需要 AllowClobber 參數。

第二個命令會使用 Get-Command Cmdlet 取得目前工作階段中的 Get-Date 命令。輸出顯示此工作階段包含原始 Get-Date Cmdlet 和 Get-Date 函數。Get-Date 函數會在 $s 的 PSSession 中執行匯入的 Get-Date Cmdlet。

第三個命令會執行 Get-Date 命令。因為函數優先於 Cmdlet,所以 Windows PowerShell 會執行匯入的 Get-Date 函數,此函數會傳回凱撒日 (Julian Date)。

第四和第五個命令會示範如何使用合格的名稱來執行匯入的命令所隱藏的命令。

第四個命令會取得將原始 Get-Date Cmdlet 加入至目前工作階段的 Windows PowerShell 嵌入式管理單元名稱。

第五個命令會使用 Get-Date Cmdlet 的嵌入式管理單元限定名稱來執行 Get-Date 命令。

如需命令優先順序和隱藏命令的詳細資訊,請參閱 about_Command_Precedence。






範例 8

C:\PS>import-pssession -session $s -commandName *Item* -AllowClobber

描述
-----------
這個命令會從 $s 的 PSSession 匯入名稱包含 "Item" 的命令。因為此命令包含 CommandName 參數,而不包含 FormatTypeData 參數,所以只會匯入此命令。

當您在遠端電腦上使用 Import-PSSession 執行命令,而且您在目前工作階段中已經有此命令的格式化資料時,請使用這個命令。






範例 9

C:\PS>$m = import-pssession -session $s -CommandName *bits* -formattypename *bits*

C:\PS> get-command -module $m

CommandType     Name
-----------     ----
Function        Add-BitsFile
Function        Complete-BitsTransfer
Function        Get-BitsTransfer
Function        Remove-BitsTransfer
Function        Resume-BitsTransfer
Function        Set-BitsTransfer
Function        Start-BitsTransfer
Function        Suspend-BitsTransfer

描述
-----------
這個命令示範如何使用 Get-Command 的 Module 參數來查明 Import-PSSession 命令將哪一個命令匯入工作階段。

第一個命令會使用 Import-PSSession Cmdlet,從 $s 變數中的 PSSession 匯入名稱包含 "bits" 的命令。Import-PSSession 命令會傳回暫時性模組,並將此模組儲存在 $m 變數中。

第二個命令會使用 Get-Command Cmdlet 取得 $m 變數中的模組所匯出的命令。

Module 參數會採用字串值,這個值是針對模組名稱所設計。但是,當您提交模組物件時,Windows PowerShell 會使用模組物件上的 ToString 方法,該物件會傳回模組名稱。

Get-Command 命令相當於 "get-command $m.name"。






請參閱




目錄