Solved

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

Posted on 2016-07-18
4
36 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 79

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

825 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