Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell how to csv file output and error

Posted on 2011-03-15
6
Medium Priority
?
570 Views
Last Modified: 2012-05-11
Guys I have this script which works but need 2 simple things but it has turned complex. I cannot get any output to write to this directory from the file. I checked to see if I have perms and I do. I would like each time the script goes through each fileshare to write the output to a excel spreadsheet or comma seperated value. If I could I would like the actual output.

total files, total directories, path fileshare

Please help and if possible can someone show me how to write standard error to a file. I seem to not able to get this working also.

get-content "c:\temp\fileshares.txt" |
foreach-object {
$filez = (get-childitem $_. -recurse -force | ?{!($_.PSIsContainer)}).Count
$directories = (get-childitem $_. -recurse -force | ?{$_.PSIsContainer}).Count

write-host " Here are the totals for $_. share"
write-host " This fileshare has $filez files and $directories folders" |
export-csv c:\temp\results.csv -noTypeInformation -ErrorAction SilentlyContinue

}
0
Comment
Question by:techdrive
[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
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35138643
Try this one.
get-content "c:\temp\fileshares.txt" | 
foreach-object {
$filez = (get-childitem $_ -recurse -force | ?{!($_.PSIsContainer)}).Count
$directories = (get-childitem $_ -recurse -force | ?{$_.PSIsContainer}).Count

write-host " Here are the totals for $_ share"
write-host " This fileshare has $filez files and $directories folders" 

New-Object -TypeName PSObject -Property @{
    TotalFiles = $filez
    TotalDirectories = $directories
    PathFileShare = $_
}
} | export-csv c:\temp\results.csv -noTypeInformation -ErrorAction SilentlyContinue

Open in new window

0
 

Author Comment

by:techdrive
ID: 35139187
soostibi the script you posted is currently running and have been running for the last 30 minutes. The script I posted only took a matter of at most 30 seconds to run. I have not stopped it but its still running.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35139257
If any of the share paths are very deep, it can take significant amount of time until it finishes. Try first on few, simple, shallow shares. If you get the right results I can make the code more efficient.

In your code you used '$_.', in which the period sign makes the whole thing work completely wrong, that is why your code finishes fast.
0
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 

Author Comment

by:techdrive
ID: 35139338
Ok I was able to run it locally pretty fast. The output is beautiful. If you could tweak it I would appreciate it very much and thanks.
0
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 35139518
This version crawls the folder structure only once, so it should be faster.

But if you run the script against remote shares, you'd better create a client-server approach, and do the crawling on the same machine as the share resides and collect every result from your workstation, but this - I think - is out of the scope of the original question.
get-content "c:\temp\fileshares.txt"  | 
foreach-object {
$stat = Get-ChildItem $_ -Recurse -Force | 
    Select-Object @{n="Type"; e={if($_.psiscontainer){"directory"}else{"file"}}} | 
        Group-Object -NoElement -Property type
if($stat[0].name -eq "Directory"){
    $filez = $stat[1].count
    $directories = $stat[0].count
}
else {
    $filez = $stat[0].count
    $directories = $stat[1].count
}

write-host " Here are the totals for $_ share"
write-host " This fileshare has $filez files and $directories folders" 

New-Object -TypeName PSObject -Property @{
    TotalFiles = $filez
    TotalDirectories = $directories
    PathFileShare = $_
}
} | export-csv c:\temp\results.csv -noTypeInformation -ErrorAction SilentlyContinue

Open in new window

0
 

Author Closing Comment

by:techdrive
ID: 35139626
Very fast and knowledgable
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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