使用C#在Exchange 2010上创建邮箱

时间:2017-02-09 12:08:33

标签: c# asp.net powershell exchange-server-2010

我正在尝试通过我的C#windows应用在Exchange 2010上创建用户邮箱。 这是我的代码

        PSCredential creds = new PSCredential("user id", StringToSecureString("password"));
        System.Uri uri = new Uri("http://servername/powershell?serializationLevel=Full");

        Runspace runspace = RunspaceFactory.CreateRunspace();

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();
        command.AddCommand("New-PSSession");
        command.AddParameter("ConfigurationName", "Microsoft.Exchange");
        command.AddParameter("ConnectionUri", uri);
        command.AddParameter("Credential", creds);
        command.AddParameter("Authentication", "Default");
        powershell.Commands = command;
        runspace.Open();
        powershell.Runspace = runspace;
        Collection<PSSession> result = powershell.Invoke<PSSession>();

        powershell = PowerShell.Create();
        command = new PSCommand();
        command.AddCommand("Set-Variable");
        command.AddParameter("Name", "ra");
        command.AddParameter("Value", result[0]);
        powershell.Commands = command;
        powershell.Runspace = runspace;
        powershell.Invoke();

        powershell = PowerShell.Create();
        command = new PSCommand();
        command.AddScript("Import-PSSession -Session $ra");
        powershell.Commands = command;
        powershell.Runspace = runspace;
        powershell.Invoke();

        powershell = PowerShell.Create();
        command.AddCommand("Invoke-Command");
        command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("New-Mailbox"));
        command.AddParameter("Name", "FnameLname");
        command.AddParameter("Password", "abcd@123");
        command.AddParameter("Alias", "FLname");
        command.AddParameter("OrganizationalUnit", "Company/OU");
        command.AddParameter("UserPrincipalName", "Fname.Lname@Domain.com");
        command.AddParameter("SamAccountName", "FLname");
        command.AddParameter("FirstName", "Fname");
        command.AddParameter("Initials", "FL");
        command.AddParameter("LastName", "Lname");
        command.AddParameter("Database", "dbname");
        command.AddParameter("LinkedMasterAccount", @"company\FLname");
        command.AddParameter("LinkedDomainController", "domain controller");
        powershell.Commands = command;
        powershell.Runspace = runspace;
        var createmailbox = powershell.Invoke();

但是当我执行上面的代码时,我得到以下错误。

  

找不到与参数名称匹配的参数&#39;名称&#39;

0 个答案:

没有答案