Solved

appending content to existing csv file

Posted on 2014-01-27
9
4,068 Views
Last Modified: 2015-06-04
I have a lot of rooms in our Exchange 2010 setup and I have been asked to produce a list with all rooms and the persons that are ResourceDelegates and are listed in BookinPolicy. I have come this far, but the problem is, that I only have Powershell V2 and -append is a V3 feature.

$rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox

ForEach ($room in $rooms) {

$mRoom = $room.name

$rows = Import-Csv c:\tools\bookingplan\bookingplan.csv

$cont = Get-CalendarProcessing $mRoom | `
		Select Identity, `
		@{Name=’Bookinpolicy’;Expression={[string]::join(";", ($_.BookInPolicy))}}, `
		@{Name=’ResourceDelegates’;Expression={[string]::join(";", ($_.ResourceDelegates))}} | `
			Export-Csv -append -path c:\tools\bookingplan\bookingplan.csv -Encoding Unicode
}

Open in new window

How do I get the output then?
0
Comment
Question by:Kasper Katzmann
[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
9 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39812033
Move the export-csv to outside the for-each loop.
0
 
LVL 7

Expert Comment

by:Steve
ID: 39812034
Create a second file and concatenate.
0
 

Author Comment

by:Kasper Katzmann
ID: 39812096
Moving the Export-Csv part outside the foreach loop will only take the last loop.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kasper Katzmann
ID: 39812097
I have tried to concatenate but I seem to fail on that part. Can you be more specific? Example (please)?
0
 
LVL 29

Expert Comment

by:becraig
ID: 39812115
$rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox

ForEach ($room in $rooms) {

$mRoom = $room.name

$rows = Import-Csv c:\tools\bookingplan\bookingplan.csv

$cont += Get-CalendarProcessing $mRoom | `
		Select Identity, `
		@{Name=’Bookinpolicy’;Expression={[string]::join(";", ($_.BookInPolicy))}}, `
		@{Name=’ResourceDelegates’;Expression={[string]::join(";", ($_.ResourceDelegates))}} 
} $cont | Select * | Export-Csv -append -path c:\tools\bookingplan\bookingplan.csv -Encoding Unicode

Open in new window




See if this works for you.
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 39812134
Oops I forgot to declare the array :(
[array]$cont=$null
$rooms = Get-Mailbox -RecipientTypeDetails RoomMailbox
ForEach ($room in $rooms) {
$mRoom = $room.name
$rows = Import-Csv c:\tools\bookingplan\bookingplan.csv
$cont += Get-CalendarProcessing $mRoom | `
		Select Identity, `
		@{Name=’Bookinpolicy’;Expression={[string]::join(";", ($_.BookInPolicy))}}, `
		@{Name=’ResourceDelegates’;Expression={[string]::join(";", ($_.ResourceDelegates))}} 
} $cont | Select * | Export-Csv -path c:\tools\bookingplan\bookingplan.csv -Encoding Unicode 

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 39812308
Looking at the script above, you are writing back to the same file you are reading from ?

$rows = Import-Csv c:\tools\bookingplan\bookingplan.csv

Export-Csv -path c:\tools\bookingplan\bookingplan.csv

I might make sense for the moment to just write to another file so you do not lose your source (as you resolve this)  ?
0
 

Author Closing Comment

by:Kasper Katzmann
ID: 39812411
You are my new best friend :-)
It works like intended and I am happy. Thank you very much.

/Kasper
0
 
LVL 1

Expert Comment

by:ncfbins
ID: 40812871
I know this is old, but its exactly what I am looking for. I am having issues running this script. It doesnt seem to create the CSV file before it tries to append to it.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

690 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