Solved

50 GB file Audit Script using powershelll not showing exact file path

Posted on 2016-07-18
4
41 Views
1 Endorsement
Last Modified: 2016-07-19
The below script has capability to search files more than 50GB in size under the path F:\Lotus\Domino\Data, but its not giving the complete path where the file exists. For example one of the file is under a sub-folder it still shows was found under F:\Lotus\Domino\Data... If any one can help me to add what is missing will be of a great help and I need this ASAP. Also I want to understand the complete script how it works.
Please help me friends.

$ServerFile = 'C:\Temp\Servers.txt'
$LocalFolder = 'F:\Lotus\Domino\Data'
$FileSize = 50 * 1GB
$ResultFile = 'C:\Temp\FileSize.csv'
Get-Content $ServerFile | ForEach-Object {
      $ComputerName = $_
      Get-ChildItem "\\$_\$($LocalFolder.Replace(':', '$'))" -Recurse |
            Where-Object {$_.Length -gt $FileSize} |
            Select-Object -Property `
                  @{n="ComputerName"; e={$ComputerName}},
                  @{n="Name"; e={"$($LocalFolder)\$($_.Name)"}},
                  @{n="SizeMB"; e={[math]::Round($_.Length / 1MB, 3)}}
} | Export-Csv -NoTypeInformation -Path $ResultFile
1
Comment
Question by:PavanPA
[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
4 Comments
 
LVL 17

Expert Comment

by:Learnctx
ID: 41718127
If you want the full path then use the fullname property instead of name.

$ServerFile = 'C:\Temp\Servers.txt'
$LocalFolder = 'F:\Lotus\Domino\Data'
$FileSize = 50 * 1GB
$ResultFile = 'C:\Temp\FileSize.csv'
Get-Content $ServerFile | ForEach-Object {
      $ComputerName = $_
      Get-ChildItem "\\$_\$($LocalFolder.Replace(':', '$'))" -Recurse |
            Where-Object {$_.Length -gt $FileSize} |
            Select-Object -Property `
                  @{n="ComputerName"; e={$ComputerName}},
                  FullName,
                  @{n="SizeMB"; e={[math]::Round($_.Length / 1MB, 3)}}
} | Export-Csv -NoTypeInformation -Path $ResultFile

Open in new window

Also this seems wrong.

Get-ChildItem "\\$_\$($LocalFolder.Replace(':', '$'))" -Recurse

Open in new window

Should it be this instead? Granted, I have not run your script so just running it over by eye.

Get-ChildItem "\\$_\$($LocalFolder.Replace('$', '$'))" -Recurse

Open in new window

2
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 41718193
I think I recognize my handiwork there. You don't get the full name because the original version of the script wasn't meant to search recursively.
$ServerFile = 'C:\Temp\Servers.txt'
$LocalFolder = 'F:\Lotus\Domino\Data'
$FileSize = 50 * 1GB
$ResultFile = 'C:\Temp\FileSize.csv'
Get-Content $ServerFile | % {
	$ComputerName = $_
	$SearchRoot = "\\$_\$($LocalFolder.Replace(':', '$'))"
	Get-ChildItem $SearchRoot -Recurse |
		Where-Object {$_.Length -gt $FileSize} |
		Select-Object -Property `
			@{n="ComputerName"; e={$ComputerName}},
			@{n="Name"; e={$_.FullName.Replace($SearchRoot, $LocalFolder)}},
			@{n="SizeMB"; e={[math]::Round($_.Length / 1MB, 3)}}
} | Export-Csv -NoTypeInformation -Path $ResultFile

Open in new window

2
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 41718224
$ServerFile = 'C:\Temp\Servers.txt'
$LocalFolder = 'd:\Hyper-v\'
$FileSize = 50 * 1GB #50 GB
$ResultFile = 'C:\Temp\FileSize.csv'
remove-item $resultfile -ErrorAction SilentlyContinue
$myobj = @()
$servers = Get-Content $ServerFile
ForEach ($Server in $Servers) {
    $filepath = "\\$server\$($LocalFolder.Replace(':', '$'))"
    $Files = Get-ChildItem  $filepath -Recurse -File
    ForEach ($file in $Files)
        {
        if ($file.length -ge $FileSize)
            {
            $obj = new-object -TypeName PSObject      
            $obj | add-member -MemberType NoteProperty -Name 'ComputerName' -Value $Server
            $obj | add-member -MemberType NoteProperty -Name 'Name' -Value $file.FullName.replace($filepath, $LocalFolder)
            $obj | add-member -MemberType NoteProperty -Name 'SizeMB' -Value ([math]::Round($file.Length / 1MB, 3))
            $myobj +=  $obj
        } 
}        
}
if ($myobj.Count -gt 0) {
    $myobj | Export-Csv -NoTypeInformation -Path $ResultFile 
    }
 else {
    write-output 'No Files over 50 GB'
    }
get-content $resultfile

Open in new window

2
 

Author Closing Comment

by:PavanPA
ID: 41718230
The script worked for me as requested and I am very thankful and will always take help from oBdA when required. :)
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

This article will help you understand what HashTables are and how to use them in PowerShell.
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.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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