Solved

appending content to existing csv file

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

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 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