Can I use RunWithElevatedPrivileges in a PowerShell script?

If yes, please give an example.

UPDATE:

I have a PowerShell script that iterates through all site collections in a selected web application and changes the property siteCollection.Audit.AuditFlags. It works fine on my development machine, but the command siteCollection.Audit.Update()does not work when an error is refused on the server, although I try to run it as a user who is the farm administrator.

+1
source share
3 answers

Run powershell as an administrator or as a user of the web application's application pool.

+3
source

Yes it is possible.

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

. , .

+7

RunWithElevatedPrivs uses the application pool user for regular web applications, not the farm administrator. If elevation occurs during central administration, then this is the farm administrator account. I assume you are doing this on regular web servers, so run PowerShell as you add the application pool.

+4
source

All Articles