共用方式為


Windows PowerShell 會話狀態

會話狀態是指 Windows PowerShell 工作階段或模組的目前設定。 Windows PowerShell 工作階段是命令行使用者以互動方式或由主應用程式以程式設計方式使用的作業環境。 會話的會話狀態稱為全域會話狀態。

從開發人員的觀點來看,Windows PowerShell 會話是指主應用程式開啟 Windows PowerShell Runspace 和關閉 Runspace 的時間。 另一種方式是會話是 Runspace 存在時叫用之 Windows PowerShell 引擎實例的存留期。

模組會話狀態

每當模組或其其中一個巢狀模組匯入會話時,就會建立模組會話狀態。 當模組匯出 Cmdlet、函式或腳本等專案時,該元素的參考會新增至會話的全域會話狀態。 不過,當專案執行時,它會在模組的會話狀態內執行。

Session-State 數據

會話狀態數據可以是公用或私人。 公用數據可用於從會話狀態外部呼叫,而私人數據只能從會話狀態內呼叫。 例如,模組可以有私用函式,該函式只能由模組呼叫,或只能由導出的公用專案在內部呼叫。 這類似於 .NET Framework 類型的私用和公用成員。

會話狀態數據是由目前 Windows PowerShell 會話內容中執行引擎的目前實例所儲存。 工作階段狀態資料包含下列專案:

  • 路徑資訊

  • 磁碟驅動器資訊

  • Windows PowerShell 提供者資訊

  • 模組所匯出之匯入模組和模組元素參考的資訊,例如模組所導出的 Cmdlet、函式和腳本。 此資訊和這些參考僅適用於全域會話狀態。

  • 會話狀態變數資訊

存取 Cmdlet 內 Session-State 資料

Cmdlet 可以透過 Cmdlet 類別的 System.Management.Automation.PSCmdlet.SessionState* 属性間接存取會話狀態數據,或直接透過 System.Management.Automation.SessionState 類別存取會話狀態數據。 System.Management.Automation.SessionState 類別提供屬性,可用來調查不同類型的會話狀態數據。

另請參閱

System.Management.Automation.PSCmdlet.SessionState

System.Management.Automation.SessionState

Windows PowerShell Cmdlet

撰寫 Windows PowerShell Cmdlet

Windows PowerShell Shell SDK