?
Solved

Powershell Script: Count items in multiple folders on multiple servers

Posted on 2016-09-28
10
Medium Priority
?
134 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
[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
  • 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 13

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 13

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

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

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.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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