0

Need to create a Powershell v2 script to list mailbox details from Exchange 2010 server (mailbox name, email address, mailbox size) and import them into an MySQL database table.

I can get the Exchange information into Powershell but how can I then insert each piece of information into a different column in MySQL?

Such as: Mailbox name, email address, mailbox size etc.

Anyone know how to do this?

Adam Chetnik
  • 1,646
  • 5
  • 25
  • 35

1 Answers1

0

Here is a script you can edit to add email address and any other exchange exported powershell stuff into mysql

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$servers = Get-MailboxServer
foreach($server in $servers)
{   $mailboxes = Get-Mailbox -server $server -ResultSize unlimited
foreach ($m in $mailboxes)
{   $stats = Get-MailboxStatistics $m | Select ItemCount,DeletedItemCount,TotalItemSize,TotalDeletedItemSize
$date = Get-Date -Format "dd MMM yyyy h:mmtt"
$alias = $m.Alias
$items = $stats.ItemCount
$deletedItems = $stats.DeletedItemCount
$size = $stats.TotalItemSize.Value.ToMB()
$deletedSize = $stats.TotalDeletedItemSize.Value.ToMB()

$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection
$dbconnect.ConnectionString = "server=<SERVER>;port=3306;uid=<USERNAME>;pwd=<PASSWORD>;database=<DATABASE>;"
$dbconnect.Open()
$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $dbconnect
$sql.CommandText = "INSERT INTO <TABLE> (Timestamp,Alias,ItemCount,TotalItemSize,DeletedItemCount,TotalDeletedItemSize) values ('" + $date + "','" + $alias + "','" + $items + "','" + $size + "','" + $deletedItems + "','" + $deletedSize + "')"
$sql.ExecuteNonQuery()
$dbconnect.Close()
}
}
jimhendo
  • 1
  • 1