Solved

Powershell Script: Count items in multiple folders on multiple servers

Posted on 2016-09-28
10
101 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 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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