1

私は以下を実行しようとしています:


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 を初めて使用し、そのエラーが何を意味するのか正確にはわかりません。これは私がインストールする必要があるものですか?

編集:完全なコード


        /// 
        /// 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();
        }

4

3 に答える 3

4

32 ビットと 64 ビットのスナップインには違いがあります。Echange が 32 ビットのみである可能性があります。その場合、C# プロジェクトをターゲット プラットフォーム x86 に設定します。Exchange が 64 ビットのみの場合、C# プロジェクトをターゲット プラットフォーム x64 に設定します。

于 2011-10-19T00:29:19.387 に答える
3

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

于 2011-10-19T08:34:05.277 に答える
0

C# から PS コマンドを実行するには、リモート PowerShell セッションを開く必要があります。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();
于 2013-11-05T08:14:47.873 に答える