Question

I'm trying to run powershell script from an ASP.NET site, hosted on IIS 7.5 on Windows 2008R2.

The site has two fields, for user name and password and a button.
When the button is pressed, the following code executes, and the output written to the site.

Everything is working just fine with the first user who logged into the site.
The first user can run the cmdlet infinite number of times.
But when I log in into the site with a different user (kerberos authentication), the second user (and all further users) can't run the script.
The RunspaceFactory.CreateRunspace() is failing with an exception.
What is the exception? good question, the Message and the innerException is blank.
The event log also empty.
After an iisreset command, again, the first user is able to run the script, and all other cant.

Furthermore, I built a WinForms application that does the exact same thing, with the username and password fields and everything workes just fine!
So I'm assuming, the problem is with the IIS connection to powershell.

The following code executes on each button press:
(May contain minor syntax errors, written in notepad (cant copy past))

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();
}

Would appriciate any help!
Thanks in advanced.

Was it helpful?

Solution

OK so the problem is actually a bug (or feature) in powershell library. The object just won't die, even if you force GC.

That is Microsoft answer after hours of windbg with Microsoft's consultant.

You can pass around this problem with open a new process for each runspace, and can pass the user using win32api.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top