EmailAddresses are being saved as System.Object[]
hi guys,
i have written powershell code , requires tweaking. values being returned fine emailaddresses being coming system.object[] how can value outputted in csv format. appreciate if can review , point out error.
################################################################################## # powershell script mailbox data collection # navdeep www.alivebits.com # date 09 jan 2015 version 1.0 baseline # date 08 feb 2015 version 1.1 added mailbox statistics # date 29 june 2015 version 1.2 updated columns database exchdb, whendatacol # datacollectiondate, mailbox size return in mbs ################################################################################## # import modules needed script add-pssnapin microsoft.exchange.management.powershell.e2010 set-adserversettings -viewentireforest $true $startdate = get-date -format g $sheetname= get-date -format "dd mmmm yyyy" $sheetname.tostring() <# get-mailbox -resultsize unlimited | select displayname, primarysmtpaddress, @{name=“emailaddresses”;expression={$_.emailaddresses |where-object {$_.prefixstring -ceq “smtp”} | foreach-object {$_.smtpaddress}}}, samaccountname, servername, database, office, whenmailboxcreated,@{name="whendatacol"; expression={$startdate}}, organizationalunit | export-csv -path c:\users\exch2013\documents\mailboxreport\mailboxreport_$sheetname.csv #> $datapath = "c:\users\profile\documents\mailboxreport\mailboxreport_$sheetname.csv" $results = @() $mailboxusers = get-mailbox -resultsize unlimited foreach($user in $mailboxusers) { $upn = $user.userprincipalname $dn = $user.displayname $psmtp = $user.primarysmtpaddress $emailaddresses = $user.emailaddresses | where-object {$_.prefixstring -ceq “smtp”} | foreach-object {$_.smtpaddress} $saccname = $user.samaccountname $sn = $user.servername $db = $user.database $ou = $user.organizationalunit $office = $user.office $mailboxcreated = $user.whencreated if($user.prohibitsendquota -eq 'unlimited') { $quota=$user.prohibitsendquota } else { $quota=$user.prohibitsendquota.value.tomb() } $mbxstats = get-mailboxstatistics $upn $properties = @{ displayname = $dn office = $office organizationalunit = $ou primarysmtpaddress = $psmtp emailaddresses = $emailaddresses samaccountname = $saccname servername = $sn exchdb = $db prohibitsendquota = $quota totalitemcount = $mbxstats.itemcount totalitemsize = $mbxstats.totalitemsize.value.tomb() mailboxcreated = $mailboxcreated lastlogontime = $mbxstats.lastlogontime datacollectiondate = $startdate } $results += new-object psobject -property $properties } $results | select-object displayname,office,organizationalunit,primarysmtpaddress,emailaddresses,samaccountname,servername,exchdb,prohibitsendquota,totalitemsize,mailboxcreated,lastlogontime,datacollectiondate | export-csv -notypeinformation -path $datapath
regards, navdeep [a.k.a v-2nas]
i've seen plenty of times myself , end @ same article boe prox: http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/. need modify how you're setting $emailaddresses variable. i'd start trying this:
$emailaddresses = ($user.emailaddresses | where-object {$_.prefixstring -ceq "smtp"} | foreach-object {$_.smtpaddress}) -join ','edit: fixed smartquotes mentioned mike. thanks, mike!
Windows Server > Windows PowerShell
Comments
Post a Comment