Solved

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

Posted on 2016-07-18
4
34 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
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 83

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 78

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 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

22 Experts available now in Live!

Get 1:1 Help Now