Set-AzureRMVMCustomScriptExtension : Object reference not set to an instance of an object

I was working on an Azure Automation Runbook for a customer when I ran into this issue.  All the Runbook does is run a PowerShell script to clean up old log files.  Seems like a very straight forward process right?

I tested everything in PowerShell first, and everything worked great.  When I went to test my Runbook, I received the following error message:

A quick google search didn’t turn up anything and the error message itself isn’t particularly useful.  The PowerShell code in the Runbook is the following:

After spending 3 hours (not an exaggeration) trying to figure out why it would work from laptop but not in Azure Automation, I stumbled across an idea that seems so obvious in retrospect that I can’t believe it took me 3 hours to come up with it.  Were the module versions in Azure Automation a different version than my local machine and that was causing the problem?  The answer is yes.  When you provision a new Azure Automation account this is what you get for Azure Modules:

When you run Update Modules from Azure Automation, you then get this:

Lesson learned.