Marek Grzenkowicz
Marek Grzenkowicz

Reputation: 17353

Can RunWithElevatedPrivileges be used in a PowerShell script?

If yes, please give an example.

UPDATE:

I have a PowerShell script that iterates through all site collections within selected Web application and changes the siteCollection.Audit.AuditFlags property. It works fine on my development machine, but the siteCollection.Audit.Update() command fails with Access is denied error on the production server, even though I am trying to run it as a user who is a farm administrator.

Upvotes: 2

Views: 6407

Answers (3)

Vinicius
Vinicius

Reputation: 1701

Yes, it can.

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({ 
    $site = get-spsite "http://localhost/nonfarmadminsitecollection";
}); 

Be careful with that. Since you are impersonating the process account, you lose information about the user in the audit trails.

Upvotes: 9

x0n
x0n

Reputation: 52430

RunWithElevatedPrivs uses the application pool user on regular web apps, not farm admin. If the elevation happens on central administration, then it's a farm admin account. I assume you are doing this on regular webapps, so launch powershell as the app pool acount.

Upvotes: 4

Janis Veinbergs
Janis Veinbergs

Reputation: 6988

Run powershell as an administrator or as your webapp application pool user.

Upvotes: 3

Related Questions