获取当前用户上下文

HW9*_*W90 6 powershell impersonation powershell-2.0

我在运行不同位置的powershellscript时遇到了问题(c#application,webservice ...).我认为这是一个用户上下文问题,所以现在我试图找出运行哪个用户上下文PowerShell脚本.

有没有可能记录powershellscript的当前usercontext?

Ste*_*fan 9

您可以使用WindowsIdentity类来获取当前线程用户:

[Security.Principal.WindowsIdentity]::GetCurrent()
Run Code Online (Sandbox Code Playgroud)


CB.*_*CB. 5

如果您需要了解实际用户:

[reflection.assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
[System.DirectoryServices.AccountManagement.UserPrincipal]::Current
Run Code Online (Sandbox Code Playgroud)

使用:

[System.DirectoryServices.AccountManagement.UserPrincipal]::Current | gm
Run Code Online (Sandbox Code Playgroud)

知道的可用属性/方法 UserPrincipal