共用方式為


關於會話設定檔

簡短描述

描述會話設定檔案,用於會話設定(也稱為「端點」),以定義使用會話設定之會話的環境。

詳細描述

「會話設定檔」是副檔名為 .pssc 的文字檔,其中包含會話設定屬性和值的雜湊表。 您可以使用會話設定檔來設定會話設定的屬性。 這麼做會定義使用該會話設定之任何 Windows PowerShell 會話的環境。

會話設定檔可讓您輕鬆地建立自訂會話設定,而不需要使用複雜的 c # 元件或腳本。

「會話設定」或「端點」是本機電腦設定的集合,可決定使用者可以在電腦上建立會話的專案;使用者可以在這些會話中執行的命令;以及會話是否應該以特殊許可權虛擬帳戶的身分執行。 如需會話設定的詳細資訊,請參閱about_Session_Configurations

會話設定是在 Windows PowerShell 2.0 引進,而會話設定檔是在 Windows PowerShell 3.0 中引進。 您必須使用 Windows PowerShell 3.0,將會話設定檔包含在會話設定中。 不過,Windows PowerShell 2.0 (和更新版本)的使用者會受到會話設定中的設定影響。

建立自訂會話

您可以在會話設定中指定會話屬性,以自訂 Windows PowerShell 會話的許多功能。 您可以撰寫定義自訂執行檔的 c # 程式來自訂會話,也可以使用會話設定檔來定義使用會話設定建立之會話的屬性。 一般的規則是,使用會話設定檔比撰寫 c # 程式更容易。

您可以使用會話設定檔,為高度信任的使用者建立功能完整的會話之類的專案;已鎖定允許最少存取的會話;專為特定,且只包含這些工作所需模組的會話;不具特殊許可權的使用者只能執行特定命令做為授權帳戶的會話。

除此之外,您也可以管理會話的使用者是否可以使用 Windows PowerShell 語言專案(例如腳本區塊),或是否只能執行命令。 您可以管理使用者可在會話中執行的 Windows PowerShell 版本。管理哪些模組會匯入至會話;並管理使用者可以執行的 Cmdlet、函式和別名會話。 使用 [RoleDefinitions] 欄位時,您可以根據群組成員資格,將會話中的不同功能提供給使用者。

如需 RoleDefinitions 和如何定義此值的詳細資訊,請參閱使用 new-psrolecapabilityfile Cmdlet 的說明主題。

建立會話設定檔

建立會話設定檔的最簡單方式是使用 New-pssessionconfigurationfile Cmdlet。 此 Cmdlet 會產生使用正確語法和格式的檔案,並自動驗證許多配置檔案屬性值。

如需可在會話設定檔中設定之屬性的詳細說明,請參閱 New-pssessionconfigurationfile Cmdlet 的說明主題。

下列命令會建立使用預設值的會話設定檔。 產生的設定檔只會使用預設值,因為不會包含 Path 參數以外的參數(指定檔案路徑):

New-PSSessionConfigurationFile -Path .\Defaults.pssc

若要在您的預設文字編輯器中查看新的設定檔,請使用下列命令:

Invoke-Item -Path .\Defaults.pssc

若要建立會話的會話設定,讓使用者可以在其中執行命令,但不使用 Windows PowerShell 語言的其他元素,請輸入:

New-PSSessionConfigurationFile -LanguageMode NoLanguage
-Path .\NoLanguage.pssc

在上述命令中,將 LanguageMode 參數設定為 NoLanguage,可防止使用者進行撰寫或執行腳本的操作,或使用變數。

若要為使用者只能使用 Get Cmdlet 的會話建立會話設定,請輸入:

New-PSSessionConfigurationFile -VisibleCmdlets Get-*
-Path .\GetSessions.pssc

在上述範例中,將 VisibleCmdlets 參數設定為 Get-* 會將使用者限制為名稱開頭為字串值 "Get-" 的 Cmdlet。

若要為以特殊許可權虛擬帳戶(而非使用者認證)執行的會話建立會話設定,請輸入:

New-PSSessionConfigurationFile -RunAsVirtualAccount
-Path .\VirtualAccount.pssc

若要為會話建立會話設定,以在角色功能檔案中指定使用者可見的命令,請輸入:

New-PSSessionConfigurationFile -RoleDefinitions
@{ 'CONTOSO\User' = @{ RoleCapabilities = 'Maintenance' }}
-Path .\Maintenance.pssc

使用會話設定檔

當您建立會話設定或新增時,您可以包含會話設定檔,您可以在稍後將檔案新增至會話設定。

若要在建立會話設定時包含會話設定檔,請使用 Enable-pssessionconfiguration Cmdlet 的 Path 參數。

例如,下列命令會在建立 NoLanguage 會話設定時使用 NoLanguage .pssc 檔案。

Register-PSSessionConfiguration -Name NoLanguage
-Path .\NoLanguage.pssc

