简体   繁体   English

$ env:Username如何在PowerShell中工作?

[英]How does $env:Username work in PowerShell?

So someone told me the following in a previous question: 因此有人在上一个问题中告诉我以下内容:

"$env:Username allways refers to the user that has created the powershell session, not to the currently logged on user." “ $ env:Username始终是指已创建Powershell会话的用户,而不是当前登录的用户。”

But isn't the currently logged on user always the one creating the powershell session? 但是当前登录的用户不是总是创建Powershell会话的用户吗?

I guess my question is, can you describe to me a scenario or two where user X is creating a powershell session for the currently logged on user Y? 我想我的问题是,您能描述一下用户X为当前登录的用户Y创建Powershell会话的情况吗?

Also I guess I'm not understanding the concept of a session. 另外,我想我不了解会议的概念。 If you right click a ps1 file and select run with powershell, who is creating that session? 如果右键单击ps1文件并选择“使用powershell运行”,那么谁在创建该会话? The currently logged on user, or the creator of the script? 当前登录的用户,还是脚本的创建者?

If I have a .bat file that runs on login, and calls a ps1 file, who is the creator of that powershell session? 如果我有一个.bat文件,该文件在登录时运行并调用ps1文件,那么该powershell会话的创建者是谁?

Any help you can provide is greatly appreciated. 您能提供的任何帮助将不胜感激。

It runs as the user that powershell is currently running, even if it's different than the logged in user. 它以Powershell当前正在运行的用户身份运行,即使它与登录用户不同。

PS C:\Windows\system32> $env:username
jasonw

PS C:\Windows\system32> Start-Process powershell.exe -Credential "domain\admin.jasonw" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"
[This opens new window running PS as the user]

PS C:\Windows\system32> $env:username
admin.jasonw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM