powershell - 在 Windows Server 2008 R2 上将 HKCR\CLSID\* key 的所有者更改为管理员

标签 powershell permissions registry windows-server-2008-r2

Win Server 2008 R2 上有一个注册表项,

HKCR:\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}

其所有者不是管理员。它是 TrustedInstaller。现在制作 Remote DCOM/WMI 连接正常,我需要授予管理员权限才能拥有 完全控制此 key 和所有权。因为这需要在 几台机器,我希望我可以使用 Powershell 来做到这一点。我跟着 这些

Controlling Registry ACL Permissions with Powershell

Change the owner of directories with powershell

但是我还是报错

Exception calling "OpenSubKey" with "3" argument(s):
"Requested registry access is not allowed."

The code I am trying to run is simple

$key = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey(
  "CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}",
  [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,
  [System.Security.AccessControl.RegistryRights]::TakeOwnership
)
echo $key

关于如何更改此 key 的所有权的任何想法?我相信一旦拥有 更改为管理员,我将能够使用 Set-Acl 更改权限。

最佳答案

我能够使用以下脚本在 powershell 中实现这一点

# Checking OS Version and changing Registry Key permissions accordingly. We do need
# to change reg-key ownership for Win Server 2008, but in 2008 R2, owner of one of
# the required keys is TrustedInstaller instead of Administrator. Thus we need to
# change the owner back to Admin in order to make any changes to that key.
echo "Checking Operating System Version..."
$cv = (gi "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion")
$wv = $cv.GetValue("ProductName")
echo "$wv"
# Mounting HKey_ClassesRoot Registry key as a drive - Silent
New-PSDrive -name HKCR -PSProvider Registry -root HKEY_CLASSES_ROOT | Out-Null
$acl = Get-Acl "HKCR:\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}"
$owner = $acl.Owner
# Case 48188: Because Windows has server version like Windows Web Server 2008 R2, we
# cannot validate the version name using "Windows Server 2008 R2". We will only
# check if the name contains "Server 2008 R2".
if($wv.Contains("Server 2008 R2") -and !$owner.Contains("Administrators"))
{
  echo "Setting Administrators Group privileges in Windows Registry..."
  $boolResult = enable-privilege SeTakeOwnershipPrivilege
    if(-not $boolResult)
    {
      echo "Privileges could not be elevated. Changing ownership of the registry"
      echo "key would fail. Please change ownership of key"
      echo "HKCR\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6} to Administrators"
      echo "Group manually."
      return
    }
  $key = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey(
    "CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}",
    [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,
    [System.Security.AccessControl.RegistryRights]::takeownership
  )
  # You must get a blank acl for the key b/c you do not currently have access
  $acl = $key.GetAccessControl(
    [System.Security.AccessControl.AccessControlSections]::None
  )
  $owner = [System.Security.Principal.NTAccount]"Administrators"
  $acl.SetOwner($owner)
  $key.SetAccessControl($acl)

  # After you have set owner you need to get the acl with the perms so you can
  # modify it.
  $acl = $key.GetAccessControl()
  $person = [System.Security.Principal.NTAccount]"Administrators"
  $access = [System.Security.AccessControl.RegistryRights]"FullControl"
  $inheritance = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit"
  $propagation = [System.Security.AccessControl.PropagationFlags]"None"
  $type = [System.Security.AccessControl.AccessControlType]"Allow"

  $rule = New-Object System.Security.AccessControl.RegistryAccessRule(
    $person,$access,$inheritance,$propagation,$type
  )
  $acl.SetAccessRule($rule)
  $key.SetAccessControl($acl)

  $key.Close()
  echo "Administrators Group ownership privileges set."
}

关于powershell - 在 Windows Server 2008 R2 上将 HKCR\CLSID\* key 的所有者更改为管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6854207/

相关文章:

powershell - 使用 set-acl 和 powershell 设置继承和传播标志

Powershell - 有条件地向 cmdlet 添加参数/参数

permissions - 更改 MariaDB PID 文件的权限掩码

c++ - Windows 7 中的 COleTemplateServer::RegisterAll() 会发生什么

windows - 使用 .reg 文件将字符串值添加到注册表时出现问题

python - 使用 Powershell 通过 Web 请求从序列中检索数字

permissions - 简化 iOS 11 中的位置权限提示

permissions - 访问控制列表和访问控制对象,好的教程吗?

node.js - NPM 注册表未更新

excel - 仅从列表中获取唯一的条目