?
Solved

Problem exporting custom object to csv

Posted on 2014-01-31
4
Medium Priority
?
2,250 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 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In this video we show how to create an email address policy 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 Mail Flow…
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

771 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