Solved

Powershell how to csv file output and error

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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