Tag Archives: Export

Export Exchange Audit logs

One of the most powerfull feature of Exchange logs is the possibility to audit for example all emails send from the source domain: domain.com every day and send a mail to some user, this is one sample scrit that colects information and sends a mail:

$file= "c:\prova.csv" 
$mailboxdata= Get-MessageTrackingLog -Start "10/14/2013 00:00:00"| Where-Object {$_.sender -like "*@domain.com"} | Select-Object eventid,sender,timestamp,@{Name="Recipients";Expression={$_.recipients}},@{Name="RecipientStatus";Expression={$_.recipientstatus}},messagesubject $mailboxdata | export-csv "$file" 
$smtpServer = "127.0.0.1" 
$att = new-object Net.Mail.Attachment($file) 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
$msg.From = "sender@domain.com" 
$msg.To.Add("dest1@domain.com") 
$msg.To.Add("dest2@domain.com")
$msg.Subject = "Emails Summary" 
$msg.Body = "Attached is the email server mailbox report" 
$msg.Attachments.Add($att) 
$smtp.Send($msg) 
$att.Dispose()
Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

Export MSSQL information to Exchange public folder

This is a small example of how to collect the contact (in this case a table of Microsoft Dynamics Navision MSSQL) in a public folder Exchange contacts “test”,to do this there are several methods, in Exchange 2007 and Exchange 2010 sp1 is better to use EWS (Exchange Web sevices), but Exchange 2003 does not have that feature, alternative ways: (WEBDAV, COM, DLL from third parties, etc. .. .)
The easiest way I found is using the COM reference Microsoft Outlook 12.0 within the proposed visual studio 2008 in c #, not if it is the best and the fastest, the advantage is that it is compatible with all versions of Exchange.
 

try
{
	timer1.Enabled = false;
	int a,rpcCount;
	lblmis.Text = "Sincronizando Contactos...";
	Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
	NameSpace oNS = oApp.GetNamespace("MAPI");
	Registre reg = new Registre();
	MAPIFolder oAllPublicFolders; //what it says on the tin
	MAPIFolder oPublicFolders; // as above
	MAPIFolder objContacts; //as above
	//search the folder test in public folders
	oPublicFolders = oNS.Folders["Carpetas públicas"];
	oAllPublicFolders = oPublicFolders.Folders["Todas las carpetas públicas"];
	objContacts = oAllPublicFolders.Folders["test"];
	 
	Items oItems = objContacts.Items;
	 
	a = oItems.Count;
	progressBar1.Maximum = a;
	progressBar1.Value = 0;
	// search contacts from a table from MSSQL :
	string sql;
	SqlDataAdapter da;
	DataTable DTsql = new DataTable();
	DataSet ds;
	ds = new DataSet();
	string dsn = "server=" + servidor + ";database=" + bbdd + ";uid=" + usuari + ";pwd=" + password;
	SqlConnection myconnection = new SqlConnection(dsn);
	myconnection.Open();
	sql = "SELECT * FROM [" + empresa + "$Contact] ";
	da = new SqlDataAdapter(sql, myconnection);
	da.Fill(DTsql);
	 
	//delete non existents ( the primary key of the contact is "CustomerID") in my case
	lblmis.Text = "Deleting non existents..... ";
	DTsql.PrimaryKey = new DataColumn[] { DTsql.Columns["No_"] };
	progressBar1.Maximum = a;
	a = 0;
	rpcCount = 0;
	progressBar1.Value = 0;
	foreach (ContactItem coi in oItems)
	{
		if (coi.CustomerID.ToString().Trim() != "")
		{
			DataRow resul= DTsql.Rows.Find(coi.CustomerID.ToString());
			if (resul == null)
			{
				lblmis.Text = "deleting: " + coi.FirstName.ToString();
				coi.Delete();
			}
		resul = null;
		}
		else
		{
			coi.Delete();
		}
		rpcCount++;
		// the next is necessary if you use outllook in cached mode ( to solve the error only 250 items can be open error)
		if (rpcCount > 50)
		{
			lblmis.Text = "Borrant no existents..... ";
			rpcCount = 0;
			//force garbage collection and see if that decrements open message counter on exchange server.  We have used both a countdown loop as shown here, and also during each single loop.
			GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();
			GC.WaitForPendingFinalizers();  //tried a second one also based on the articles
		}
		System.Windows.Forms.Application.DoEvents();
		lbltemps.Text = progressBar1.Value.ToString();
		progressBar1.Value = progressBar1.Value + 1;
	}
	//update or Add existents ( the primary key of the contact is "CustomerID") in my case
	a = 0;
	rpcCount = 0;
	if (DTsql.Rows.Count > 0)
	{
		progressBar1.Maximum = DTsql.Rows.Count;
		progressBar1.Value = 0;
		// Do for each row in the sql result
		foreach (DataRow dr in DTsql.Rows)
		{
			lbltemps.Text = progressBar1.Value.ToString();
			lblmis.Text = dr["Name"].ToString();
			System.Windows.Forms.Application.DoEvents();
			// set the first and last name to search for
			string sFirstName = dr["No_"].ToString();
			// field name to search in [] and the value to search in ''.
			string sSearch = String.Format("[CustomerID]='{0}' ", sFirstName);
			//busquem:
			ContactItem contact = (ContactItem)oItems.Find(sSearch);
			 
			if (contact != null)
			{
			// Contact found, update
			if (altes.Trim() != "1")
				{
				contact.FirstName = dr["Name"].ToString().Replace("'", "´").ToString();
				contact.LastName = "";
				contact.Email1Address = dr["E-Mail"].ToString();
				contact.MailingAddressStreet = dr["Address"].ToString().Replace("'", "´").ToString() + "\n " + dr["Address 2"].ToString().Replace("'", "´").ToString();
				contact.MailingAddressCity = dr["City"].ToString();
				contact.MailingAddressPostalCode = dr["Post Code"].ToString();
				contact.PrimaryTelephoneNumber = dr["Phone No_"].ToString();
				contact.HomeFaxNumber = dr["Fax No_"].ToString();
				contact.TelexNumber = dr["Telex No_"].ToString();
				contact.Email2Address = dr["E-Mail 2"].ToString();
				contact.MailingAddressState = dr["County"].ToString();
				contact.CompanyName = dr["Company Name"].ToString();
				System.Windows.Forms.Application.DoEvents();
				contact.Save();
				 
				}
			}
			else
			{
				//add contact
				ContactItem newContact = (ContactItem)objContacts.Items.Add(OlItemType.olContactItem);
				newContact.FirstName = dr["Name"].ToString().Replace("'", "´").ToString();
				newContact.LastName = "";
				newContact.Email1Address = dr["E-Mail"].ToString();
				newContact.MailingAddressStreet = dr["Address"].ToString().Replace("'", "´").ToString() + "\n " + dr["Address 2"].ToString().Replace("'", "´").ToString(); ;
				newContact.MailingAddressCity = dr["City"].ToString();
				newContact.MailingAddressPostalCode = dr["Post Code"].ToString();
				newContact.PrimaryTelephoneNumber = dr["Phone No_"].ToString();
				newContact.HomeFaxNumber = dr["Fax No_"].ToString();
				newContact.TelexNumber = dr["Telex No_"].ToString();
				newContact.Email2Address = dr["E-Mail 2"].ToString();
				newContact.MailingAddressState = dr["County"].ToString();
				newContact.CompanyName = dr["Company Name"].ToString().Replace("'", "´").ToString() ;
				newContact.CustomerID = dr["No_"].ToString();
				System.Windows.Forms.Application.DoEvents();
				newContact.Save();
				//newContact.Close(OlInspectorClose.olDiscard );
				 
				//newContact.Display(true);
			}
			a = a + 1;
			progressBar1.Value = a;
			rpcCount++;
			//the next is necessary if you use outllook in cached mode ( to solve the error only 250 items can be open error)
			if (rpcCount > 50)
			{
				rpcCount = 0;
				//force garbage collection and see if that decrements open message counter on exchange server.  We have used both a countdown loop as shown here, and also during each single loop.
				GC.Collect();
				GC.WaitForPendingFinalizers();
				GC.Collect();
				GC.WaitForPendingFinalizers();  //tried a second one also based on the articles
			}
	 
		}
	}
	 
	progressBar1.Value = 0;
	segons = reg.Read("segons").ToString();
	lblmis.Text = "Esperant...";
	this.Close();
}
catch (SqlException er)
{
	MessageBox.Show(er.Message, "SQL Error");
	this.Close();
}
catch (System.Exception  er)
{
	MessageBox.Show(er.Message, "General Error");
	this.Close();
 
}
Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

