Solved

Powershell how to csv file output and error

Posted on 2011-03-15
6
568 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
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.

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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 outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

630 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