Solved

appending content to existing csv file

Posted on 2014-01-27
9
3,456 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
9 Comments
 
LVL 28

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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 28

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 28

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 28

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

18 Experts available now in Live!

Get 1:1 Help Now