2

我想知道 powershell 如何处理 powershell 模块 - 我一直在阅读有关 AppDomain、PSSession、Runspace 的信息,我想知道当 powershell 导入模块时,模块是否加载到同一个 AppDomain、同一个运行空间中?

我知道如果你把 C# 代码放在 powershell 中,c# 代码将被编译并加载到同一个 AppDomain 中。但是我找不到有关 Powershell 模块的相关信息。

- 编辑 -

我只是使用

[System.AppDomain]::CurrentDomain.FriendlyName

它显示模块加载在同一个 AppDomain 中。但我仍然不知道 PSSession ..

4

1 回答 1

4

如果您想找出加载了哪些模块,请执行Get-Module. 但不要将 PowerShell 模块与 ProcessModule(一个 .dll)混淆。如果进程模块是二进制 PowerShell 模块或 PSSnapin 的一部分,则进程模块可能与 PowerShell 相关。OTOH PowerShell 模块可以(并且通常是)只是一个 PSM1 文件 - 根本没有 dll。

AppDomain 是一个非常广泛的 .NET 概念,适用于所有 .NET 进程。PowerShell 具有运行空间,它是运行管道的环境,您的会话状态被管理(全局变量、提供程序状态等)。通常每个 PowerShell 进程都有一个运行空间,但在 PowerShell_ISE 的情况下,它可以有多个运行空间(每个 PowerShell 选项卡一个)。您可以通过以下方式看到这一点:

PS> $ExecutionContext.Host.Runspace


Events                 : System.Management.Automation.PSLocalEventManager
ThreadOptions          : ReuseThread
JobManager             : System.Management.Automation.JobManager
RunspaceConfiguration  :
InitialSessionState    : System.Management.Automation.Runspaces.InitialSessionState
Version                : 3.0
RunspaceStateInfo      : Opened
RunspaceAvailability   : Busy
ConnectionInfo         :
OriginalConnectionInfo :
LanguageMode           : FullLanguage
ApartmentState         : STA
InstanceId             : b80ae2aa-a70b-43b2-a63f-def6c92fd032
SessionStateProxy      : System.Management.Automation.Runspaces.SessionStateProxy
Debugger               : System.Management.Automation.Debugger

请注意,在 PowerShell 提示符中,InstanceId 通常是相同的。例外是在不同运行空间(通常是不同的进程)中运行的作业、事件处理程序和工作流。

PSSession 是一种有点不同的野兽,用于指定与远程计算机的持久远程会话。

于 2013-01-13T18:33:03.970 回答