Reputation: 1467
I wonder how powershell handles powershell modules - I have been reading about AppDomain, PSSession, Runspace, I wonder when powershell imports a module, is the module loaded in the same AppDomain, same runspace?
I know that if you put C# code in powershell, c# code will be compiled and loaded into the same AppDomain. However I cannot find relevant information on Powershell module..
--edit--
I just run some test using
[System.AppDomain]::CurrentDomain.FriendlyName
It shows modules are loaded in the same AppDomain. But I still dont know about PSSession..
Upvotes: 2
Views: 633
Reputation: 201592
If you want to find out what modules are loaded execute Get-Module
. But don't confuse PowerShell modules with ProcessModule (a .dll). A process module could be related to PowerShell if the process module is part of a binary PowerShell module or PSSnapin. OTOH a PowerShell module can be (and often is) just a PSM1 file - no dll at all.
AppDomain is very broad .NET concept that applies to all .NET processes. PowerShell has Runspaces which is the environment in which pipelines run your session state is managed (global variables, provider state, etc). Typically each PowerShell process gets a Runspace but in the case of PowerShell_ISE, it can have multiple runspaces (one per PowerShell Tab). You can see this via the following:
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
Note the InstanceId will typically be the same in a PowerShell prompt. The exceptions are jobs, event handlers and workflows which run in different runspaces (usually different processes).
PSSession is a somewhat different beast used to specify a pesistent remoting session to a remote computer.
Upvotes: 4