This repo includes the Automated AD PowerShell script, along with a sample .NET application to demonstrate the usage. Find a walkthrough of the process here in the following posts:
http://blog.jonathanhuss.com/automate-the-creation-of-an-azure-ad-application/
http://blog.jonathanhuss.com/assigning-permissions-to-an-azure-ad-application/