?
Solved

Powershell Script: Count items in multiple folders on multiple servers

Posted on 2016-09-28
10
Medium Priority
?
228 Views
Last Modified: 2016-09-30
Hi experts. I am trying to get the count of files in a number of folders on multiple servers.
Example: I want to get the count of items in subfolderB in each folder on each server. They all reside in the same path on each server the folder structure names all the same. I only want subfolderB in every folder.
Server1
    folder1
       subfolderA
       subfolderB
    folder2
       subfolderA
       subfolderB

Server2
    folder1
       subfolderA
       subfolderB
    folder2
       subfolderA
       subfolderB

This is what i started with. First i have the array of servers pulled in from a text file. Then i put in a exact location of one of the subfolders. This did pull counts for that one location on all servers. but like i said i need counts for all subfolder B's on each server in each folder.
$results = New-Object System.Collections.ArrayList
$Computers = Get-Content D:\NCR\DAX\Scripts\servers.txt

Foreach ($Computer in $Computers)
{
    set-location "\\$computer\D$\ProdData\Data\Administration\Dump"
   
        $file = Get-ChildItem


        $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'Size'=($file | Get-ChildItem).count}
   


 }
$results
0
Comment
Question by:jacobJL
  • 5
  • 3
  • 2
10 Comments
 

Author Comment

by:jacobJL
ID: 41820441
Sorry looking for results of something like this

server      location                              Count
server1      folder1\subfolderB      200
server1      folder2\subfolderB      300
server1      folder3\subfolderB      2
server2      folder1\subfolderB      59
server2      folder2\subfolderB      29
server2      folder3\subfolderB      74
server3      folder1\subfolderB      1000
server3      folder2\subfolderB      4950
server3      folder3\subfolderB      1290
0
 
LVL 14

Accepted Solution

by:
Dustin Saunders earned 1000 total points
ID: 41820477
Are the root folders always the same?  (Folder1, Folder2, Folder3)?

If so you can make another list and get it like you do the computers:
$Folder= Get-Content D:\NCR\DAX\Scripts\folders.txt

Open in new window


Then nest your foreach loops, like this:

foreach ($computer in $computers)
{
    foreach ($folder in $folders)
    {
        $path = "\\$computer\D$\$folder"
        $file = Get-ChildItem -Path $path
        $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'Size'=($file | Get-ChildItem).count}
    }
}

Open in new window

0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1000 total points
ID: 41820487
Assuming all your folders are under D:\ drive on servers and the folder you want to look is Dump
$results = New-Object System.Collections.ArrayList
$Computers = Get-Content D:\NCR\DAX\Scripts\servers.txt
Foreach ($Computer in $Computers) 
{
  GCI "\\$computer\D$" -Recurse | ?{ $_.PSIsContainer -and $_.name -eq "Dump"}| % {
     $Count = (Get-ChildItem $_.FullName -Recurse | Measure).Count
     $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$_.FullName;'Size'=$Count}
   }
 }
$results

Open in new window


Script will look all folders named Dump under D:\ drive of server and list recursive file count as output..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jacobJL
ID: 41820549
Awesome with a little modifications thats exactly what i was looking for. here is my final output. Could you help me with one last step please. I would like to email the results. I get an error when running below.

$results = New-Object System.Collections.ArrayList
$Computers = Get-Content D:\NCR\DAX\Scripts\servers.txt
$folders= Get-Content D:\NCR\DAX\Scripts\folderNames.txt

Foreach ($Computer in $Computers)
{
   foreach ($folder in $folders)
    {
        $path = "\\$computer\D$\ProdData\Data\$folder\Dump"
        $file = Get-ChildItem -Path $path
        $results += New-Object psObject -Property @{'Server'=$computer;'Location'=$path; 'Count'=($file | Get-ChildItem).count}
    }

 }
$results


    $email = @{
                From = "abc@gmail.com"
                To = "deb@gmail.com"
                Subject = "Dumped Records"
                SMTPServer = "rc-ex01.msl.local"
                Body = $results
              }

    send-mailmessage @email

This is the error i get.
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At D:\NCR\DAX\Scripts\CheckDumpFiles.ps1:26 char:22
+     send-mailmessage @email
+                      ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41820560
Try
Body = $results | Out-String

Open in new window

Or for  HTML body..
BodyAsHtml = $results | ConvertTo-Html | Out-String

Open in new window

0
 

Author Closing Comment

by:jacobJL
ID: 41820580
Thank you guys so much for the help. Greatly appreciated!
0
 

Author Comment

by:jacobJL
ID: 41823447
Sorry guys, i am getting an error here. Let me know if i should open another question. 2 things. I can't send and email to 2 people for some reason. It only sends the last person.

$recipients = @("abc@gmail.com","123@gmail.com")
    $email = @{
                From = "noname@gmail.com"
                To = "$recipients"
                Subject = "Dumped Records"
                SMTPServer = "wmvpexg.markets.com"
                Body = $results | Out-String
              }

    send-mailmessage @email


And then also when i change Body to:
BodyAsHtml = $results | ConvertTo-Html | Out-String
I get below error

Send-MailMessage : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'BodyAsHtml'.
At D:\NCR\DAX\Scripts\CheckDumpFiles.ps1:26 char:22
+     send-mailmessage @email
+                      ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41823502
Try..
$recipients = "abc@gmail.com","123@gmail.com"
     $email = @{
                 From = "noname@gmail.com"
                 To = $recipients
                 Subject = "Dumped Records"
                 SMTPServer = "wmvpexg.markets.com"
                 Body = $results | ConvertTo-Html | Out-String
                 BodyAsHtml = $True
               }

     send-mailmessage @email

Open in new window

0
 
LVL 14

Expert Comment

by:Dustin Saunders
ID: 41823510
As an FYI, yes generally you would open a new question (as this is a different problem than the original one), but if you post a new question on an already resolved posting other Experts won't be able to see that there is a follow up and you may not get a response.
1
 

Author Comment

by:jacobJL
ID: 41823661
That worked thank you. Understood Dustin. Next time I will open new question. You guys rock
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
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 anti-spam), the admin…
Loops Section Overview

850 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