Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Well that works, cheers Anders –  GavinB Jun 19 '11 at 22:13
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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