使用系统帐户更新当前登录的用户HKEY_USERS配置单元注册表

时间:2018-09-17 08:47:40

标签: powershell registry sccm

此脚本对所有用户的个人资料进行更改。

以下是脚本:

# Get each user profile SID and Path to the profile
$UserProfiles = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*" |
                Where {$_.PSChildName -match "S-1-5-21-(\d+-?){4}$" } |
                Select-Object @{Name="SID"; Expression={$_.PSChildName}}, @{Name="UserHive";Expression={"$($_.ProfileImagePath)\NTuser.dat"}}

# Loop through each profile on the machine
foreach ($UserProfile in $UserProfiles) {
    # Load User ntuser.dat if it's not already loaded
    if (($ProfileWasLoaded = Test-Path Registry::HKEY_USERS\$($UserProfile.SID)) -eq $false) {
        Start-Process -FilePath "CMD.EXE" -ArgumentList "/C REG.EXE LOAD HKU\$($UserProfile.SID) $($UserProfile.UserHive)" -Wait -WindowStyle Hidden
    }
}

# Manipulate the registry
$key = "Registry::HKEY_USERS\$($UserProfile.SID)\Software\SomeArchaicSoftware\Configuration"

New-Item -Path $key -Force | Out-Null

New-ItemProperty -Path $key -Name "LoginURL" -Value "https://www.myCompany.local" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $key -Name "DisplayWelcome" -Value 0x00000001 -PropertyType DWORD -Force | Out-Null

$key = "$key\UserInfo"

New-Item -Path $key -Force | Out-Null

New-ItemProperty -Path $key -Name "LoginName" -Value "$($ENV:USERDOMAIN)\$($ENV:USERNAME)" -PropertyType STRING -Force | Out-Null

# Unload NTuser.dat 
if ($ProfileWasLoaded -eq $false) {
    [GC]::Collect()

    Start-Sleep 1
    Start-Process -FilePath "CMD.EXE" -ArgumentList "/C REG.EXE UNLOAD HKU\$($UserProfile.SID)" -Wait -WindowStyle Hidden| Out-Null
}

我只需要更改当前登录的用户HKEY_USERS配置单元。 谁能帮助我更改脚本,以便只有当前登录的用户才能更改?

1 个答案:

答案 0 :(得分:0)

您可以通过WMI确定当前登录用户的SID。检查正在运行的explorer.exe进程的所有者,然后将帐户名解析为其SID:

$user = (Get-WmiObject Win32_Process -Filter "Name='explorer.exe'").GetOwner()
$fltr = "Name='{0}' AND Domain='{1}'" -f $user.User, $user.Domain
$sid  = (Get-WmiObject Win32_UserAccount -Filter $fltr).SID

不过,我认为登录脚本是更改用户注册表设置的更好位置。