當新的 NoLanguage 會話啟動時,使用者將只能存取 Windows PowerShell 命令。

若要將會話設定檔新增至現有的會話設定,請使用 Enable-pssessionconfiguration Cmdlet 和 Path 參數。 這會影響使用指定的會話設定所建立的任何新會話。 請注意,Enable-pssessionconfiguration 指令程式會變更會話本身,而不會修改會話設定檔。

例如,下列命令會將 NoLanguage .pssc 檔案新增至 LockedDown 會話設定。

Set-PSSessionConfiguration -Name LockedDown
-Path .\NoLanguage.pssc

當使用者使用 LockedDown 會話設定來建立會話時,他們將能夠執行 Cmdlet,但它們將無法建立或使用變數、指派值,或使用其他 Windows PowerShell 語言元素。

下列命令會使用新的-PSSession Cmdlet,在使用 LockedDown 會話設定的電腦 Srv01 上建立會話,並將物件參考儲存至 $s 變數中的會話。 會話設定的 ACL (存取控制清單)會決定可以使用它來建立會話的人員。

$s = New-PSSession -ComputerName Srv01
-ConfigurationName LockedDown

由於 NoLanguage 條件約束已新增至 LockedDown 會話設定,因此 LockedDown 會話中的使用者只能執行 Windows PowerShell 命令和 Cmdlet。 例如,下列兩個命令會使用 Invoke 命令 Cmdlet,在 $s 變數中所參考的會話中執行命令。 第一個命令會執行 UICulture Cmdlet,而且不會使用任何變數,因此會成功。 第二個命令會取得 $PSUICulture 變數的值,而失敗。

Invoke-Command -Session $s {Get-UICulture}
en-US

Invoke-Command -Session $s {$PSUICulture}
The syntax is not supported by this runspace. This might be
because it is in no-language mode.
+ CategoryInfo          : ParserError: ($PSUICulture:String) [],
ParseException
+ FullyQualifiedErrorId : ScriptsNotAllowed

編輯工作階段設定檔

會話設定中除了將 runasvirtualaccount 設和 RunAsVirtualAccountGroups 以外的所有設定都可以藉由編輯會話設定所使用的會話設定檔來進行修改。 若要這麼做,請從尋找會話設定檔的使用中複本開始。

當您在會話設定中使用會話設定檔時,Windows PowerShell 會建立會話設定檔的作用中複本,並將它儲存 $ 在 \ 本機電腦上的 pshome SessionConfig 目錄中。

會話設定檔的使用中複本位置會儲存在會話設定物件的 ConfigFilePath 屬性中。

下列命令會取得 NoLanguage 會話設定的會話設定檔案位置。

(Get-PSSessionConfiguration -Name NoLanguage).ConfigFilePath

該命令會傳回類似下列的檔案路徑:

C:\WINDOWS\System32\WindowsPowerShell\v1.0\SessionConfig\
NoLanguage_0c115179-ff2a-4f66-a5eb-e56e5692ba22.pssc

您可以在任何文字編輯器中編輯 .pssc 檔案。 儲存檔案之後,任何使用會話設定的新會話都會採用該檔案。

如果您需要修改將 runasvirtualaccount 設或 RunAsVirtualAccountGroups 設定,您必須取消註冊會話設定,並重新註冊包含已編輯值的會話設定檔。

測試會話設定檔

使用 New-pssessionconfigurationfile Cmdlet 來測試手動編輯的會話設定檔。 這一點很重要:如果檔案語法和值不是有效的,使用者將無法使用會話設定來建立會話。

例如,下列命令會測試 NoLanguage 會話設定的使用中會話設定檔。

Test-PSSessionConfigurationFile -Path C:\WINDOWS\System32\
WindowsPowerShell\v1.0\SessionConfig\
NoLanguage_0c115179-ff2a-4f66-a5eb-e56e5692ba22.pssc

如果設定檔中的語法和值為有效的測試,則 New-pssessionconfigurationfile 會傳回 True。 如果語法和值無效,則 Cmdlet 會傳回 False。

您可以使用測試 New-pssessionconfigurationfile 來測試任何會話設定檔案,包括 Enable-pssessionconfiguration Cmdlet 所建立的檔案。 如需詳細資訊,請參閱 New-pssessionconfigurationfile Cmdlet 的說明主題。

移除會話設定檔

您無法從會話設定中移除會話設定檔。 不過,您可以將檔案取代為使用預設設定的新檔案。 這會有效地取消原始設定檔案所使用的設定。

若要取代會話設定檔案,請建立使用預設設定的新會話設定檔,然後使用 Enable-pssessionconfiguration 指令程式,以新的檔案取代自訂的會話設定檔案。

例如,下列命令會建立預設的會話設定檔,然後取代 NoLanguage 會話設定中的使用中會話設定檔。

