powershellexchange-serverexchange-server-2010

EMS Powershell V2 how to append to CSV


I'm new to Powershell so please bear with me.

I'm trying to write a powershell script that will scan a number of mailboxes in Exchange 2010 and write the last date received to an existing CSV file. As the server is running Exchange 2010 it has PowerShell v2 so I cannot use the -Append parameter that is available in PS v3, so I was trying to use out-file and Add-Content, but both fail with the error listed below on the second run of the script. I assume that I'm either doing something wrong with the import or writing to the existing CSV file but I can't figure out what exactly.

# Path to the CSV file. This needs to be in place before the scrpt runs

$results = Import-CSV -Path 'C:\temp\numberofmails.csv' -Header "Mailbox","TotalItems","LastReceived",
    
# go through the mabList array and get the identity, number of items and last received date and time
    
Foreach($i in $mbxList){

$data = Get-MailboxFolderStatistics -Identity $i -IncludeOldestAndNewestItems | Where {$_.Name -match "Inbox"} |select identity,ItemsInFolder,NewestItemReceivedDate

$mbID=$data.identity

$numItems=$data.ItemsInFolder

$lastReceived=$data.NewestItemReceivedDate

#Append data to the CSV file using PS v2 so cannot use append as can be done in v3

$exp = $results

$exp.Mailbox = $mbID

$exp.TotalItems = $numItems

$exp.LastReceived = $LastReceived

 
#$exp | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1| Out-File -Append 'C:\temp\numberofmails.csv'

$exp | ConvertTo-CSV -NoTypeInformation | Select-Object -Skip 1|  Add-Content 'C:\temp\numberofmails.csv' -Encoding UTF8
}

Error received on the second run of the script.

Property 'Mailbox' cannot be found on this object; make sure it exists and is settable.

At C:\Temp\ReceivedItemsv2.ps1:29 char:6

+ $exp. <<<< Mailbox = $mbID

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

Property 'TotalItems' cannot be found on this object; make sure it exists and is settable.

At C:\Temp\ReceivedItemsv2.ps1:30 char:6

+ $exp. <<<< TotalItems = $numItems

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

Property 'LastReceived' cannot be found on this object; make sure it exists and is settable.

At C:\Temp\ReceivedItemsv2.ps1:31 char:6

+ $exp. <<<< LastReceived = $LastReceived
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException

+ FullyQualifiedErrorId : PropertyAssignmentException

Solution

  • $results is a collection of objects with Mailbox, TotalItems, and LastReceived properties. You can choose to add a new object to that collection or you can filter for one of its objects and change the values of its properties. It appears you want to create a new object, convert it to CSV, and append that to a text file.

    # Run this code inside of the foreach loop
    # Create new object with desired properties
    $exp = "" | Select-Object Mailbox,TotalItems,LastReceived
    
    # Set property values
    $exp.Mailbox = $mbID
    $exp.TotalItems = $numItems
    $exp.LastReceived = $LastReceived
    
    # Append object to CSV file
    $exp | ConvertTo-CSV -NoTypeInformation |
        Select-Object -Skip 1 |
            Add-Content 'C:\temp\numberofmails.csv' -Encoding UTF8