Solved

Powershell how to csv file output and error

Posted on 2011-03-15
6
562 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
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.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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