Solved

Powershell: Export-Csv gives only one result

Posted on 2014-03-31
3
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

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 35

Expert Comment

by:Dan Craciun
ID: 39968550
Glad I could help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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