Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create remote powershell session in c#?

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)?

like image 606
noctonura Avatar asked Jul 31 '26 10:07

noctonura


1 Answers

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

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

like image 125
Taylor Bird Avatar answered Aug 02 '26 00:08

Taylor Bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!