[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

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

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
jjugland
Asked:
jjugland
  • 3
1 Solution
 
chrismerrittCommented:
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
 
jjuglandAuthor Commented:
Perfect! Just what i needed, many thanks for your assistance :-)
0
 
jjuglandAuthor Commented:
Just what i needed! Thanks alot :-)
0
 
jjuglandAuthor Commented:
Woops, double post :X
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now