Kasper Katzmann
asked on
appending content to existing csv file
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
}
How do I get the output then?
Move the export-csv to outside the for-each loop.
Create a second file and concatenate.
ASKER
Moving the Export-Csv part outside the foreach loop will only take the last loop.
ASKER
I have tried to concatenate but I seem to fail on that part. Can you be more specific? Example (please)?
$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
See if this works for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looking at the script above, you are writing back to the same file you are reading from ?
$rows = Import-Csv c:\tools\bookingplan\booki ngplan.csv
Export-Csv -path c:\tools\bookingplan\booki ngplan.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) ?
$rows = Import-Csv c:\tools\bookingplan\booki
Export-Csv -path c:\tools\bookingplan\booki
I might make sense for the moment to just write to another file so you do not lose your source (as you resolve this) ?
ASKER
You are my new best friend :-)
It works like intended and I am happy. Thank you very much.
/Kasper
It works like intended and I am happy. Thank you very much.
/Kasper
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.