0

So I am a real newbie at this and could really use some help. In my company we have user accounts and admin accounts set up for those in my department. We are always logged into our normal user accounts. There are some powershell scripts I need to run as my admin account. The shift+right click on the file doesn't give me the option to "Run as different user".

So I am trying to figure out a script I can put at the beginning of the script that would prompt myself or anyone else who uses it to log in with their AD account before the script is ran.

Thanks

Amitus
  • 1
  • 1
  • 1

2 Answers2

2

The "Run as different user" option is a security setting you could change in your environment.

You could use the Login-User command, as documented here. Given an identity and password in your AD, it should run every command after it in the script from the account.

Login-User -Identity "admin" -Password "test123#"

But I wouldn't recommend it this way, as you would have to store your password in the script.

Maybe the Get-Credential promt could help you, if you really want to execute it on the users PC, it is thoroughly documented here.

My personal though would be: try adjusting your scripts so you can run them from your (admin)-user account and target the users via network.

creyD
  • 150
  • 5
1

There is also the "runas" command line option. Documentation at https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc771525(v%3Dws.11).

The syntax I use is

runas /u:domain\username /netonly powershell.exe

This will prompt you for the password, then launch a new Powershell window that will perform all actions as the username you provide, so you could launch the script in that window.

C C
  • 56
  • 4