[英]PowerShell script to remove TDL key registry
我在脚本中工作,从Windows 10注册表中删除TDL密钥。 他们在:
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileNotification
HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows NT\\CurrentVersion\\ProfileNotification
所有计算机的Windows版本都是Windows 10 Pro 1809.这些计算机位于Windows Server域中,有35台计算机。
CLS
# AMBAS RUTAS AL REGISTRO, elimino el comentario segun requiero
$keyLocation = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileNotification\'
# $keyLocation = 'SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\ProfileNotification\'
$definition = @"
using System;
using System.Runtime.InteropServices;
namespace Win32Api
{
public class NtDll
{
[DllImport("ntdll.dll", EntryPoint="RtlAdjustPrivilege")]
public static extern int RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool Enabled);
}
}
"@
# Defino variables
$computerName = $env:computername | Select-Object
$UserName = 'administrador'
$computerUser = $computerName + '\' + $UserName
$keyInitPath = 'HKLM'
$keyName = 'TDL'
$keyPath = $keyInitPath + ':\' + $keyLocation
$keyPathComplete = $keyPath + $keyName
$keyNameLocation = $keyLocation + $keyName
Add-Type -TypeDefinition $definition -PassThru
$bEnabled = $false
# Tomo posesión de la clave TDL
$res = [Win32Api.NtDll]::RtlAdjustPrivilege(9, $true, $false, [ref]$bEnabled)
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyLocation + $keyName, [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership)
$acl = $key.GetAccessControl()
$acl.SetOwner([System.Security.Principal.NTAccount]$computerUser)
# SET Permisos a TDL para administradores
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($keyNameLocation,[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$rule = New-Object System.Security.AccessControl.RegistryAccessRule (".\administradores","FullControl",@("ObjectInherit","ContainerInherit"),"None","Allow")
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)
# Elimino la clave TDL
Remove-Item -Path $keyPathComplete
问题是当我在另一台计算机上运行它时,它们会在同一环境中显示ACCESS.DENIED。 在开发计算机中,没有找到错误,所有都是作为一个咒语执行,但在另一个咒语中,这显示了一个ACCESS DENIED错误,但不是在TAKE OWNER段中,而是在REMOVE段中。
我通过Key throug注册表项,没有任何修改,也没有PowerShell中的错误。 我不明白这一点。
可能是您在开发时在源计算机的注册表中修改了权限。 我不知道......代码似乎还可以,但......
注意:我作为提升的特权执行。 (对不起我的英语不好)
inheritanceFlags
以及propagationFlags
参数是Flags Enum ,意味着应该将各种选项添加(或“OR-ed”)在一起以形成按位组合。
在您的代码中,您将它们设置为数组@("ObjectInherit","ContainerInherit")
,因此权限未设置为您期望的权限。
然后,当您尝试删除密钥时,您将获得“拒绝访问”,因为密钥内的属性的权限不会从密钥继承。
实际上,您需要包含单个值
[System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
在PowerShell中,您只需使用逗号分隔的字符串:
"ContainerInherit, ObjectInherit"
尝试
$rule = New-Object System.Security.AccessControl.RegistryAccessRule(".\administradores","FullControl","ContainerInherit, ObjectInherit","None","Allow")
希望有所帮助
请注意,也可以将数值按位OR
,如[System.Security.AccessControl.InheritanceFlags]::ObjectInherit.value__ -bor [System.Security.AccessControl.InheritanceFlags]::ContainerInherit.value__
,这将等于3
。 但是,这是不可取的,因为它使代码的可读性降低。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.