Solved

Problem exporting custom object to csv

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create a Shared Mailbox 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 Recipients >> Sha…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …

762 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

24 Experts available now in Live!

Get 1:1 Help Now