Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to find out size of specific folders from a list of servers

Posted on 2013-01-08
5
Medium Priority
?
408 Views
Last Modified: 2013-01-16
We need to find the size of two folders from a list of file servers. The problem is they may me mounted on any of the volume on the servers. So no consistency as such.
Only thing that is consistent is the names of two folders.
Example- on all 100 file servers  we need to list size of A and B which are sub folders under shared drive  C  and C is on  a volume (volume name may be anything).

Powershell or VBscript anything can work for me.
0
Comment
Question by:sydneyithelpdesk
[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
  • 2
  • 2
5 Comments
 

Author Comment

by:sydneyithelpdesk
ID: 38758294
Making this bit easy - C is a shared folder and accessible from any computer so we can forget about which volume it resides on.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38758406
Hi, give this script a shot.

Regards,

Rob,

strInputFile = "servers.txt"
strOutputFile = "foldersize.csv"
strFolderShare = "C$"
arrSubFolders = Array("Temp", "Users")

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading, True)
Set objOutput = objFSO.CreateTextFile(strOutputFile, True)
objOutput.WriteLine """Server"",""" & Join(arrSubFolders, """,""") & """"
While Not objInput.AtEndOfStream
	strComputer = Trim(objInput.ReadLine)
	If strComputer <> "" Then
		If Ping(strComputer) = True Then
			objOutput.Write """" & strComputer & """"
			For Each strSubFolder In arrSubFolders
				strFolderPath = "\\" & strComputer & "\" & strFolderShare & "\" & strSubFolder
				If objFSO.FolderExists(strFolderPath) = False Then
					objOutput.Write ",""<NOT FOUND>"""
				Else
					On Error Resume Next
					Err.Clear
					strSize = 0
					Set objFolder = Nothing
					Set objFolder = objFSO.GetFolder(strFolderPath)
					If Err.Number = 0 Then
						strSize = objFolder.Size/1024/1024
						If Err.Number = 0 Then
							objOutput.Write ",""" & strSize & """"
						Else
							objOutput.Write ",""" & Err.Number & ": " & Err.Description & """"
						End If
					Else
						objOutput.Write ",""" & Err.Number & ": " & Err.Description & """"
					End If
					Err.Clear
					On Error Goto 0
				End If
			Next
			objOutput.WriteLine
		Else
			objOutput.WriteLine """" & strComputer & """<OFFLINE>"",""<OFFLINE>"""
		End If
	End If
Wend
objInput.Close
objOutput.Close

WScript.Echo "Done"

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Comment

by:sydneyithelpdesk
ID: 38758506
Thanks Rob! I was already following one of your article
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27989367.html#a38758294

Here If i try to run this script Result says "Not found"

I changed third line to "Ndrive$" however as it is a shared folder on every server does this require $ with it.
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 750 total points
ID: 38759090
If you are interested in PowerShell script..
C:\server.txt is the input file..
The script will search for directories subsun and PerfLogs in server's C:\ drive (Check line number 6 and 7 to change it)..
[Array]$Result = $null
GC C:\server.txt | % {
$Server = $_
If (Test-Connection $Server -Count 2 -ErrorAction SilentlyContinue) {
 If (Get-PSDrive z -ErrorAction SilentlyContinue) {Remove-PSDrive -Name z}
  If (New-PSDrive -Name z -PSProvider FileSystem -Root \\$Server\C$ -ErrorAction SilentlyContinue) { 
    $Folders = Get-ChildItem z:\ -Recurse | ? {$_.PSIsContainer -eq $true -and $_.Name -eq "Subsun" -or $_.Name -eq "PerfLogs"}
	If ($Folders){
	   foreach ($Folder in $Folders) { 
	    $Result += $Folder | Get-ChildItem -Recurse | Measure-Object -Sum Length | Select-Object `
	    @{N="Server";E={$Server}},
	    @{N="Path";E={$Folder.FullName}},
	    @{N="Files";E={$_.Count}},
	    @{N="Size";E={"{0:N2}" -f ($_.sum / 1MB) + " MB"}}
	  }
      }
    Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="No folder";Files="NA";Size="NA"}}
   }
  Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="No folder";Files="NA";Size="NA"}}
  Remove-PSDrive -Name z -ErrorAction SilentlyContinue
 }
 Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="Not pinging";Files="NA";Size="NA"}}
}
$Result | Select Server,Path,Files,Size | Export-Csv c:\report.csv -NoTypeInformation

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 750 total points
ID: 38762067
Hi, the third line you changed to
strFolderShare = "NDrive$"

should reflect the "Share Name" that is given to the folder on each server.  So, you should be able to manually browse to
\\servername\NDrive$

on each server.  Similary, if you have the fourth line as
arrSubFolders = Array("Temp", "Users")

then you must be able to browse to
\\servername\NDrive$\Temp
and
\\servername\NDrive$\Users

on each server.  If the folder is multiple levels deep from the shared folder, you can use
arrSubFolders = Array("Temp\Folder1", "Users\MyAccount")

to find
\\servername\NDrive$\Temp\Folder1
and
\\servername\NDrive$\Users\MyAccount

on each server.

I hope that helps.

Regards,

Rob.
0

Featured Post

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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