0
votes

I'm trying to get an Exchange 2010 report from a multitenant platform. The thing is I need info obtained from different cmdlets. Our client is asking for DisplayName, MailboxPlan, PrimarySMTPAddress (Get-Mailbox), TotalItemSize and LastLogonTime (Get-MailboxStatistics)

I'm trying to do this using Powershell, but I'm getting an error... can you help me find out what's wrong?

Here's the script:

$TBMailbox = Get-Mailbox -Organization organization -ResultSize Unlimited | Select-Object Identity,DisplayName,MailboxPlan,PrimarySMTPAddress

ForEach ($Mbx in $TBMailbox) {$temp += ,(Get-MailboxStatistics -Identity $Mbx.Identity | Select $Mbx.Identity,$Mbx.DisplayName,$Mbx.MailboxPlan,$Mbx.PrimarySMTPAddress,TotalItemSize,LastLogonTime)}

$temp | Export-Csv -Path "C:\Path"

I'm Getting this error:

  • ForEach ($Mbx in $TBMailbox) {$temp += ,(Get-MailboxStatistics -Identity $Mbx.Identity | Select <<<< $Mbx.Identity,$Mbx.MailboxPlan,$Mbx.PrimarySMTPAddress,TotalItemSize,LastLogonTime)}
    • CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
    • FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand Select-Object : Cannot convert Microsoft.Exchange.Data.Directory.ADObjectId to one of the following types {System.String, System. Management.Automation.ScriptBlock}. At line:1 char:96

Any thoughts?

Update

Tried a different approach, but with the same result. I think the problem here is Select-Object:

Get-Mailbox -Organization organization -ResultSize Unlimited | ForEach-Object -Process {Select-Object $_.DisplayName,$_.MailboxPlan,$_.PrimarySMTPAddress,@{n=”Size(MB)”;e = {$MBXstat = Get-MailboxStatistics -Identity $_.Identity; $MBXstat.totalItemsize.value.toMB()}},@{n="LastLogonTime";e = {$MBXstat = Get-MailboxStatistics -Identity $_.Identity; $MBXstat.lastlogontime}}} | Export-Csv "C:\report.csv"
2
Update: I tried using Select and Select-Object -Property, and it fails with the same error messageEdoBarroso

2 Answers

0
votes

HINT: Cannot convert...ObjectId..to..one of the following types...System.String...

Don't use Select-Object, just Select Name, etc.

$TBMailbox = Get-Mailbox -Organization organization -ResultSize Unlimited | Select Identity,DisplayName,MailboxPlan,PrimarySMTPAddress I'll have to play with your "foreach" statement though, that's not how I would have written it, but that doesn't mean it wouldn't work.

0
votes

Finally I solved this using a Powershell Script. I'll share it here, in case somebody could make use of it:

# Script will get organization users and print it's name, email address, account usage and last logon time

$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition 
$dir = Split-Path $scriptpath 

#Variables to configure
$organization = "organization";

#No change needed from here!!!
$reportPath = "$dir\"
$reportName = "Report_$(get-date -format dd-MM-yyyy__HH-mm-ss).html";
$mbxReport = $reportPath + $reportName

$i = 0;
If (Test-Path $mbxReport)
    {
        Remove-Item $mbxReport
    }

#Loading Exchange CMDlets
. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto
cls

Write-Host "Obteniendo informacion de las Casillas..."
$mbxArray = Get-Mailbox -organization $organization -ResultSize Unlimited

$Total = $mbxArray.count
Write-Host "Se procesaran $Total casillas"

$titleDate = get-date -uformat "%d-%m-%Y"
$header = "
        <html>
        <head>
        <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
        <title>Reporte de usuarios - $organization</title>
        <STYLE TYPE='text/css'>
        <!--
            table {
                    border: thin solid #666666;
            }
        td {
            font-family: Tahoma;
            font-size: 11px;
            border-top: 1px solid #999999;
            border-right: 1px solid #999999;
            border-bottom: 1px solid #999999;
            border-left: 1px solid #999999;
            padding-top: 0px;
            padding-right: 0px;
            padding-bottom: 0px;
            padding-left: 0px;
        }
        body {
            margin-left: 5px;
            margin-top: 5px;
            margin-right: 0px;
            margin-bottom: 10px;
            table {
            border: thin solid #000000;
        }
        -->
        </style>
        </head>
        <body>
        <table width='100%'>
        <tr bgcolor='#CCCCCC'>
        <td colspan='7' height='25' align='center'>
        <font face='tahoma' color='#003399' size='4'><strong>Información de usuarios $Organization $titledate</strong></font>
        </td>
        </tr>
        </table>
"
 Add-Content $mbxReport $header
 $tableHeader = "
 <table width='100%'><tbody>
    <tr bgcolor=#CCCCCC>
    <td width='10%' align='center'>Usuario</td>
    <td width='5%' align='center'>Mail</td>
    <td width='15%' align='center'>Mailbox Plan</td>
    <td width='10%' align='center'>Tamaño (MB)</td>
    <td width='10%' align='center'>Ultimo Logon</td>
    </tr>
"
Add-Content $mbxReport $tableHeader
  foreach($mbx in $mbxArray)
    {   
    $Name = $mbx.Name
    $Plan = $mbx.MailboxPlan
    $Address = $mbx.PrimarySMTPAddress
    $Statistics = Get-MailboxStatistics -Identity $mbx.Identity

        foreach($stats in $Statistics)
    {        
        $Size = $stats.TotalItemSize.value.ToMB()
        $Logon = $stats.LastLogonTime

    $dataRow = "
        <tr>
        <td width='10%' align='center'>$Name</td>
        <td width='5%'>$Address</td>
        <td width='15%' align='center'>$Plan</td>
        <td width='10%' align='center'>$Size</td>
        <td width='10%' align='center'>$Logon</td>
        </tr>
"
Add-Content $mbxReport $dataRow;
$i++
Write-Host "Procesando $Address ($i de $Total)";
    }
}

Add-Content $mbxReport  "</body></html>"