Great work building your own script, I went down the same path a few years ago until I found ManagePartition.exe. Check out my two part blog series on working with it.
Part 1: Intro to SQL Server Partition Management Utility
Part 2: Using Partition Management Utility with SSIS