You can't create multiple System.Management.Automation runspaces in a web app for two or more different users

Applies to: Windows Server 2012 R2 EssentialsWindows Server 2012 R2 StandardWindows Server 2012 Essentials More

Symptoms


When you try to allow multiple users to create System.Management.Automation runspaces in a web application and you're using implicit credentials (for example, credentials from impersonation), you notice that only the first user who accesses the web application can call the CreateRunspace method. All subsequent requests fail with an exception that resembles the following:

System.Management.Automation.PSInvalidOperationException
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize
at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor
at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor
at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor
at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.CreateDSHandler
at System.Management.Automation.Runspaces.RunspacePool..ctor
at System.Management.Automation.RemoteRunspace..ctor
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace

Cause


This behavior is by design.

Workaround


To create multiple System.Management.Automation runspaces in a web application for two or more different users, you must pass in explicit credentials.

More Information


In order to create multiple remote management runspaces, make sure that the following conditions are true: 
  • ASP.Net impersonation is not enabled on the IIS virtual directory.
  • All Windows impersonation logic has been removed from your code.
  • Your users are prompted for a user name and password.