SharePoint 2013 - Accessing referenced file from is not allowed because the reference is outside of the App Web

Tags: Development, Event Receivers, PowerShell, SharePoint 2013

Scenario

I ran into this when deploying an event receiver that would update the master page of all sites (SPWeb objects) when activated in a particular site collection (SPSite object).

Accessing referenced file from is not allowed because the reference is outside of the App Web.

Problem

The problem is described and resolved here:  http://mctalex.blogspot.com.au/2013/07/powershell-fix-for-app-master-problem.html.

Better Resolution

I adapted this script to something a bit more readily reusable:  https://github.com/skaggej/powershell/blob/master/Set-SPAppWebMasterPages.ps1.  Just enter the root URL of a site collection and it will update the master pages for all app webs within it.