Thursday, July 28, 2011

Powershell script to use RunAs for SharePoint Add-Ins

I am in a environment where I want to use a Service Account ( say Farm account) for doing any command line task (for eg, add, install, retract solutions ….). In MOSS 2007, we used simple runas different user command to execute STSAM.

How am I going to achieve this, to use my lovely SharePoint Shell..

Anyway, i don't want to use Powershell profile because of some restrictions i have.

here is something I did..

1) Create a Powershell script with below entries and save it with RunAs.ps1

runas /user:domain\useraccount D:\MaintenanceScripts\Powershell\Runas\SPShell.Bat



2)Create a batch file named SPShell.Bat (you can name it as you wanted and ensure you update the same in the above RunAs.PS1) with the below content.

C:\Windows\system32\windowspowershell\v1.0\PowerShell.exe -NoExit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "


Procedure to execute

1) Have both the files in a common folder
2) Create a shortcut of RunAs.ps1 to your desktop
3) Now double click on your RunAs.PS1 and work is done :)

If RunAS.PS1 opens in a notepad, use the following step.

i) Right click RunAs.PS1 and say open with and select Powershell, ensure you have selected "Always use the Selected program to open this kind of file"


Now you are ready to execute the SharePoint Shell as a different user (Farm admin).

Hope this helped you..

No comments:

Post a Comment