Créer une session Powershell à distance dans c #?

Je suis très nouveau sur PS alors je pense qu’il me manque quelque chose de fondamental ici. Je peux faire une session de Powershell à distance comme ça …

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

Je veux l’équivalent en C #, alors j’essaie ça mais ça ne marche pas …

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

Cela échoue avec …

 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.... 

Comment puis-je traduire cela en C #? Quel est le paramètre “shellUri” et comment transmettre le nom de la configuration (Microsoft.Exchange dans mon cas)?

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

devrait travailler pour shell uri et obtenir un contexte dans la configuration Exchange

J’utilise quelque chose comme

 int iRemotePort = 5985; ssortingng strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; ssortingng 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; 

Avec cela, vous avez access à la session Powershell à distance. Utilisez psh pour exécuter des commandes à distance.