【问题标题】:get Current user context获取当前用户上下文
【发布时间】:2012-06-22 08:40:06
【问题描述】:

我在从不同位置(c# 应用程序、webservice...)运行 powershellscript 时遇到问题。 我认为这是一个用户上下文问题,所以现在我试图找出在哪个用户上下文下运行 powershell 脚本。

是否有可能记录 powershellscript 的当前用户上下文?

【问题讨论】:

    标签: powershell powershell-2.0 impersonation


    【解决方案1】:

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

    [Security.Principal.WindowsIdentity]::GetCurrent()
    

    【讨论】:

    • 添加,[Security.Principal.WindowsIdentity]::GetCurrent().Name 将为您提供特定用户
    • 另一个更简单的命令是:whoami。这也将为您提供当前上下文用户
    【解决方案2】:

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

    [reflection.assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
    [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
    

    用途:

    [System.DirectoryServices.AccountManagement.UserPrincipal]::Current | gm
    

    了解UserPrincipal 的可用属性/方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 2019-04-22
      • 2020-05-05
      相关资源
      最近更新 更多