Solved

Problem exporting custom object to csv

Posted on 2014-01-31
4
1,867 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article explains how to install and use the NTBackup utility that comes with Windows Server.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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‚Ķ
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

828 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