- http://sadomovalex.blogspot.ca/2012/10/fix-nullreferenceexception-when-use.html
- https://pholpar.wordpress.com/2013/07/15/injecting-httpcontext-and-spcontext-into-your-powershell-scripts/
- ... and many others
Frustratingly, none of this was working when I tried. I was running as the Farm Account.
Then, I came across this: http://blog.claudiobrotto.com/2011/sharepoint-management-shell-vs-standard-powershell-console/
I normally use just the Windows Powershell while on my servers and call Add-PSSnapin Microsoft.SharePoint.Powershell. However, I forgot that the SPServiceContext::Current is being lost when a new thread is being used.
I added the following and all is good again:
$Host.RunSpace.ThreadOptions = 'ReuseThread'