Friday 2 June 2017

Run PowerShell using Local System Account

You will very frequently come into a scenario where you will need to run some commands using local system account.
In my case, I was working on a windows service running using local system account. The windows service internally was calling PowerShell scripts.
I developed PowerShell scripts independently and they were absolutely working fine(running using my account which has elevated privileges), but when tested along with Windows Service, it failed due to various reasons like access and permission issues (as Windows service run scripts using local system account).
So to resolve this issue, I launched PowerShell with local system account, did required changes to code, performed complete testing and whoa I was all setup to go.
I thought to share all this information to you and save some of your time in troubleshooting.

Initial Setup

  1. Download PsExec from this link.
  2. It will ask you to download PSTools folder on your machine. Save it on your C:\ drive.

Open PowerShell with Local System Account

  1. Open Command Prompt
  2. Run below commands :
  3. Change directory to PSTools folder.
cd C:\PsTools
  1. Invoke PowerShell using PsExec.exe
.\PsExec.exe –i –s PowerShell.exe
  1. It will launch PowerShell. Validate current user
whoami


Open PowerShell ISE with Local System Account

  1. Open Command Prompt
  2. Run below commands :
  3. Change directory to PSTools folder.
cd C:\PsTools
  1. Invoke PowerShell using PsExec.exe
.\PsExec.exe –i –s PowerShell_Ise.exe
  1. It will launch PowerShell ISE. Validate current user
whoami






Whoa! You are good to go. Do all your work and testing required to be done using Local System Account.

Happy Coding!!!

4 comments:

  1. power shell script that starts web applications? can you help me with that.

    ReplyDelete
    Replies
    1. https://docs.microsoft.com/en-us/powershell/module/webadministration/start-website?view=windowsserver2019-ps this should help

      Delete