当 powershell 是 "runas" 不同的用户(以及不同的域)时,获取该用户的域,而不是登录用户的(在本地主机上)域

When powershell is "runas" different user (different domain as well), get that user's domain, and not the logged in user's (on localhost) domain

我知道如何获取您登录的用户的域 windows。那不是问题。 您可以使用 $env:USERDOMAIN(以及其他各种方式...)

我的情况:

我该怎么做才能获取通过 "runas" 登录的用户的域?

我没有测试此 atm 的资源。但这不正是 /netonly 参数的用途吗?

/netonly : Indicates that the user information specified is for remote access only.

来源:Runas @ TechNet

$env:USERDOMAIN 是本地进程中的本地 variable/run,因此它应该报告本地用户的域。但是,如果您在远程连接另一台计算机时检查该变量,则 runas 凭据应该用于网络身份验证(并且因此是远程会话)并且它有望 return DomainTwo。尝试:

Invoke-Command { $env:USERDOMAIN } -ComputerName computer2.domainone.com

runas-命令中删除 /netonly 应该会给你想要的输出。