get all folders of a disk with total size in MB or GB

This topic contains 2 replies, has 1 voice, and was last updated by Profile photo of Jeremy-Smit Jeremy-Smit 2 months, 2 weeks ago.

  • Author
    Posts
  • #61206
    Profile photo of Jeremy-Smit
    Jeremy-Smit
    Participant

    Hi Forum,

    I have a question about making a script. i have a script to list the folders in a folder wit size, name of the folder, created
    it has been exported to html.

    but this is a test because this sript must be changed to the following that it must do.

    we have to backup server with x counts of disks
    server a has 3 disks
    server b has 2 disk

    on both disks are serveral folder of customers that has their own backupfolder.

    once every month there must be a xml, html, or what ever format to puth out a list of both servers with the disk of every folder, name / created / changed / size

    servers are online accessable through IP, and adminstrator credentials must be built in the script but encrypted.

    in the top there must be showned date and time when the output is created.

    and if possible the output must also be automated send.

    i will put the script in the next comment.

    is there someone that can help me with this?

  • #61207
    Profile photo of Jeremy-Smit
    Jeremy-Smit
    Participant
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline)]
    	[string[]]$Paths = "c:\scripts",
    	[string]$ReportPath = "c:\reports",
        [ValidateSet("Folder","Folders","Size","Created","Changed","Owner")]
        [string]$Sort = "Folder",
        [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} GB" -f ($Size / 1GB)
        	}
        	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("","")
            		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 = @"
    
    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; }
    
    
    Folder Sizes for "$Path"
    
    "@
    
        $TotalSize = CalculateSize $TotalSize
    
        $Pre = "Folder Sizes ReportFolders processed: ""$($Title -join ", ")"""
        $Post = "Total Folders Processed: $NumDirsTotal Space Used:  $TotalSizeRun on $(Get-Date -f 'MM/dd/yyyy hh:mm:ss tt')"
    
        #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\FolderSizes.html
    
        #Display the report in your default browser
        & $ReportPath\FolderSizes.html
        
        Write-Verbose "$(Get-Date): $NumDirs folders processed"
        Write-Verbose "$(Get-Date): Script completed!"
    }
    
  • #61354
    Profile photo of Jeremy-Smit
    Jeremy-Smit
    Participant

    Anyone tips?

You must be logged in to reply to this topic.