本帖最后由 WHY 于 2023-2-18 23:14 编辑
参考:https://www.likecs.com/ask-1396155.html
第4行是需要修改的注册表项路径,第5行是修改以后的所有者名称。
如果不需要连同子项一起修改,删除最后一行 Get-SubKeys $subKey;
2023/02/18 修复Win7(PowerShell v2.0)报错- <# :
- @echo off
- REM 右键以管理员身份运行
- set "regPath=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Classes\CLSID\{FE38753A-44A3-11D1-B5B7-0000C09000C4}"
- set "OwnerName=NT SERVICE\TrustedInstaller"
- REM set "OwnerName=Administrators"
- PowerShell ". ([ScriptBlock]::Create((gc -Literal '%~f0') -join \"`r`n\")) '%regPath%' '%OwnerName%'"
- pause & exit
- #>
-
- param([string]$regPath, [Security.Principal.NTAccount]$owner);
-
- $Code = @'
- using System;
- using System.Security.Principal;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- namespace WinAPI{
- public static class Program{
- public static bool ModifyPrivilege(PrivilegeName privilege, bool enable){
- LUID luid;
- if (!LookupPrivilegeValue(null, privilege.ToString(), out luid)){
- throw new Win32Exception();
- }
- using (WindowsIdentity identity = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges|TokenAccessLevels.Query)){
- TOKEN_PRIVILEGES newPriv;
- newPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
- newPriv.PrivilegeCount = 1;
- newPriv.Privileges[0].Luid = luid;
- newPriv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
-
- TOKEN_PRIVILEGES prevPriv;
- prevPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
- prevPriv.PrivilegeCount = 1;
- uint returnedBytes;
-
- if (!AdjustTokenPrivileges(identity.Token, false, ref newPriv, (uint) Marshal.SizeOf(prevPriv), ref prevPriv, out returnedBytes)){
- throw new Win32Exception();
- }
- return prevPriv.PrivilegeCount == 0 ? enable : ((prevPriv.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED) != 0);
- }
- }
-
- const uint SE_PRIVILEGE_ENABLED = 2;
-
- [DllImport("advapi32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLengthInBytes, ref TOKEN_PRIVILEGES PreviousState, out UInt32 ReturnLengthInBytes);
-
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
-
- struct TOKEN_PRIVILEGES {
- public UInt32 PrivilegeCount;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1 /*ANYSIZE_ARRAY*/)]
- public LUID_AND_ATTRIBUTES[] Privileges;
- }
-
- [StructLayout(LayoutKind.Sequential)]
- struct LUID_AND_ATTRIBUTES {
- public LUID Luid;
- public UInt32 Attributes;
- }
-
- [StructLayout(LayoutKind.Sequential)]
- struct LUID {
- public uint LowPart;
- public int HighPart;
- }
- }
-
- public enum PrivilegeName {
- SeAssignPrimaryTokenPrivilege,
- SeAuditPrivilege,
- SeBackupPrivilege,
- SeChangeNotifyPrivilege,
- SeCreateGlobalPrivilege,
- SeCreatePagefilePrivilege,
- SeCreatePermanentPrivilege,
- SeCreateSymbolicLinkPrivilege,
- SeCreateTokenPrivilege,
- SeDebugPrivilege,
- SeEnableDelegationPrivilege,
- SeImpersonatePrivilege,
- SeIncreaseBasePriorityPrivilege,
- SeIncreaseQuotaPrivilege,
- SeIncreaseWorkingSetPrivilege,
- SeLoadDriverPrivilege,
- SeLockMemoryPrivilege,
- SeMachineAccountPrivilege,
- SeManageVolumePrivilege,
- SeProfileSingleProcessPrivilege,
- SeRelabelPrivilege,
- SeRemoteShutdownPrivilege,
- SeRestorePrivilege,
- SeSecurityPrivilege,
- SeShutdownPrivilege,
- SeSyncAgentPrivilege,
- SeSystemEnvironmentPrivilege,
- SeSystemProfilePrivilege,
- SeSystemtimePrivilege,
- SeTakeOwnershipPrivilege,
- SeTcbPrivilege,
- SeTimeZonePrivilege,
- SeTrustedCredManAccessPrivilege,
- SeUndockPrivilege,
- SeUnsolicitedInputPrivilege,
- }
- }
- '@
-
- Add-Type -TypeDefinition $Code;
- [WinAPI.Program]::ModifyPrivilege('SeRestorePrivilege', $true);
- [WinAPI.Program]::ModifyPrivilege('SeTakeOwnershipPrivilege', $true);
-
- Switch -regex ($regPath) {
- '^(?:HKLM|HKEY_LOCAL_MACHINE)' { $root = 'LocalMachine' }
- '^(?:HKCU|HKEY_CURRENT_USER)' { $root = 'CurrentUser' }
- '^(?:HKCR|HKEY_CLASSES_ROOT)' { $root = 'ClassesRoot' }
- '^(?:HKCC|HKEY_CURRENT_CONFIG)'{ $root = 'CurrentConfig' }
- '^(?:HKU|HKEY_USERS)' { $root = 'Users' }
- }
- $subKey = $regPath.Split('\', 2)[1];
-
- Function Set-Owner($subKey){
- $objKey = [Microsoft.Win32.Registry]::$root.OpenSubKey($subKey,'ReadWriteSubTree', 'TakeOwnership');
- $acl = $objKey.GetAccessControl();
- $acl.SetOwner($owner);
- $objKey.SetAccessControl($acl);
- $objKey.Close();
- }
-
- Function Get-SubKeys($subKey){
- $objKey = [Microsoft.Win32.Registry]::$root.OpenSubKey($subKey);
- $arrName = $objKey.GetSubKeyNames();
- $objKey.Close();
- forEach( $subName In $arrName ){
- Set-Owner ($subKey + '\' + $subName);
- Get-SubKeys ($subKey + '\' + $subName);
- }
- }
-
- Set-Owner $subKey;
- Get-SubKeys $subKey;
复制代码
|