Let's say I am logged into a server using "user1". I have a project where the powershell process runs under "user2". So every time I execute a script it will be under "user2". Is there a way to specify powershell to run a certain script under "user1" without prompting for credentials (like mentioned before I'm already logged in as that user)? Thanks in advance!
Asked
Active
Viewed 2,788 times
0
-
1I'm confused. If you are logged onto a server as User1 and you just run a script, it will run as you (User1). – EBGreen May 16 '18 at 12:35
-
I run a bat file which opens the powershell process under "user2" . So every other script ran from within the main script will be under "user2". – Peta Eduard May 16 '18 at 12:52
-
Well then don't run that bat file? Just run the scripts. Or make a differnt bat file that does not run those scripts as the other user. – EBGreen May 16 '18 at 12:58
-
Hi, welcome to stack overflow, kindly please visit links at [How to ask](https://stackoverflow.com/help/how-to-ask), clarify your situation / question, its unclear and confusing, what are you asking for, thanks. – xxxvodnikxxx May 16 '18 at 12:59
-
Btw I guess [that fits](https://stackoverflow.com/a/28991396/4892907) – xxxvodnikxxx May 16 '18 at 13:01
-
1If I didn't want to run the bat file I wouldn't have asked in the first place. I just wanted to know if there is a method to capture the user with which I'm logged in on the server and run certain scripts using that. @xxxvodnikxxx I would prefer a solution without harcoding passwords or prompting me for credentials. I'm more into somehow capturing the user which I'm logged as and run scripts using those credentials – Peta Eduard May 16 '18 at 13:07
-
1Unless you tell it otherwise, every script that you run runs as the user that you are logged on as. Period. So, if you can create an example of what you have now then we can tell you how you might accomplish your goal. Otherwise, the question is just not clear enough. – EBGreen May 16 '18 at 13:17
-
If you will log into machine, then everything what will you run, will be executed under the logged user- until you tell other one, if I am correct .. Particular answer- for getting only username, you can use `$env:username` , for getting also the domain, you can use `Get-WMIObject -class Win32_ComputerSystem | select username` – xxxvodnikxxx May 16 '18 at 13:18
1 Answers
0
No, there is not a way to do this.
The process is running under the context of user2, it cannot run anything in the context of user1 without giving it user1 credentials. As you may be able to imagine if there were a way to do that, it would be a pretty significant security vulnerability.
Bonus Information
If however, you are looking to do something like open a web page as user1 from a process running as user2, you can do so by passing the http/https protocol to the user session IIRC. However, this is only if the process running as user2 is running in the user1 session.
For example:
- Logged in to a console session as User1
- Powershell Process running under User2 context, within the User1 session
- Within the Powershell process, run
start-process http://www.google.com
- Default http application opens the URL in the User1 session

Adam Parsons
- 506
- 2
- 9