3

I'm very new to PS so I think I'm missing something basic here. I can do a remote powershell session like this...

$Session = New-PSSession -ConfigurationName Microsoft.Exchange
                             -ConnectionUri https://remote.com/Powershell
                             -Credential "Domain\Admin"

I want the equivalent in C# so I'm trying this but it doesn't work...

WSManConnectionInfo connInfo = new WSManConnectionInfo(
   new Uri("https://remote.com/Powershell"),    /* uri */
   "/wtf",                        /* shellUri??? */
   cred);                                       /* ps-credential */

using (Runspace runspace = RunspaceFactory.CreateRunspace(connInfo))
{
    runspace.Open();
    using (PowerShell ps = PowerShell.Create())
    {
        ps.Runspace = runspace;
    }
}

This fails with...

System.Management.Automation.Remoting.PSRemotingTransportException was unhandled
  Message=Connecting to remote server failed with the following error message : The WS-Management service cannot process the request. The resource URI (/wsman) was not found in the WS-Management catalog....

How can I translate that into C#? What is the "shellUri" parameter and how to I convey the configuration name (Microsoft.Exchange in my case)?

3 Answers 3

1

http://schemas.microsoft.com/powershell/Microsoft.Exchange

should work for shell uri and get context into the Exchange config

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see, so specifying this "shell uri" in C# is equivalent setting the ConfigurationName to Microsoft.Exchange in Powershell?
1

I use Something like

int iRemotePort = 5985;
string strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string strAppName = @"/wsman";
AuthenticationMechanism auth = AuthenticationMechanism.Negotiate;

WSManConnectionInfo ci = new WSManConnectionInfo(
    false,
    sRemote,
    iRemotePort,
    strAppName,
    strShellURI,
    creds);
ci.AuthenticationMechanism = auth;

Runspace runspace = RunspaceFactory.CreateRunspace(ci);
runspace.Open();

PowerShell psh = PowerShell.Create();
psh.Runspace = runspace;

With this you have access to remote powershell session.. Use psh to run commands remotely.

Comments

-1

The shellUri should be http://schemas.microsoft.com/powershell/Microsoft.Exchange.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.