Solved

Powershell: Export-Csv gives only one result

Posted on 2014-03-31
3
814 Views
Last Modified: 2014-03-31
Hi,

I have this script that shows the shares that are published bij DFS.
I want to pipe the output to a csv file. There are more than 40 shares in DFS, but the script places only one result into the csv.
Only if I add an "-append"at the of the Export-Csv line, I get all the results.
The problem to that is, the csv is not overwritten.
Can someone help me on this ?

Clear-Host
$search= "department"
$dfsroots=Get-DfsnRoot
foreach ($root in $dfsroots) {
write-Host $root.NameSpacePath
$check=Get-DfsnFolder -Path "$($root.NamespacePath)\*" | Get-DfsnFolderTarget | Select-String $search foreach ($path in $check) {
$path=$path.ToString().Replace('?','$') -split "="
$path=$path.Split(',')
$shares=$path[3].Replace(')','')
#Out-File -filepath "C:\Temp\Shares.txt" -InputObject "$shares"
""|select-object @{Name="Shares";Expression={$shares}}|Export-Csv C:\Temp\shares.csv -NoTypeInformation -append 
}
}

Open in new window

0
Comment
Question by:Loyall
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39966640
The script behaves exactly as it should.
In the loop you get exactly one share, and you export that to the shares.csv file.

If you want to clear the file, just put this at the top:
Clear-Content C:\Temp\shares.csv

Open in new window

HTH,
Dan
0
 
LVL 2

Author Closing Comment

by:Loyall
ID: 39968500
Thanks a lot, Dan !
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39968550
Glad I could help!
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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article will help you understand what HashTables are and how to use them in PowerShell.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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