New-PSSessionConfigurationFile -Path .\Default.pssc
Set-PSSessionConfiguration -Name NoLanguage
-Path .\Default.pssc

當這些命令完成時,NoLanguage 會話設定實際上會針對使用該會話設定建立的所有會話,提供完整的語言支援(預設設定)。

查看會話設定的內容使用會話設定檔來代表會話設定的會話設定物件具有額外的屬性,可讓您輕鬆地探索和分析會話設定。 (請注意,下面顯示的類型名稱包含格式化的視圖定義)。您可以藉由執行 Enable-pssessionconfiguration Cmdlet,並將傳回的資料傳送至取得成員 Cmdlet,來查看屬性:

Get-PSSessionConfiguration NoLanguage | Get-Member
TypeName: Microsoft.PowerShell.Commands.PSSessionConfigurationCommands
#PSSessionConfiguration

Name                          MemberType     Definition
----                          ----------     ----------
Equals                        Method         bool Equals(System.O...
GetHashCode                   Method         int GetHashCode()
GetType                       Method         type GetType()
ToString                      Method         string ToString()
Architecture                  NoteProperty   System.String Archit...
Author                        NoteProperty   System.String Author...
AutoRestart                   NoteProperty   System.String AutoRe...
Capability                    NoteProperty   System.Object[] Capa...
CompanyName                   NoteProperty   System.String Compan...
configfilepath                NoteProperty   System.String config...
Copyright                     NoteProperty   System.String Copyri...
Enabled                       NoteProperty   System.String Enable...
ExactMatch                    NoteProperty   System.String ExactM...
ExecutionPolicy               NoteProperty   System.String Execut...
Filename                      NoteProperty   System.String Filena...
GUID                          NoteProperty   System.String GUID=0...
ProcessIdleTimeoutSec         NoteProperty   System.String Proces...
IdleTimeoutms                 NoteProperty   System.String IdleTi...
lang                          NoteProperty   System.String lang=e...
LanguageMode                  NoteProperty   System.String Langua...
MaxConcurrentCommandsPerShell NoteProperty   System.String MaxCon...
MaxConcurrentUsers            NoteProperty   System.String MaxCon...
MaxIdleTimeoutms              NoteProperty   System.String MaxIdl...
MaxMemoryPerShellMB           NoteProperty   System.String MaxMem...
MaxProcessesPerShell          NoteProperty   System.String MaxPro...
MaxShells                     NoteProperty   System.String MaxShells
MaxShellsPerUser              NoteProperty   System.String MaxShe...
Name                          NoteProperty   System.String Name=N...
PSVersion                     NoteProperty   System.String PSVersion
ResourceUri                   NoteProperty   System.String Resour...
RunAsPassword                 NoteProperty   System.String RunAsP...
RunAsUser                     NoteProperty   System.String RunAsUser
SchemaVersion                 NoteProperty   System.String Schema...
SDKVersion                    NoteProperty   System.String SDKVer...
OutputBufferingMode           NoteProperty   System.String Output...
SessionType                   NoteProperty   System.String Sessio...
UseSharedProcess              NoteProperty   System.String UseSha...
SupportsOptions               NoteProperty   System.String Suppor...
xmlns                         NoteProperty   System.String xmlns=...
XmlRenderingType              NoteProperty   System.String XmlRen...
Permission                    ScriptProperty System.Object Permis...

這些屬性可讓您輕鬆地搜尋特定的會話設定。 例如,您可以使用 ExecutionPolicy 屬性來尋找可支援具有 RemoteSigned 執行原則之會話的會話設定。 請注意,因為 ExecutionPolicy 屬性只存在於使用會話設定檔的會話中,所以此命令可能不會傳回所有合格會話設定。

Get-PSSessionConfiguration |
where {$_.ExecutionPolicy -eq "RemoteSigned"}

下列命令會取得 RunAsUser 為 Exchange 系統管理員的會話設定。

 Get-PSSessionConfiguration |
where {$_.RunAsUser -eq "Exchange01\Admin01"}

若要查看與設定相關聯之角色定義的資訊,請使用 Get-pssessioncapability Cmdlet。 此 Cmdlet 可讓您判斷特定端點中特定使用者可用的命令和環境。

注意

會話設定也支援一種會話類型,稱為「空的」會話。 空白的會話類型可讓您使用選取的命令來建立自訂會話。 如果您未將模組、函式或腳本加入空的會話,則會話會限制為運算式,而且可能不會有任何實際使用。 SessionType 屬性會告訴您,您是否正在使用空的會話。

另請參閱

about_Session_Configurations

New-PSSession

Disable-PSSessionConfiguration

Enable-PSSessionConfiguration

Get-PSSessionConfiguration

New-PSSessionConfigurationFile

Register-PSSessionConfiguration

Set-PSSessionConfiguration

Test-PSSessionConfigurationFile

Unregister-PSSessionConfiguration

Get-PSSessionCapability

New-PSRoleCapabilityFile