I have a custom assembly which contains code to setup Variations for a SharePoint 2010 Publishing site. This code is based off a blog post from Waldek

I have a PowerShell script in which I create a new WebApplication and SiteCollection, I then load my custom assembly and attempt to invoke methods which setup the variations. Problem is that the CurrentUser property of the SPWeb (RootWeb) I'm trying to work with returns null, which is the underlying cause of the Access is denied error I receive.

If I decompose this into two scripts, one to do the creation and one to call the custom code, it works, provided I call them both interactively. If I wrap them into a single parent script that invokes each of them in turn I'm back to the CurrentUser being null.

Now for my required usage I need this to work in the context of a single script unattended install.

Has anyone seen this or something simialr? Or do you have any ideas as to how I might overcome this problem?

2 Answers 2


The way you describe this, it sounds like a caching issue, since PowerShell relies heavily on caching.

I avoid this in my provisioning scripts by spawning new PowerShell processes on the fly from my main console. This can also be used to ensure that shells are called as administrator, when UAC is enabled:

$shell = Start-Process powershell -verb runas -Passthru 

Passthru makes it possible to exit shells with return codes. Runas verb runs the shell elevated.

  • Well that works, cheers Anders
    – GavinB
    Commented Jun 19, 2011 at 22:13

I've seen similar with there being a lack of an active SPContext. This may be relevant, but is worth your checking. There's a technique you can use to create a wrapper which is quite helpful (I use this in console apps too).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.