Solved

Powershell: Export-Csv gives only one result

Posted on 2014-03-31
3
777 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article will help you understand what HashTables are and how to use them in PowerShell.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

16 Experts available now in Live!

Get 1:1 Help Now