Solved

Problem exporting custom object to csv

Posted on 2014-01-31
4
1,642 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
  • 2
  • 2
4 Comments
 
LVL 39

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 39

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create a mailbox database 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 Servers >> Data…
This video discusses moving either the default database or any database to a new volume.

863 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

23 Experts available now in Live!

Get 1:1 Help Now