Thursday, January 15, 2015

Run PowerShell Script as Administrator (Elevated Privileges) by Default

Its a common SharePoint Administrator's pitfall - Forget to run PowerShell script using "Run as Administrator" option, failing so could lead to many *weird* issues while running PowerShell scripts in SharePoint, such as: "The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.".

Solution is pretty simple! just right click the SharePoint 2013 PowerShell Snap-in and choose the option "Run as Administrator".
 run as administrator powershell script

Enable "Run as Administrator" elevated privilege for SharePoint 2013 Management Shell by default:
To run PowerShell script as administrator automatically, Create a shortcut to your PowerShell console on your desktop
  • Right-click the "SharePoint 2013 Management Shell" shortcut and click Properties
  • Click "Advanced" button under Shortcut tab
  • Enable "Run as Administrator" and click on "OK" button.
powershell script always run as administrator
Now you can run PowerShell in elevated mode by simply double-clicking the new shortcut on your desktop.

Run PowerShell as administrator in scheduled tasks:
If you are scheduling a PowerShell script, make sure you select the "Run With Highest Privileges" check box. Otherwise your scheduled task which invokes a UAC prompt may fail to run unattended.

Handle Run as Administrator with in PowerShell script:
Lets handle it in our PowerShell code itself, even you forget to use "Run as Administrator" option!
Function Check-RunAsAdministrator()
  #Get current user context
  $CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  #Check user is running the script is member of Administrator Group
       Write-host "Script is running with Administrator privileges!"
       #Create a new Elevated process to Start PowerShell
       $ElevatedProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

       # Specify the current script path and name as a parameter
       $ElevatedProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

       #Set the Process to elevated
       $ElevatedProcess.Verb = "runas"

       #Start the new elevated process

       #Exit from the current, unelevated, process


#Check Script is running with Elevated Privileges

#Place your script here.
write-host "Welcome"
add this code at the beginning of your script.

Run as a Different User in PowerShell scripts:
$credential = New-Object System.Management.Automation.PsCredential("Domain\UserID", (ConvertTo-SecureString "Password" -AsPlainText -Force))
Start-Process powershell -Credential $credential -NoNewWindow
How to run PowerShell as administrator from the command line? 
To run PowerShell as administrator in command line:
  1. Type : PowerShell to enter into PowerShell console
  2. Now, Type: Start-Process PowerShell -Verb RunAs
BTW, There could be some more reasons for "The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered" issue such as:
Another solution to address this issue: Disable UAC! Here is how - How to Disable UAC in Windows Server 2012/2008

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...