2016-04-06 6 views
0

Exchange Server 2013でメールボックスを作成する場合は、C#を使用します。CでExchangeメールボックスを作成する#

私は多くのコードを試しましたが、それぞれを解決する明白な解決策がないというエラーが表示されます。

私はこのエラーを取得
Boolean Success = CreateUser("firstname", "lastName", "aliasName", "AAaa12345", "mydomain.com", "mydomain.com/Users"); 

::私はテスト

Additional information: The term 'New-MailBox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

、別のコードは次のとおりです。

string userName = "administrator"; 
     string password = "mypass"; 
     System.Security.SecureString securePassword = new System.Security.SecureString(); 
     foreach (char c in password) 
     { 
      securePassword.AppendChar(c); 
     } 
     PSCredential credential = new PSCredential(userName, securePassword); 
     WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://{my server IP}/POWERSHELL/Microsoft.Exchange"), 
     "http://schemas.microsoft.com/powershell/Microsoft.Exchange", 
     credential); 

     connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; 
     connectionInfo.SkipCACheck = true; 
     connectionInfo.SkipCNCheck = true; 
     connectionInfo.MaximumConnectionRedirectionCount = 2; 

     using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) 
     { 
      runspace.Open(); 
      using (PowerShell powershell = PowerShell.Create()) 
      { 
       powershell.Runspace = runspace; 
       //Create the command and add a parameter 
       powershell.AddCommand("Get-Mailbox"); 
       powershell.AddParameter("RecipientTypeDetails", "UserMailbox"); 
       //Invoke the command and store the results in a PSObject collection 
       Collection<PSObject> results = powershell.Invoke(); 
       //Iterate through the results and write the DisplayName and PrimarySMTP 
       //address for each mailbox 
       foreach (PSObject result in results) 
       { 
        Console.WriteLine(
         string.Format("Name: { 0}, PrimarySmtpAddress: { 1}", 

       result.Properties["DisplayName"].Value.ToString(), 
       result.Properties["PrimarySmtpAddress"].Value.ToString() 

       )); 

       } 
      } 
     } 

私のコードは、それを呼び出す

public static Boolean CreateUser(string FirstName, string LastName, string Alias,string PassWord, string DomainName, string OrganizationalUnit) 
    { 
     string Name = FirstName + " " + LastName; 
     string PrincipalName = FirstName + "." + LastName + "@" + DomainName; 

     Boolean success = false; 
     string consolePath = @"C:\Program Files\Microsoft\Exchange Server\V15\bin\exshell.psc1"; 
     PSConsoleLoadException pSConsoleLoadException = null; 
     RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(consolePath, out pSConsoleLoadException); 
     SecureString spassword = new SecureString(); 
     spassword.Clear(); 

     foreach (char c in PassWord) 
     { 
      spassword.AppendChar(c); 
     } 

     PSSnapInException snapInException = null; 
     Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig); 
     myRunSpace.Open(); 
     Pipeline pipeLine = myRunSpace.CreatePipeline(); 

     Command myCommand = new Command("New-MailBox"); 
     myCommand.Parameters.Add("Name", Name); 
     myCommand.Parameters.Add("Alias", Alias); 
     myCommand.Parameters.Add("UserPrincipalName", PrincipalName); 
     myCommand.Parameters.Add("Confirm", true); 
     myCommand.Parameters.Add("SamAccountName", Alias); 
     myCommand.Parameters.Add("FirstName", FirstName); 
     myCommand.Parameters.Add("LastName", LastName); 
     myCommand.Parameters.Add("Password", spassword); 
     myCommand.Parameters.Add("ResetPasswordOnNextLogon", false); 
     myCommand.Parameters.Add("OrganizationalUnit", OrganizationalUnit); 
     pipeLine.Commands.Add(myCommand); 
     pipeLine.Invoke();  // got an error here 
     myRunSpace.Dispose(); 
     } 

です

と私は私が私の管理者ユーザーに必要とされるすべての権限を与え、ファイアウォールがオフになっているが、それはまだ動作しないと思います。このエラーに

Additional information: Connecting to remote server {Server IP Address} failed with the following error message : [ClientAccessServer=WIN-FRP2TC5SKRG,BackEndServer=,RequestId=460bc5fe-f809-4454-8472-ada97eacb9fb,TimeStamp=4/6/2016 6:23:28 AM] Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. 

を取得します。

ヘルプやヒント おかげ

答えて

0

私は、「32ビットを好む」をチェックし、問題が解決した、x64のにプラットフォームターゲットを変更しました。

