Solved

appending content to existing csv file

Posted on 2014-01-27
9
3,565 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 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
 

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

23 Experts available now in Live!

Get 1:1 Help Now