Powershell Archiving Script, Part 8 (Final Part)

The final functions are done (in the last part) now it’s just the main body of the script.  This will bring all the code together and produce the output we want.  The full script can be found here.

Script Body

    PROCESS
    {
        Write-Debug "Entering Move-Archive function"
        $Command=$Null
        #Build a sort a list of all the folders we're interested in.
        $FolderList=$Folder | Get-FolderInformation -SourcePath $SourcePath -ArchivePath $ArchivePath | Sort-Object -Property Size -Descending | Format-FolderList

        While (($Command -ne "") -and ($Command -ne "Exit"))
        {
            #List all the folders with an index number and allow the user to toggle them on or off for movement to/from the archive.
            $FolderList | Select-Object -Property Index ,Moving, Direction,FullSourcePath,FullArchivePath, @{name="FolderSize";expression={(Display-BytesAsKB -ByteCount $_.Size)}} | Format-Table -AutoSize
            $Command=Read-Host -Prompt "Hit [Return] to Continue, Type 'Exit' to Cancel or enter the Index number to toggle processing"
            if ([bool]($Command -as [int]))
            {
                ($FolderList[[int]$Command-1]).Moving=!(($FolderList[[int]$Command-1]).Moving)
            }
        }
        if ($Command -eq "")
        {
            $FolderList | Where-Object -FilterScript {$_.Moving}| Move-ArchiveObject | Format-Table -Property FullSourcePath,FullArchivePath,Successful,Message -AutoSize
        }
    }
    END
    {
    }

This is the main body of the script. All of the code is within the Process block so it is performed on every object that is passed via the pipeline.

As a reminder we want the output of the script to look like this;

Archiver Script List Screenshot
Example output

<pre
$Command=$Null
#Build a sort a list of all the folders we’re interested in.
$FolderList=$Folder | Get-FolderInformation -SourcePath $SourcePath -ArchivePath $ArchivePath | Sort-Object -Property Size -Descending | Format-FolderList

$Command is going to be used to hold whatever command the user types in. We set it to $Null to distinguish it from the empty string (“”) which we’re going to use as the ‘take the current selection and process them’ option.

We then build $FolderList from a multi-stage pipeline;

  1. First, we user Get-FolderInformation to return all the information about folders that match $SourcePath and $ArchivePath.
  2. We want the largest folders in that group to be at the front of the list as they’re the ones it’s most useful to archive.
  3. Finally we pipe the output to Format-FolderList which adds an index property to each object in the pipeline which is equal to its position on the list.
        While (($Command -ne "") -and ($Command -ne "Exit"))
        {

In the main body we’re going to keep looping, getting input from the user until they either hit return (which executes the command with whatever folders they’ve selected) or enters “exit” which just exits without doing anything.

            $FolderList | Select-Object -Property Index ,Moving, Direction,FullSourcePath,FullArchivePath, @{name="FolderSize";expression={(Display-BytesAsKB -ByteCount $_.Size)}} | Format-Table -AutoSize

This outputs $FolderList to the screen. Again it’s via a multi-stage pipeline;

  1. Use $FolderList as the source of the pipeline.
  2. Only select the properties of each object we want on the screen (skip all the properties that are used by the script itself and the user won’t be interested in).  Instead of the “Size” property we create a new temporary property called “FolderSize“which is the contents of “Size” converted to KB (using the Display-BytesAsKB function).
  3. Finally we format the output as a table with the -AutoSize switch to space it nicely.
            $Command=Read-Host -Prompt "Hit [Return] to Continue, Type 'Exit' to Cancel or enter the Index number to toggle processing"
            if ([bool]($Command -as [int]))
            {
                ($FolderList[[int]$Command-1]).Moving=!(($FolderList[[int]$Command-1]).Moving)
            }

Now we read what the user wants to do (after prompting them). If the user entered an integer we find the object with that index (remembering that arrays start counting at 0 so we have to subtract 1 from the user’s input) and then toggle its “Moving” property.

        if ($Command -eq "")
        {
            $FolderList | Where-Object -FilterScript {$_.Moving}| Move-ArchiveObject | Format-Table -Property FullSourcePath,FullArchivePath,Successful,Message -AutoSize
        }
    }
    END
    {
    }

And this is it. After all this build-up this is where the command actually gets executed in another multi-stage pipeline;

  1. Use $FolderList as the start of the pipeline.
  2. Next filter the pipeline so only objects where the “Moving” property has been set to $True are processed.
  3. Move that item using Move-ArchiveObject.
  4. Finally format the results as a table.

And that’s it!  The full script can be found here.  There’s plenty we can do to make the script more efficient, more resilient or more functional but this should work as a starting point for an archive script.

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