2

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

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

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

4

1 回答 1

3

如果您是本地管理员组的成员,您可以简单地浏览所有用户注册表配置单元并在那里搜索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%
  }
}

Volatile Environment您可以通过搜索env 变量从注册表子树中获取相应的用户名USERNAME,也可以使用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
于 2019-01-03T17:07:29.970 回答