public class ExchangeShellExecuter 
{ 

    public Collection<PSObject> ExecuteCommand(Command command) 
    { 
     RunspaceConfiguration runspaceConf = RunspaceConfiguration.Create(); 
     PSSnapInException PSException = null; 
     PSSnapInInfo info = runspaceConf.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out PSException); 
     Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConf); 
     runspace.Open(); 
     Pipeline pipeline = runspace.CreatePipeline(); 
     pipeline.Commands.Add(command); 
     Collection<PSObject> result = pipeline.Invoke(); 
     return result ; 
    } 
} 
    public class ExchangeShellCommand 
{ 
    public Command NewMailBox(string userLogonName,string firstName,string lastName,string password 
     ,string displayName,string organizationUnit = "mydomain.com/Users", 
     string database = "Mailbox Database 1338667540", bool resetPasswordOnNextLogon = false) 
    { 
     try 
     { 
      SecureString securePwd = ExchangeShellHelper.StringToSecureString(password); 
      Command command = new Command("New-Mailbox"); 
      var name = firstName + " " + lastName; 

      command.Parameters.Add("FirstName", firstName); 
      command.Parameters.Add("LastName", lastName); 
      command.Parameters.Add("Name", name); 

      command.Parameters.Add("Alias", userLogonName); 
      command.Parameters.Add("database", database); 
      command.Parameters.Add("Password", securePwd); 
      command.Parameters.Add("DisplayName", displayName); 
      command.Parameters.Add("UserPrincipalName", userLogonName+ "@mydomain.com"); 
      command.Parameters.Add("OrganizationalUnit", organizationUnit); 
      //command.Parameters.Add("ResetPasswordOnNextLogon", resetPasswordOnNextLogon); 

      return command; 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

    public Command AddEmail(string email, string newEmail) 
    { 
     try 
     { 
      Command command = new Command("Set-mailbox"); 
      command.Parameters.Add("Identity", email); 
      command.Parameters.Add("EmailAddresses", newEmail); 
      command.Parameters.Add("EmailAddressPolicyEnabled", false); 

      return command; 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
     // 
    } 

    public Command SetDefaultEmail(string userEmail, string emailToSetAsDefault) 
    { 
     try 
     { 
      Command command = new Command("Set-mailbox"); 
      command.Parameters.Add("Identity", userEmail); 
      command.Parameters.Add("PrimarySmtpAddress", emailToSetAsDefault); 

      return command; 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
     //PrimarySmtpAddress 
    } 


} 

とで実行:次のコードで

target.Ifケルベロスが必要とされないため

var addEmailCommand = new ExchangeShellCommand().AddEmail("[email protected]","[email protected]"); 
     var res2 = new ExchangeShellExecuter().ExecuteCommand(addEmailCommand); 

     var emailDefaultCommand = new ExchangeShellCommand().AddSetDefaultEmail("[email protected]", "[email protected]"); 
     var res3 = new ExchangeShellExecuter().ExecuteCommand(emailDefaultCommand); 
0

はこれを試してみてください:

//Secure String 

string pwd = "Password"; 
char[] cpwd = pwd.ToCharArray(); 
SecureString ss = new SecureString(); 
foreach (char c in cpwd) 
ss.AppendChar(c); 

//URI 

Uri connectTo = new Uri("http://exchserver.domain.local/PowerShell"); 
string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; 

//PS Credentials 

PSCredential credential = new PSCredential("Domain\\administrator", ss); 

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, credential); 
connectionInfo.MaximumConnectionRedirectionCount = 5; 
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo); 
remoteRunspace.Open(); 

PowerShell ps = PowerShell.Create(); 
ps.Runspace = remoteRunspace; 

ps.Commands.AddCommand("Get-Mailbox"); 
ps.Commands.AddParameter("Identity","[email protected]"); 

foreach (PSObject result in ps.Invoke()) 
{ 
Console.WriteLine("{0,-25}{1}", result.Members["DisplayName"].Value, 
result.Members["PrimarySMTPAddress"].Value); 
} 
+0

が....構成されていない、ネゴシエート認証メカニズムと再送信を指定します操作。詳細については、about_Remote_Troubleshootingのヘルプトピックを参照してください。 –

+0

はあなたの広告の中のあなたの交換サーバーですか、それとも外ですか?それが外で使用されている場合基本認証 – Avshalom

+0

はい。私の交換サーバーは私の広告の中にあります。 –

関連する問題