简体   繁体   English

如何在powershell中更改另一个用户的特定注册表设置

[英]How to change specific registry setting for another user in powershell

Goal: 目标:

To edit a specific registry key setting for a specific user, and no others, in powershell. powershell中编辑特定用户的特定注册表项设置,而不是其他用户

Known: 已知:

OS: Windows 8.1 Embedded Industry Pro (Same as Win 8.1, but with some embedded features) 操作系统:Windows 8.1 Embedded Industry Pro(与Win 8.1相同,但具有一些嵌入式功能)

I can do this manually on the target machine by opening REGEDIT, selecting HKU, then click on File Menu, click on Load Hive, navigate to the user's profile directory, eg: c:\\users\\MrEd and when prompted, type in 'ntuser.dat' - import HKEY_CURRENT_USER. 我可以在目标机器上手动执行此操作,方法是打开REGEDIT,选择HKU,然后单击File Menu,单击Load Hive,导航到用户的配置文件目录,例如:c:\\ users \\ MrEd,当出现提示时,输入'ntuser .dat' - 导入HKEY_CURRENT_USER。 The Hive will be loaded into HKU where you can navigate and make necessary modifications. Hive将加载到HKU,您可以在其中导航并进行必要的修改。

Summary: 摘要:

I have a powershell script that returns the SID of the specific user, but when used in context of the registry hive, the hive"is not found" -- so I'm guessing I must be missing a step? 我有一个powershell脚本,它返回特定用户的SID,但是当在注册表配置单元的上下文中使用时,“找不到”配置单元 - 所以我猜我一定错过了一步? How does one "Load Hive" from Powershell? 如何从Powershell“加载蜂巢”? Or am I missing a special, magical, goats-entrails-on-keyboard incantation somewhere? 或者我在某个地方错过了一个特殊的,神奇的,山羊 - 内脏 - 键盘咒语?

Param(
    [string]$user= $null
)


Function GetSIDfromAcctName()
{
    Param(
        [Parameter(mandatory=$true)]$userName
    )
    $myacct = Get-WmiObject Win32_UserAccount -filter "Name='$userName'" 
    return $myacct.sid
}

if($user)
{
    $sid = GetSIDfromAcctName $user
    New-PSDrive HKU Registry HKEY_USERS
    $myHiveEntry = Get-Item "HKU:\${sid}"
    Write-Host "Key:[$myHiveEntry]"
}

Your existing code should work for a user whose hive is already loaded (like a currently logged in user), but it makes no attempt to load the hive. 您的现有代码应该适用于已经加载了配置单元的用户(如当前登录的用户),但它不会尝试加载配置单元。

I don't know of a way to make a programmatic call to load a hive, but you can shell out to reg.exe . 我不知道如何通过编程方式调用来加载配置单元,但是你可以将其发送到reg.exe

This ends up being kind of janky. 这最终变得有点笨拙。 It seems to have issues unloading the hive if it's in use anywhere, so I've put a bunch of crap in place in this sample to try to get rid of stuff that might be holding it open, but in my tests, it can take quite a while before the reg unload command is successful, hence the whole retry portion in the finally block. 如果它在任何地方都在使用,它似乎有卸载蜂巢的问题,所以我在这个样本中放了一堆垃圾来试图摆脱可能让它打开的东西,但在我的测试中,它可能需要在reg unload命令成功之前很长一段时间,因此finally块中的整个重试部分。

This is super unpolished, I just whipped it up on the spot. 这是超级未经修饰的,我只是当场鞭打它。

Function GetSIDfromAcctName()
{
    Param(
        [Parameter(mandatory=$true)]$userName
    )
    $myacct = Get-WmiObject Win32_UserAccount -filter "Name='$userName'" 
    return $myacct.sid
}

$user = 'someuser'

$sid = GetSIDfromAcctName -userName $user

$path = Resolve-Path "$env:USERPROFILE\..\$user\NTUSER.DAT"

try {
    reg load "HKU\$sid" $path 

    #New-PSDrive -Name HKUser -PSProvider Registry -Root "HKEY_USERS\$sid"

    #Get-ChildItem HKUser:\

    Get-ChildItem Registry::\HKEY_USERS\$sid

} finally {

    #Remove-PSDrive -Name HKUser

    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()

    $retryCount = 0
    $retryLimit = 20
    $retryTime = 1 #seconds

    reg unload "HKU\$sid" #> $null

    while ($LASTEXITCODE -ne 0 -and $retryCount -lt $retryLimit) {
        Write-Verbose "Error unloading 'HKU\$sid', waiting and trying again." -Verbose
        Start-Sleep -Seconds $retryTime
        $retryCount++
        reg unload "HKU\$sid" 
    }
}

This doesn't use a PS drive, but that code is in there too, commented out. 这不是使用PS驱动器,但该代码也在那里,注释掉了。

Note that if you don't name the hive mount point with the SID, you won't actually need the SID at all because you use the username to find the NTUSER.DAT file anyway. 请注意,如果没有使用SID命名hive挂载点,则根本不需要SID,因为无论如何都使用用户名来查找NTUSER.DAT文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM