有没有办法使用 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/