Solved

Powershell Script: Count items in multiple folders on multiple servers

Posted on 2016-09-28
10
37 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jacobJL
Comment Utility
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
Comment Utility
Try
Body = $results | Out-String

Open in new window

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

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Closing Comment

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

Author Comment

by:jacobJL
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That worked thank you. Understood Dustin. Next time I will open new question. You guys rock
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now