Monday, May 30, 2011

Windows Powershell - Run powershell script at startup under different domain user account logon

Run powershell script at startup under different domain user account logon.
This was the problem with which I struggled a lot. As usual my companies GOD, none other than CLIENT, wanted to run this SharePoint powershell script on start up of machine.
The challenge was, the powershell script should run on startup, but not with Local System account, rather a domain user account that will be in the administrator group on the machine.
Alright here we go!!




Problem Statement – Execute or run powershell script at startup under domain user account.
Applicable Technology is Windows XP and higher, Windows Server 2003 and higher.
Ok. The solution lies more likely in running the powershell script as a Windows Service. The steps to achieve this is as follows –
For running Powershell script as a service, you need to install Windows 2003 Resource Kit tools. Download the exe from here. While installations, you may receive compatibility warnings. Ignore them and proceed with installation.


Open command prompt. Change the directory to windows Resource kit tools installation directory as follows –
Cd\
Cd C:\Program Files (x86)\Windows Resource Kits\Tools
Now run the following command to create the windows service –
Instsrv.exe PowerShellStartup C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe
The above command creates a windows service named as “PowerShellStartup”. If you open the “Run” window and type “services.msc”, Services console will be opened and your windows service will be listed. The Local System account will be the default user account under which service will run. The domain user under which you want to run the powershell script, add it to “Administrators” group of the machine. Then set this domain user account as logon account for the above created windows service. Also set its startup as “Automatic Delayed”. This will ensure that, the powershell script will be executed after 2 minutes of machine rebbot.




Open registry editor by running command “RegEdit” in run window. Go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\PowerShellStartup. Right click on it and choose option as NEW KEY. Name it as “Parameters”. Then right click on “Parameters” and select NEW STRING VALUE. Name it as “Application”. Provide the value as “c:\BatchFileToExecutePowerShell.bat”. Means we are telling the service to run file “BatchFileToExecutePowerShell.bat” whenever it gets started and this file can be found on C drive.

Create a batch file with same name provided above and write following code in it.
rem use PSExec.exe to run powershell exe
C:\Windows\SysWow64\PsExec.exe -accepteula -u YourDomain\DomainUser -p DomainUserPassword C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\YourPowerShell.ps1
The above statement tells that, run the powershell script file under the domain user account using exe PSExec.
Copy your powershell script file to C drive. Download PSExec.exe from here. After download, copy PSexec.exe file to location “C:\Windows\SysWow64\”.
That’s all friend, you are done!! On machine restart, your powershell script will run the domain user specified in batch file code statement.
Cheers…

Please give food to all my fishes swimming at the bottom. It's fun!! Try it!!
Thanks for reading!!
Happy Coding!!

No comments:

Post a Comment