none
PowerShell remote session in Csharp

    问题

  • Hi,

    I have web page (C#) where I'm trying to take powershell remote session into Lync's server.

    The problem is that the line "remoteRunspace.Open();" on the code:

                System.Security.SecureString password = new System.Security.SecureString();

                foreach (char c in "***")
                {                   
                    password.AppendChar(c);
                }

                PSCredential psc = new PSCredential("domain\\adminUser", password);

                WSManConnectionInfo remoteConnectionInfo = new WSManConnectionInfo(new Uri("https://lyncserver.company.com/ocspowershell"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", psc);

                remoteConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
                remoteConnectionInfo.OperationTimeout = 4 * 60 * 1000;
                remoteConnectionInfo.OpenTimeout = 1 * 60 * 1000;
                remoteConnectionInfo.SkipCNCheck = true;
                remoteConnectionInfo.SkipCACheck = true;

                Runspace remoteRunspace = RunspaceFactory.CreateRunspace(remoteConnectionInfo);

                try
                {
                    remoteRunspace.Open();
                }
                catch (Exception ex)
                {
                    return false;
                }

                PowerShell remotePowerShell = PowerShell.Create();

    Through me an exception with message: "Processing data from remote server failed with the following error message: Access is denied. For more information, see the about_Remote_Troubleshooting Help topic."

    On target server's security log I can see successfully logins.

    On the IIS log I can see "/ocspowershell PSVersion=2.0 443 domain\adminUser 10.11.12.16 Microsoft+WinRM+Client 500 0 0 60006"

    I have tried to use account with spn mode (adminuser@domain.com) but no help.

    I have tried different authentication mechanism: basic, kerberos, negotiate, only basic give different error which says: "....Possible authentication mechanisms reported by server:     Negotiate"

    No need to say, that on the pure powershell session I'm able to use that connection URL in the new-pssession. So problem should be in the code..?


    Petri

    2012年2月20日 23:07

全部回复

  • If the Powershell script works in cmd window, perhaps taking this to the lync developer/dev forum might assist getting an answer?

    +Say thanks and observe basic forum courtesy:
    +If this post answered your question, Mark As Answer
    +If this post was helpful, Vote as Helpful

    windowspbx blog: my thots/howtos
    see/submit Lync suggestions here: simple and public

    2012年2月22日 0:19
  • I actually tested this, and this seems somehow out from the Lync part. As I used the same code, and build an .exe file. And I was able to run that just fine...

    So does anybody knows, is there some limitations/protections that PSCredentials are not actually used when you are running the code under the IIS?


    Petri

    2012年2月22日 9:44
  • Hi Petri,

    From the error message Access is denied, it seems you haven’t got the right permission.

    To use Windows PowerShell remoting features, you must start Windows PowerShell as an administrator. When starting PowerShell from another program, you must start that program as an administrator too.


    Noya Lau

    TechNet Community Support

    2012年2月23日 10:54
  • I have a feeling, that access denied is coming from the remote server,  not locally....but where....

    Personally, I got this work when I switched the application pool's to run my personal account instead of the network service. But now it is working only for my account, not anybody else... weird..

    What permissions are needed remotely to get pssession up and running?


    Petri

    2012年2月27日 10:19