Powershell Archiving Script, Part 4

In the last part we wrote out the Powershell to handle the parameters (including some crude validation) and for the skeleton of the rest of the script. In the next few parts we’ll define the functions the script will use.  The full script can be found here.

Get-FolderSize Function 

        function Get-FolderSize
        {
            param
            (
                [Parameter(Mandatory=$True)]
                [ValidateScript(
                    {
                        if(!(Test-Path -Path $_))
                        {
                            Throw ("Source path ($_) does not exist.")
            
                        }else
                        {
                            $True
                        }
                    }
                )]
                [string]$Path
            )
            #Recurse through subfolders in the path and add the size of any child items that aren't folders.
            Write-Debug "Entering Get-FolderSize function"
            Write-Debug "Parameter : Path = $Path"
            [int64]$Size=0;
            foreach ($ChildItem in Get-ChildItem -Path $Path)
            {
                if ($ChildItem.PSIsContainer)
                {
                    $Size=$Size+(Get-FolderSize -Path $ChildItem.FullName)
                }else
                {
                    $Size=$Size+$ChildItem.Length
                }
            }
            Write-Debug "Leaving Get-FolderSize function"
            return $Size
        }

This function is required for a couple of reasons. First, we want to list all the folders that could be archived (or returned from archive) in the source path in descending order of size (so the user can make an informed choice about what to archive). Second after copying a folder we want to check the destination folder size matches the original source size before the source is deleted.

        function Get-FolderSize
        {
            param
            (
                [Parameter(Mandatory=$True)]
                [ValidateScript(
                    {
                        if(!(Test-Path -Path $_))
                        {
                            Throw ("Source path ($_) does not exist.")
            
                        }else
                        {
                            $True
                        }
                    }
                )]
                [string]$Path
            )

Here we define and open the function definition and list the only parameter I’m interested in; the path to the folder to get the size of. This is mandatory as there’s no point carrying on without it! It also validates that $Path is in fact a valid path.

            Write-Debug "Entering Get-FolderSize function"
            Write-Debug "Parameter : Path = $Path"
            [int64]$Size=0;

The Write-Debug lines are outputted to the Debug stream when the script is called with the -Debug switch. Normally we don’t need this information but it’s useful if I’m debugging. $Size is going to be a running total size of all the files and folders in the current path. It needs to be defined as 64-bit otherwise it will overflow pretty quickly.

            foreach ($ChildItem in Get-ChildItem -Path $Path)
            {
                if ($ChildItem.PSIsContainer)
                {
                    $Size=$Size+(Get-FolderSize -Path $ChildItem.FullName)
                }else
                {
                    $Size=$Size+$ChildItem.Length
                }
            }
            Write-Debug "Leaving Get-FolderSize function"
            return $Size
        }

Finally the meat of the function. It runs through each item in the path and adds its size to the running total $Size. Folders themselves however have no size so the when a child item is a folder the function calls itself recursively to get the size of that folder.

The function returns the final value of $Size as part of closing the function.

In the next part we’ll look at the Get-FolderInformation function which gets all the data for the user about the folders they might want to process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s