Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Report on folders displaying size but also other calculated values

$
0
0

Dear,

I have a powershell script running, which returns me a proper report on the size of all subfolders of the homedirectories on our fileserver:

***********************************************************************

[CmdletBinding()]

Param (

    [Parameter(ValueFromPipeline)]

[string[]]$Paths = "Y:\debiej",

[string]$ReportPath = "C:\admin\03_scripts",

    [ValidateSet("Folder","Folders","Size","Created","Changed","Owner")]

    [string]$Sort = "Size",

    [switch]$Descending,

    [switch]$Recurse

)

 

Begin {

    Function AddObject {

    Param ( 

    $FileObject

    )

        $RawSize = (Get-ChildItem $FileObject.FullName -Recurse | Measure-Object Length -Sum).Sum

 

    If ($RawSize)

    {$Size = CalculateSize $RawSize

    }

    Else

    {$Size = "0.00 MB"

    }

    $Object = New-Object PSObject -Property @{

    'Folder Name' = $FileObject.FullName

    'Created on' = $FileObject.CreationTime

    'Last Updated' = $FileObject.LastWriteTime

    Size = $Size

    Owner = (Get-Acl $FileObject.FullName).Owner

            RawSize = $RawSize

    }

        Return $Object

    }

 

    Function CalculateSize {

    Param (

    [double]$Size

    )

    If ($Size -gt 1000000000)

    {$ReturnSize = "{0:N2} MB" -f ($Size / 1MB)

    }

    Else

    {$ReturnSize = "{0:N2} MB" -f ($Size / 1MB)

    }

    Return $ReturnSize

    }

 

 

    Function Set-AlternatingRows {

        [CmdletBinding()]

       Param(

           [Parameter(Mandatory=$True,ValueFromPipeline=$True)]

            [object[]]$Lines,

 

          [Parameter(Mandatory=$True)]

           [string]$CSSEvenClass,

 

            [Parameter(Mandatory=$True)]

          [string]$CSSOddClass

       )

    Begin {

    $ClassName = $CSSEvenClass

    }

    Process {

            ForEach ($Line in $Lines)

            {$Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")

        If ($ClassName -eq $CSSEvenClass)

        {$ClassName = $CSSOddClass

        }

        Else

        {$ClassName = $CSSEvenClass

        }

        Return $Line

            }

    }

    }

 

    #Validate sort parameter

    Switch -regex ($Sort)

    {   "^folder.?$" { $SortBy = "Folder Name";Break }

        "created" { $SortBy = "Created On";Break }

        "changed" { $SortBy = "Last Updated";Break }

        default { $SortBy = $Sort }

    }

 

    $Report = @()

    $TotalSize = 0

    $NumDirs = 0

    $Title = @()

    Write-Verbose "$(Get-Date): Script begins!"

}

 

Process {

    ForEach ($Path in $Paths)

    {   #Test if path exists

        If (-not (Test-Path $Path))

        {   $Result += $Object = New-Object PSObject -Property @{

        'Folder Name' = $Path

        'Created on' = ""

        'Last Updated' = ""

        Size = ""

        Owner = "Path not found"

                RawSize = 0

        }

            $Title += $Path

            Continue

        }

 

        #First get the properties of the starting path

        $NumDirs ++

        Write-Verbose "$(Get-Date): Now working on $Path..."

        $Root = Get-Item -Path $Path 

        $Result = AddObject $Root

        $TotalSize += $Result.RawSize

        $Report += $Result

        $Title += $Path

 

        #Now loop through all the subfolders

        $ParamSplat = @{

            Path = $Path

            Recurse = $Recurse

        }

        ForEach ($Folder in (Get-ChildItem @ParamSplat | Where { $_.PSisContainer }))

        {$Report += AddObject $Folder

            $NumDirs ++

        }

    }

}

 

End {

    #Create the HTML for our report

    $Header = @"

<style>

TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}

.odd  { background-color:#ffffff; }

.even { background-color:#dddddd; }

</style>

<Title>

Folder Sizes for "$Path"

</Title>

"@

 

    $TotalSize = CalculateSize $TotalSize

 

    $Pre = "<h1>Folder Sizes Report</h1><h3>Folders processed: ""$($Title -join ", ")""</h3>"

    $Post = "<h2><p>Total Folders Processed: $NumDirs<br>Total Space Used:  $TotalSize <br></h2>Run on $(Get-Date -f 'MM/dd/yyyy hh:mm:ss tt')</body></html>"

 

    #Create the report and save it to a file

    $HTML = $Report | Select 'Folder Name',Owner,'Created On','Last Updated',Size | Sort $SortBy -Descending:$Descending | ConvertTo-Html -PreContent $Pre -PostContent $Post -Head $Header | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd | Out-File $ReportPath\FolderSizes1.html

 

}

***********************************************************************

I would like to extend this report with some extra columns telling me:

Totals size of images:

This gives me total size of folder:

$RawSize = (Get-ChildItem $FileObject.FullName -Recurse | Measure-Object Length -Sum).Sum

This gives me total size of images in folder:

$RawSize = (Get-ChildItem $FileObject.FullName -Recurse -Include *.bmp,*.png,*.jpg, *.gif | Measure-Object Length -Sum).Sum

How can I add this value to my report?

Any help would be much appreciated


Viewing all articles
Browse latest Browse all 8583

Trending Articles