Solved

Problem exporting custom object to csv

Posted on 2014-01-31
4
2,012 Views
Last Modified: 2014-01-31
Trying to pull name from Get-Mailbox and a few properties from Get-ActiveSyncDeviceStatistics into a custom object and export to a csv.



$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('yyyy-MM-dd_hh-mm-ss')
$UserInfo = @{}
$UserList = get-casmailbox -Filter {HasActivesyncDevicePartnership -eq $true -and -not DisplayName -like "CAS_{*"} | Get-Mailbox 
Foreach ($User in $UserList)
{
 $Devices = Get-ActiveSyncDeviceStatistics -Mailbox $User.Alias
 Foreach ($Device in $Devices)
 {
	
   $UserInfo += [pscustomobject]@{Name=$User.alias;DeviceType=$Device.DeviceType;FirstSync=$Device.FirstSyncTime;LastSync=$Device.LastSuccessSync}
  }
}
$UserInfo | Export-CSV -path c:\Scripts\EAS\UserDevices_$CurrentDate.csv -NoTypeInformation

Open in new window


Data exported to Excel 2010
EMS output when  = is used.
EMS output when "$UserInfo   " is usedThis pick shows that I am getting the info needed.

In all cases the eport to csv is the same.
0
Comment
Question by:pmetech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:footech
ID: 39824863
Try this.  It might be enough to just modify line 3, but I also changed line 11.
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('yyyy-MM-dd_hh-mm-ss')
$UserInfo = @()
$UserList = get-casmailbox -Filter {HasActivesyncDevicePartnership -eq $true -and -not DisplayName -like "CAS_{*"} | Get-Mailbox 
Foreach ($User in $UserList)
{
 $Devices = Get-ActiveSyncDeviceStatistics -Mailbox $User.Alias
 Foreach ($Device in $Devices)
 {
	
   $UserInfo += $device | Select @{n="Name";e={$User.alias}},DeviceType,@{n="FirstSync";e={$_.FirstSyncTime}},@{n="LastSync";e={$_.LastSuccessSync}}
  }
}
$UserInfo | Export-CSV -path c:\Scripts\EAS\UserDevices_$CurrentDate.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:pmetech
ID: 39824946
Thanks for the quick replay

If I run it with both line changes I receive the following although it does create and fill the CSV with the correct data.
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\scripts\EAS\UserDevices1.ps1:14 char:23
+ $UserInfo | Export-CSV <<<<  -path c:\Scripts\EAS\UserDevices_$CurrentDate.csv -NoTypeInformation
    + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
   mmand

Open in new window


If I only change line 3 I receive the same CSV with addtional row filled.  Same amount of rows as the run with both line changes.
CSV file
Any ideas on the error?
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39825052
Unfortunately I can't test but one of the elements of the $userinfo array must be null.  Probably due to the results (or lack thereof) in either $Devices or $UserList.  If you can find it and keep it from being added to $userinfo, that would be good, but you could also filter out any nulls at the end.
$UserInfo | Where {$_}| Export-CSV -path c:\Scripts\EAS\UserDevices_$CurrentDate.csv -NoType

Open in new window

0
 

Author Closing Comment

by:pmetech
ID: 39825116
Thanks for the quit help!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
how to add IIS SMTP to handle application/Scanner relays into office 365.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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