C# 如何在C语言中运行ExchangeShell命令?

C# 如何在C语言中运行ExchangeShell命令?,c#,exchange-server,C#,Exchange Server,如何在C#中运行(Exchange 2013)获取mailboxdatabasecopystatus-databasename 我尝试了很多组合,但都失败了,最近的一个组合是: string un = @"domain\username"; System.Security.SecureString pw = new System.Security.SecureString(); string password = "password"; foreach (char ch in passw

如何在C#中运行(Exchange 2013)
获取mailboxdatabasecopystatus-databasename

我尝试了很多组合,但都失败了,最近的一个组合是:

string un = @"domain\username";
System.Security.SecureString pw = new     System.Security.SecureString();
string password = "password";
foreach (char ch in password)
{
    pw.AppendChar(ch);
}
PSCredential cred = new PSCredential(un, pw);
PowerShell ps = PowerShell.Create();

string cmdlet = "Get-MailboxDatabaseCopyStatus database1";

ps.AddCommand("Set-Variable");

ps.AddParameter("Name", "cred");
ps.AddParameter("Value", cred);

ps.AddScript(@"$session = New-PSSession -configurationname microsoft.exchange -connectionuri http://exchangeserver.com/powershell -auth kerberos -credential $cred");
//ps.AddScript(@"Import-PSSession $session");
ps.AddScript(@"$a = Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");
ps.AddScript(@"Remove-PSSession -Session $s");
ps.AddScript(@"echo $a");

foreach (PSObject result in ps.Invoke())
{
    MessageBox.Show(result.Members.ToString());
}

下面是一些代码,我可以用它来获取Office 365上的用户。我没有在Exchange 2013内部部署安装中进行测试以尝试该特定cmdlet,但这种方法应该非常类似,假设您是从客户机执行此操作

PSCredential credentials = new PSCredential(userName, password);

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
    new Uri("https://outlook.office365.com/PowerShell-LiveID"),
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
    credentials);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.AddCommand("Get-User");
        powershell.AddParameter("ResultSize", 50);

        runspace.Open();
        powershell.Runspace = runspace;

        Collection<PSObject> results = powershell.Invoke();

        Console.WriteLine("Results: {0}", results.Count);
    }
}
PSCredential credentials=新的PSCredential(用户名、密码);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(
新Uri(“https://outlook.office365.com/PowerShell-LiveID"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
证书);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Basic;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
使用(PowerShell PowerShell=PowerShell.Create())
{
powershell.AddCommand(“获取用户”);
powershell.AddParameter(“ResultSize”,50);
Open();
powershell.Runspace=运行空间;
收集结果=powershell.Invoke();
WriteLine(“结果:{0}”,Results.Count);
}
}

非常感谢@Jason Johnston。通过一些调整,我能够让它正常工作

现在,我需要获取用户所在域中Exchange服务器的列表,以便自动填充该字段

string un = @"domain\username";
            System.Security.SecureString pw = new System.Security.SecureString();
            string password = "password";
            string databaseName = "databasename";
            string exchangeServerName = "http://exchangeserver.com/powershell";
            string microsoftSchemaName = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
            foreach (char ch in password)
            {
                pw.AppendChar(ch);
            }
            PSCredential cred = new PSCredential(un, pw);

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(exchangeServerName), microsoftSchemaName, cred);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.AddCommand("Get-Mailboxdatabasecopystatus");
                    powershell.AddParameter("identity", databaseName);
                    powershell.AddCommand("select-object");
                    var props = new string[] { "name", "status" };
                    powershell.AddParameter("property", props);
                    runspace.Open();
                    powershell.Runspace = runspace;

                    Collection<PSObject> results = powershell.Invoke();

                    foreach (PSObject result in results)
                    {
                        MessageBox.Show(result.ToString());
                    }
                }
            }
string un=@“域\用户名”;
System.Security.SecureString pw=新的System.Security.SecureString();
字符串password=“password”;
字符串databaseName=“databaseName”;
字符串exchangeServerName=”http://exchangeserver.com/powershell";
字符串microsoftSchemaName=”http://schemas.microsoft.com/powershell/Microsoft.Exchange";
foreach(密码中的字符)
{
pw.AppendChar(ch);
}
PSCredential cred=新PSCredential(un,pw);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(新的Uri(exchangeServerName),microsoftSchemaName,cred);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Kerberos;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
使用(PowerShell PowerShell=PowerShell.Create())
{
AddCommand(“Get-Mailboxdatabasecopystatus”);
powershell.AddParameter(“标识”,数据库名);
powershell.AddCommand(“选择对象”);
var props=新字符串[]{“名称”,“状态”};
powershell.AddParameter(“属性”,props);
Open();
powershell.Runspace=运行空间;
收集结果=powershell.Invoke();
foreach(PSObject结果生成结果)
{
Show(result.ToString());
}
}
}

您是否遇到错误?怎么了?术语$session=New PSSession-configurationname microsoft.exchange-connectionuri-auth kerberos-credential$cred“”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者检查是否包含路径。请验证路径是否正确,然后重试。然后是我正在使用的includes和dll。只要看看get-mailboxdatabasecopystatus的工作格式,我就可以调整它,这将非常有帮助。