2015-01-20 10 views
-1

C (Exchange 2013) get-mailboxdatabasecopystatus -databasename을 어떻게 실행할 수 있습니까?C sharp에서 Exchange 셸 명령을 실행하는 방법?

나는 많은 조합을 시도하고 그들 모두는, 가장 최근의 존재 실패 : 여기

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

오류가 있습니까? 뭐가 문제 야? – DLeh

+0

$ session = New-PSSession -configurationname Microsoft.exchange-connectionuri http://exchangeserver.com/powershell-auth kerberos -credential $ cred "라는 용어는 cmdlet, 함수, 스크립트 파일 또는 작동 가능 프로그램의 이름으로 인식되지 않습니다. 프로그램 이름의 철자를 검사하거나 경로가 포함되었는지 경로가 올바른지 확인하고 다시 시도하십시오 다음은 포함 및 dll을 사용하고 있습니다. – Jay

+0

get-mailboxdatabasecopystatus, 내가 조정할 수있는 것은 매우 도움이 될 것입니다. – Jay

답변

1

을 내가없는 오피스 365에 대한 가져 오기 - 사용자를 할 나를 위해 일한 몇 가지 코드입니다 이 특정 cmdlet을 테스트하기 위해 Exchange 2010을 전제로 설치 테스트를 수행하지만 클라이언트 컴퓨터에서이 작업을 수행 중이라고 가정하면이 방법이 매우 유사해야합니다.

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

감사합니다. 제이슨 존스턴. 이것에 대한 약간의 조정을 통해 제대로 작동 할 수있었습니다.

이제 사용자가 해당 필드를 자동으로 채우려는 도메인의 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()); 
        } 
       } 
      } 
관련 문제