Powershell Archiving Script, Part 7

We’re on the home straight now.  In the last part we dealt with the last major workhorse of the script (actually moving objects to and from the archive with Move-ArchiveObject) and in this part we deal with some of the formatting / presentation functions.  The full script can be found here.

 

 Format-FolderList and Display-BytesAsKB

        function Format-FolderList
        {
            param
            (
                [Parameter(Mandatory=$True,ValueFromPipeline=$true)]
                    [psobject[]]$FolderList
            )
            BEGIN
            {
                $i=0;
            }
            PROCESS
            {
                #Add an Index attribute to show the order in the list.
                Write-Debug "Entering Get-FolderInformation function"
                foreach($Folder in $FolderList)
                {
                    $i++;
                    Add-Member -InputObject $Folder -MemberType NoteProperty -Name Index -Value $i
                    Write-Output $Folder
                }
                Write-Debug "Leaving Get-FolderInformation function"
            }
        }

        function Display-BytesAsKB
        {
            param
            (
                [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
                    $ByteCount
            )
            Return ("{0:N2} KB" -f ($ByteCount / 1KB))
        }

We want the user to be able to confirm which folders they want to process. The best way to display this would be in the form of a list containing as much of the relevant information as possible. The PSObject we created in the Get-FolderInformation function already has properties for much of the information we want (the size of the folder, it’s source and destination, whether it’s going to be processed or not, etc) which can easily be presented as a list.

After it has been presented we need a way for the user to interact with it and toggle individual list items for processing. The easiest way to do that will be to give each list entry a numeric index and then the user can enter the index in to toggle the processing of that entry.  Here’s an example;

Archiver Script List Screenshot

So each item can have it’s ‘Moving’ property toggled by typing its index.

        function Format-FolderList
        {
            param
            (
                [Parameter(Mandatory=$True,ValueFromPipeline=$true)]
                    [psobject[]]$FolderList
            )
            BEGIN
            {
                $i=0;
            }
            PROCESS
            {

This code defines the function and stipulates that an array of folders is a mandatory parameter. This can be passed from the pipeline. This allows us to accept an array, a single folder or a folder passed from the pipeline. We want to count the number of items passed and increment a counter (which will be shown as the index). The counter should start at 0 ($i=0) but we only want to initialise the variable before the first item in the pipeline is passed; we don’t want to re-initialise it for each item in the pipeline or they’d all be returned with an index of 1!

                Write-Debug "Entering Get-FolderInformation function"
                foreach($Folder in $FolderList)
                {
                    $i++;
                    Add-Member -InputObject $Folder -MemberType NoteProperty -Name Index -Value $i
                    Write-Output $Folder
                }
                Write-Debug "Leaving Get-FolderInformation function"
            }
        }

In this code we just enumerate through every folder that’s been passed, increase the index by 1 and then add that index as property to the PSObject. The PSObject is then returned from the function.

        function Display-BytesAsKB
        {
            param
            (
                [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
                    $ByteCount
            )
            Return ("{0:N2} KB" -f ($ByteCount / 1KB))
        }

Currently the size of each folder is stored as bytes. We want to keep storing as bytes for accuracy but when the information is shown to the user we want to show the size in KB, rounded to two decimal points. We make use of PowerShell’s string formatting functionality to do exactly that (“{0:N2} KB” -f ($ByteCount / 1KB)). A good primer on using string formatting is here.

The last part will look at the actual body of the script which calls all these functions.

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 )

Connecting to %s

%d bloggers like this: