Solved

Exchange 2007: Script to ouput multiple variables to comma-separated csv

Posted on 2011-10-01
4
615 Views
Last Modified: 2012-05-12
Hello guys,

Im having abit of trouble creating a powershell script that will post the following:

DisplayName,ItemCount,ItemSize,DatabaseName for all users on all Exchange servers in the organization, and exported to a comma-separated csv file. ItemSize preferably in KB/MB.

I will then use a phpscript to import info from the csv file and insert to a mysql db.

Here is my non-workikng code :)
 
$ExchangeServers = Get-ExchangeServer |where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true }
$AllServers = @()
foreach ($server in $ExchangeServers)
{
  $collect = Get-MailboxStatistics -Server $server | ft MailboxGuid
  foreach ($objItem in $collect)
  {

    $user = Get-MailboxStatistics -Identity $objItem | fl
    $name = $user.DisplayName
    $size = $user.TotalItemSize
    $count = $user.ItemCount
    $db = $user.DatabaseName

    $output = New-Object PSObject
    $output | Add-Member NoteProperty -Name "Name" -Value $name
    $output | Add-Member NoteProperty -Name "Size" -Value $size
    $output | Add-Member NoteProperty -Name "Count" -Value $count
    $output | Add-Member NoteProperty -Name "Database" -Value $db
    $AllServers += $output
  }
}


$AllServers | export-csv c:\test\output.csv

Open in new window


Any ideas?
0
Comment
Question by:jjugland
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
ID: 36899096
Try this, Mailbox stats is not great for a full mailbox list as some mailboxes won't have any values yet (i.e. not logged onto). This script uses Get-Mailbox for the user list, and then gets the mailbox stats of each user:

$ExchangeServers = Get-ExchangeServer | where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true}

$MasterArray = @()

foreach ($Server in $ExchangeServers)
{
	$Mailboxes = Get-Mailbox -Server $Server.Name -ResultSize "Unlimited"
	
	foreach ($Mailbox in $Mailboxes)
	{
		Write-Host -ForegroundColor "yellow" "Processing Mailbox - $($Mailbox.PrimarySMTPAddress)"
	
		$TempArray = @()
		$TempArray = "" | Select PrimarySMTP, DisplayName, DatabaseName, TotalItemSizeMB, ItemCount
	
		$MbxStats = Get-MailboxStatistics -Identity $Mailbox
		
		#If mailbox has MailboxStatistics then add the data
		if ($MbxStats -ne $Null)
		{
			[string]$TempArray.PrimarySMTP = $Mailbox.PrimarySMTPAddress
			[string]$TempArray.DisplayName = $Mailbox.DisplayName
			[string]$TempArray.DatabaseName = $Mailbox.Database
			[int]$TempArray.TotalItemSizeMB = $MbxStats.TotalItemSize.Value.ToMB()
			[int]$TempArray.ItemCount = $MbxStats.ItemCount
			
			$MasterArray += $TempArray
		}
		#Otherwise the TotalItemSize/ItemCount values are Null
		else
		{
			[string]$TempArray.PrimarySMTP = $Mailbox.PrimarySMTPAddress
			[string]$TempArray.DisplayName = $Mailbox.DisplayName
			[string]$TempArray.DatabaseName = $Mailbox.Database
			[int]$TempArray.TotalItemSizeMB = $Null
			[int]$TempArray.ItemCount = $Null
			
			$MasterArray += $TempArray
		}
		
		Remove-Variable -Name "MbxStats" -ErrorAction "SilentlyContinue"
	}
}

$MasterArray | Export-CSV "C:\Some Folder\Somefile.csv" -NoType

Open in new window

0
 

Author Comment

by:jjugland
ID: 36899630
Perfect! Just what i needed, many thanks for your assistance :-)
0
 

Author Closing Comment

by:jjugland
ID: 36899634
Just what i needed! Thanks alot :-)
0
 

Author Comment

by:jjugland
ID: 36899637
Woops, double post :X
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now