Copy Exchange 2007 Mailboxes to pst

Many people prefer to have a copy of the exchange mailboxes in a format much easier to treat or recover, this is the end of this script, which generates the files pst exported from Microsoft Exchange mailboxes, keep in mind that it has to work in a X32 machine with

  • outlook installed ( mapi32.dll )
  • have the exchange management tools installed.
  • Powershell isntalled

This tested in an environment of Exchange 2007 (x64 server) running on a pc with windows server 2003 x32.
consists of the following, a file script.ps1

get-mailbox | export-mailbox -PSTFolderPath:'X:\Dades Exchange\psts' -Confirm:$false
in this case, we will export all the mailboxes, we can filter with get-mailbox and the paremeter filter if we want to export only some.
 
and the export.bat to schedule the execution:
powershell.exe -PSConsoleFile "C:\Archivos de programa\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command ". 'C:\script export pst\script.ps1'"

simply call the powershell script to run,
pd: if we run it on a X64 server not work

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

Export mailboxes to pst Exchange 2010

This little script if for export mailboxes to a pst files form an Exchange 2010 SP1 or SP2  or SP3

to change:

  • e:\exchange –>path to export pst files (\\server2k\e$\Exchange)
  • F:\Exchange\bin ->path to exe of Exchange
  • F:\backup –>path to ps script

first, assing role to users administrator

New-ManagementRoleAssignment –Role "Mailbox Import Export" –User Administrator

 the script:

</p>
'F:\Exchange\bin\RemoteExchange.ps1';
Connect-ExchangeServer -auto;
Remove-Item <a href="file:///\\murano\F\Exchange\*.pst">\\murano\F\Exchange\*.pst</a> -recurse;
Get-MailboxExportRequest | where {$_.status -eq "Completed"} | Remove-MailboxExportRequest -confirm:$false;
Get-MailboxExportRequest | where {$_.status -eq "Failed"} | Remove-MailboxExportRequest -confirm:$false;
foreach ($i in (Get-Mailbox))
	{ New-MailboxExportRequest -Mailbox $i -FilePath "<a href="file:///\\murano\F\Exchange\$($i.Alias).pst">\\murano\F\Exchange\$($i.Alias).pst</a>"  -baditemlimit 200 -AcceptLargeDataLoss }

then, create one new scheduled task:

 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

with this parameters:

 

-version 2.0  -command “. ‘F:\Exchange\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto;. ‘F:\backup\exportmailboxes.ps1′”

{jcomments on}

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...