简体   繁体   English

PowerShell 远程处理:WSManClientSessionTransportManager.Initialize() 中的异常 PSInvalidOperationException

[英]PowerShell remoting: exception PSInvalidOperationException in WSManClientSessionTransportManager.Initialize()

I have C# Asp.Net application, which connects to remote computers (remote in relation to web server) to run commands on them using PowerShell.我有 C# Asp.Net 应用程序,它连接到远程计算机(与 Web 服务器相关的远程计算机)以使用 PowerShell 在它们上运行命令。 This generally works, however under some circumstances (which I don't completely understand yet), we completely lose ability to establish ANY remote sessions.这通常有效,但是在某些情况下(我还没有完全理解),我们完全失去了建立任何远程会话的能力。 When that happens, any attempt to connect to remote PowerShell results in the following exception:发生这种情况时,任何连接到远程 PowerShell 的尝试都会导致以下异常:

      System.Management.Automation.PSInvalidOperationException
      at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)
      at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper, String sessionName)
      at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)
      at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)
      at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)
      at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.CreateDSHandler(TypeTable typeTable)
      at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspacePool(Int32 minRunspaces, Int32 maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments)
      at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspacePool(Int32 minRunspaces, Int32 maxRunspaces, RunspaceConnectionInfo connectionInfo)
      ...

When that condition begins, this exception is thrown for all attempts to connect to any hosts, and from what I can tell, this is entirely client-side problem on the originating host.当这种情况开始时,对于连接到任何主机的所有尝试都会抛出此异常,据我所知,这完全是原始主机上的客户端问题。

I haven't found any explanation to that here or on MS forums.我在这里或在 MS 论坛上都没有找到对此的任何解释。 This is not the problem with user permissions of any kind, because everything works until some condition is triggered within Microsoft components.不是任何类型的用户权限的问题,因为在 Microsoft 组件中触发某些条件之前,一切都正常。

Here is how I connect:这是我的连接方式:

  WSManConnectionInfo ci = new WSManConnectionInfo(
     false, host, 5985, "/wsman",
     "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
     credential
  );
  ci.AuthenticationMechanism = AuthenticationMechanism.Credssp;

I tried to obtain run-space directly:我试图直接获得运行空间:

 runspace = RunspaceFactory.CreateRunspace(ci);
 runspace.ApartmentState = ApartmentState.MTA; // other values don't help
 runspace.Open();

and also via pool:也通过游泳池:

 pool = RunspaceFactory.CreateRunspacePool(1, 5, ci);
 pool.ApartmentState = ApartmentState.MTA;
 pool.Open();

... that makes no difference, except that few outer stack frames are slightly different. ...这没什么区别,只是很少有外部堆栈帧略有不同。

I suspect this has something to do with threading.我怀疑这与线程有关。 We do some PowerShell connections directly from IIS thread responding to AJAX request (synchronously), and some connections are done from separate threads asynchronously, started via ThreadPool.QueueUserWorkItem(...) .我们直接从响应 AJAX 请求的 IIS 线程(同步)进行一些 PowerShell 连接,并且一些连接是从单独的线程异步完成的,通过ThreadPool.QueueUserWorkItem(...)启动。 So, in the beginning (upon restarting IIS), both types of calls work, and then after some bad event, all types of connection attempts start to throw the above exception.因此,在开始时(重新启动 IIS 时),两种类型的调用都可以工作,然后在发生一些不良事件之后,所有类型的连接尝试都开始抛出上述异常。 This always happens after something that synchronous call does (which is very simply and seemingly benign) and never after asynchronous invocations.这总是发生在同步调用所做的事情之后(这是非常简单且看似良性的),而不是在异步调用之后。 IIS re-start clears that until the condition is triggered again. IIS 重新启动会清除该情况,直到再次触发该条件。

It looks as if some PowerShell operations may not be freeing resources properly.似乎某些 PowerShell 操作可能无法正确释放资源。 When sessions are finished, I tried to close them using all combinations of Close() , Disconnect() and Dispose() on run-space/pool, and that made no difference.会话完成后,我尝试在运行空间/池上使用Close()Disconnect()Dispose()所有组合关闭它们,这没有任何区别。

PS.附注。 There's this in event log for Remote Management:远程管理的事件日志中有这个:

(Event ID: 28) Access Denied error: the WSManCreateSession API caller does not match the creator of the application object (事件 ID:28)拒绝访问错误:WSManCreateSession API 调用方与应用程序对象的创建者不匹配

This message first appears after successful synchronous PowerShell connection, and then for all connections thereafter.此消息首先同步 PowerShell 连接成功出现,然后出现此后的所有连接中。

I have this same issue when trying to remotely connect to Exchange 2010 using Impersonation and Kerberos when calling runspace.Open(), but in my case it never works.在调用 runspace.Open() 时尝试使用 Impersonation 和 Kerberos 远程连接到 Exchange 2010 时,我遇到了同样的问题,但在我的情况下,它永远不会工作。 In the Windows Remote Management Event Logs I see WSMan successfully authenticate using the authenticated users accounts but then I see "(Event ID: 22) Calling into WSMan to receive output from the shell" initiated from the Application Pool account resulting in the same error "(Event ID: 28) Access Denied error: the WSManCreateSession API caller does not match the creator of the application object"在 Windows 远程管理事件日志中,我看到 WSMan 使用经过身份验证的用户帐户成功进行身份验证,但随后我看到“(事件 ID:22)调用 WSMan 以接收来自 shell 的输出”从应用程序池帐户启动,导致相同的错误“ (事件 ID:28)拒绝访问错误:WSManCreateSession API 调用方与应用程序对象的创建者不匹配”

Aspnet.config includes <legacyUnhandledExceptionPolicy enabled="false" /> <legacyImpersonationPolicy enabled="false" /> Aspnet.config 包含<legacyUnhandledExceptionPolicy enabled="false" /> <legacyImpersonationPolicy enabled="false" />

Web.config includes <identity impersonate="true" /> Web.config 包括<identity impersonate="true" />

Site Authentication Windows Authentication: Enabled Anonymous: Disabled站点身份验证 Windows 身份验证:启用匿名:禁用

The issue is with PowerShell 4.0.问题在于 PowerShell 4.0。 Update to Version 5.1更新至 5.1 版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM