Solved

Powershell Script: Count items in multiple folders on multiple servers

Posted on 2016-09-28
10
62 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 12

Accepted Solution

by:
Dustin Saunders earned 250 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 250 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
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.

 

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 12

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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