第二次尝试从IIS调用powershell失败



我正在尝试从ASP.NET站点运行powershell脚本,该站点托管在Windows 2008R2上的IIS 7.5上。

该网站有两个字段,用于用户名和密码,以及一个按钮
按下按钮后,将执行以下代码,并将输出写入站点。

第一个登录该网站的用户一切都很好
第一个用户可以无限次运行cmdlet
但是,当我使用不同的用户(kerberos身份验证)登录网站时,第二个用户(以及所有其他用户)无法运行脚本
RunspaceFactory.CreateRunspace()失败,并出现异常
例外情况是什么?好问题,Message和innerException为空
事件日志也为空
在iisreset命令之后,第一个用户可以再次运行脚本,而所有其他用户都不能运行。

此外,我构建了一个WinForms应用程序,它做了完全相同的事情,使用用户名和密码字段,一切都很好
所以我认为问题出在IIS与powershell的连接上。

以下代码在每次按下按钮时执行:
(可能包含小的语法错误,写在记事本中(不能复制过去)

string serverNameFQDN = "myserver.mydomain.com";
char[] cArray = passwordClearText.ToCharArray();
SecureString secString = new SecureString();
foreach(char c in cArray)
{
        secString.AppendChar(c);
}
PSCredential psCred = new PSCredential(Username, secString);
WSManConnectionInfo connectionInfo = 
    new WSManConnectionInfo(new Uri(String.Format("http://{0}:5985/wsman", serverNameFQDN)),
                    "http://schemas.microsoft.com/powershell/Microsoft.Powershell", psCred)
    {
        AuthenticationMechanism = AuthenticationMechanism.Kerberos,
        OpenTimeout = 10000,
        OperationTimeout = 30*60*1000;
    };
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
    var pipe = remoteRunspace.CreatePipeline();
    remoteRunspace.Open();
    pipe.Commands.AddScript("some-cmdlet");
    var res = pipe.Invoke();
    // Get the output, irrelevent.
    pipe.Stop();
    pipe.Dispose();
    remoteRunSpace.Close();
}

愿意提供任何帮助
提前感谢。

好的,所以问题实际上是powershell库中的一个bug(或功能)。即使强制GC,对象也不会死。

这是微软在与微软顾问闲聊数小时后的回答。

您可以通过为每个运行空间打开一个新进程来解决这个问题,也可以使用win32api传递给用户。

最新更新