Part 9 Create The Email Account

Create the class that will create the user’s email account.

Once again, this example uses the AresAdmin account used in the ChangePassword.cs class.

The AresAdmin account will need edit permission to your exchange system.

Copy the method used to create the Globals.cs class but this time call it ‘Enable_Mailbox.cs’.

Change the code inside Enable_Mailbox.cs to the following:

using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Runtime.InteropServices;

namespace Ares
{
class Enable_Mailbox
{

public void EnableMailbox(string identityVar)
{
try
{
#region retrieve securestore credentials

try
{
var securePwd = new SecureString();
securePwd.AppendChar(‘1’);
securePwd.AppendChar(‘2’);
securePwd.AppendChar(‘3’);
securePwd.AppendChar(‘4’);
securePwd.AppendChar(‘5’);
securePwd.AppendChar(‘6’);
securePwd.AppendChar(‘7’);
securePwd.AppendChar(‘8’);
securePwd.AppendChar(‘9’);
securePwd.AppendChar(‘0’);
securePwd.AppendChar(‘1’);
securePwd.AppendChar(‘2’);
securePwd.AppendChar(‘3’);
securePwd.AppendChar(‘4’);
securePwd.AppendChar(‘5’);

PSCredential UserCredential = new System.Management.Automation.PSCredential(“AcreAdmin”, securePwd);

#region Set the connection info

RunspaceConfiguration rc = RunspaceConfiguration.Create();

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(“http://<exchange server>/powershell?serializationLevel=Full”), “http://schemas.microsoft.com/powershell/Microsoft.Exchange”, UserCredential);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

#endregion

using (Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo))

{
try
{
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand(“Enable-Mailbox”);
command.AddParameter(“Identity”, identityVar);
command.AddParameter(“Database”, Globals.exchangeDB);

powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
powershell.Invoke();
}
catch (Exception usEx)
{
}
}
rc = null;
}
catch (Exception exsec)
{
}
#endregion
}
catch (Exception Implicit_Ex)
{
}
}

private static string GetStringFromSecureString(SecureString secStr)
{
if (secStr == null)
{
return null;
}

IntPtr pPlainText = IntPtr.Zero;

try
{
pPlainText = Marshal.SecureStringToBSTR(secStr);
return Marshal.PtrToStringBSTR(pPlainText);
}
finally
{
if (pPlainText != IntPtr.Zero)
{
Marshal.FreeBSTR(pPlainText);
}
}
}
}
}

About the author: Author

Leave a Reply