C#を介してリモートでExchangeサーバーにコマンドを発行したい。PowerShellを介してこれを直接行うと、機能します。しかし、C#を介してPowerShellにコマンドを発行すると、失敗します。
これが私がPowerShellで行っていることです(これは機能します):
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeFQDN/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
Enable-Mailbox -Identity "SomeIdentity" -Shared
これがC#です(動作しません):
var connectionInfo = new WSManConnectionInfo(
new Uri("http://ExchangeFQDN/PowerShell/"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
new PSCredential(username, password));
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
//Next line throws a PSInvalidOperationException
using (var runspace = RunspaceFactory.CreateRunspace(connectionInfo)) //<- fails
{
//Execute Enable-Mailbox command
}
このリモートログインを実行する権限を持つ特定のユーザーがいます。PowerShellはこのユーザーとして実行されますが、ユーザーはC#コードで偽装されます。これは、PSCredentialとして渡すこのユーザーのユーザー名とパスワードでもあります。
何が悪いのかについて何か考えはありますか?