Solved

Powershell how to csv file output and error

Posted on 2011-03-15
6
561 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
  • 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

25 Experts available now in Live!

Get 1:1 Help Now