2011-10-18 9 views
1

に私は、次のコマンドを実行しようとしています:のPowerShellスナップインの問題C#の

 

StringBuilder errorList = new StringBuilder(); 
      RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create(); 
      PSSnapInException snapEx = null; 
      PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx); 
      Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig); 
      runSpace.Open(); 
      Pipeline pipeLine = runSpace.CreatePipeline(); 
 

は、私は次のエラーを取得する: No snap-ins have been registered for Windows PowerShell version 2.

私はそのエラーの意味を正確に確認してくださいPoweShellに新しくないです。これは私がインストールする必要がありますか?

EDIT:完全なコード

 

     /// 
     /// Creates mailbox for the given user. 
     /// 
     /// Email address of user. 
     public void EnableMailbox(string userEmail) 
     { 
      StringBuilder errorList = new StringBuilder(); 
      RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create(); 
      PSSnapInException snapEx = null; 
      PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx); 
      Runspace runSpace = RunspaceFactory.CreateRunspace(runspaceConfig); 
      runSpace.Open(); 
      Pipeline pipeLine = runSpace.CreatePipeline(); 
      if (!MailBoxAlreadyExist(userEmail, runSpace)) 
      { 
       Command createMailbox = new Command("Enable-Mailbox"); 
       createMailbox.Parameters.Add("identity", userEmail); 
       createMailbox.Parameters.Add("database", "Mailbox Database Name"); 
       pipeLine.Commands.Add(createMailbox); 
       pipeLine.Invoke(); 
       if (pipeLine.Error != null && pipeLine.Error.Count > 0) 
       { 
        foreach (object item in pipeLine.Error.ReadToEnd()) 
        { 
         errorList.Append(item.ToString()); 
         errorList.Append(System.Environment.NewLine); 
        } 
        Console.WriteLine(errorList.ToString()); 
       } 
      } 
      else 
      { 
       Console.WriteLine("Mailbox of user " + userEmail + " already exists on exchange server."); 
      } 
      pipeLine.Dispose(); 
      runSpace.Close(); 
      runSpace.Dispose(); 
     } 
 

答えて

3

32ビットと64ビットSNAPINS間dinstinctionがあります。 Echangeが32ビットのみであっても、その場合はC#プロジェクトをターゲットプラットフォームx86に設定することができます。 Exchangeが64ビットの場合、C#プロジェクトをターゲットプラットフォームx64にのみ設定します。

2

Keithが指摘しているように、適切なターゲットプラットフォームが必要です。 Exchangeは64ビットのみです。 C#プロジェクトをターゲットプラットフォームx64に設定する必要があります。新しいプロジェクトでは、デフォルトでx86に設定されています(少なくともVisual Studio 2010では)。

0

リモートパワーセッションを開いて、C#からPSコマンドを実行する必要があります。 c#コードからExchangeスナップインをローカルで実行することは、これ以上サポートされません。 既存のメールボックスを列挙するサンプルを次に示します。

var ExchangeCredential = new PSCredential(user, password.ToSecureString()); 
string serverName = string.Format("{0}.{1}", GetMachinename(), GetDomainName()); 
var serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName)); 

var connectionInfo = new WSManConnectionInfo(serverUri,"http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential); 

runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
PowerShell psh = PowerShell.Create(); 
psh.Runspace = ru 

Pipeline pipeline = runspace.CreatePipeline(); 
var command = new Command("Get-MailboxDatabase"); 
command.Parameters.Add(new CommandParameter("Status", true)); 

pipeline.Commands.Add(command); 
commandResults = pipeline.Invoke();