windows - 访问 Windows 服务器上的特定用户环境变量

标签 windows powershell environment-variables

有没有办法使用 Powershell 访问特定的用户环境变量?

在服务器机器上,我们有 50 个用户。 我想知道特定用户(例如 user1)是否对环境变量 PYTH_HOME_LOG 使用不同的值。

此系统变量指向C:\PYTHON\LOG,我想通过配置用户环境变量PYTH_HOME_LOG 来检查哪个用户更改了此位置。

最佳答案

如果您是本地 Administrators 组的成员,您可以简单地浏览所有用户注册表配置单元并在那里搜索 Environment 变量:

$AllUserHives = Get-ChildItem Registry::HKEY_USERS\ |Where Name -like "HKEY_USERS\S-1-5-21*[0-9]"
foreach($UserHive in $AllUserHives){
  if(Get-ItemProperty -Path "$UserHive\Environment" -Name PYTH_HOME_LOG -EA SilentlyContinue){
    # User is overriding %PYTH_HOME_LOG%
  }
}

您可以通过搜索 USERNAME env 变量从 Volatile Environment 注册表子树中获取相应的用户名,或者您可以使用 IdentityReference.Translate() :

$SID = [System.Security.Principal.SecurityIdentifier]"S-1-5-21-436246-18267386-368368356-356777"
$Username = $SID.Translate([System.Security.Principal.NTAccount]).Value

或者,在这种情况下:

$SID = ($UserHive -split '\\')[-1] -as [System.Security.Principal.SecurityIdentifier]
$Username = $SID.Translate([System.Security.Principal.NTAccount]).Value

关于windows - 访问 Windows 服务器上的特定用户环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54023494/

相关文章:

c - 如何多播(发送)到第一个网卡?

c# - 命令按钮在 c# 客户端应用程序中不显示下划线快捷方式

c++ - 不是全局命名空间的成员?

powershell - 抑制导入模块的 VERBOSE

php - Laravel 5 环境总是 'Production' 而实际上是 'Local'

Windows 应用商店应用目前在 Windows 应用商店中不可用

xml - Powershell XML .GetAttribute() 和过滤器 $nulls

powershell - 无法替换字符串中的 "$"

c - 使用 environ 打印环境变量

linux - 为什么 env -i 与命令手册不匹配