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 "***")

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

                WSManConnectionInfo remoteConnectionInfo = new WSManConnectionInfo(new Uri(""), "", 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);

                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 Microsoft+WinRM+Client 500 0 0 60006"

    I have tried to use account with spn mode ( 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..?


    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?


    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?


    2012年2月27日 10:19