Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Run PowerShell Script as Administrator (Elevated Privileges) by Default

$
0
0
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:
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
if($CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))
{
Write-host "Script is running with Administrator privileges!"
}
else
{
#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
[System.Diagnostics.Process]::Start($ElevatedProcess)

#Exit from the current, unelevated, process
Exit

}
}

#Check Script is running with Elevated Privileges
Check-RunAsAdministrator

#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

Viewing all articles
Browse latest Browse all 1058

Trending Articles