2010-07-09 5 views
3

Я очень новичок в PS, поэтому я думаю, что у меня что-то не хватает здесь. Я могу сделать удаленный сеанс PowerShell, как это ...Создать удаленную сессию powershell в C#?

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

Я хочу эквивалент в C#, так что я пытаюсь это, но это не работает ...

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

Это терпит неудачу с ...

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

Как я могу перевести это на C#? Что такое параметр «shellUri» и как передать имя конфигурации (Microsoft.Exchange в моем случае)?

ответ

0

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

должны работать для оболочки и URI получить контекст в Биржевого конфигурации

+0

О, я вижу, поэтому указание этого «shell uri» на C# эквивалентно установке ConfigurationName на Microsoft.Exchange в Powershell? – noctonura

1

Я использую что-то вроде

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; 

При этом у вас есть доступ к удаленной сессии Powershell .. Используйте PSH для выполнения команд удаленно.

Смежные вопросы