質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

Q&A

0回答

2372閲覧

PowerShellでのレジストリアクセス権取得

wellwell

総合スコア5

PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

0グッド

0クリップ

投稿2021/01/11 02:28

編集2021/01/11 03:08

PowerShellでのレジストリアクセス権取得が出来なくて困っております。

レジストリキーの内、所有者がtrustedinstallerになっているものについて編集しようとした際、アクセス権で弾かれるのでアクセス権を取得しようと、下記ページのanswerに書いてある関数を試しました。
リンク内容

しかし、最初の$keyの取得でsecurity exceptionが発生し、失敗してしまいます。
当方ではこれ以上は対処法がわかりませんでした。
わかる方よろしくお願いします。

追記
試した関数

PowerShell

1 2function enable-privilege { 3 param( 4 ## The privilege to adjust. This set is taken from 5 ## http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx 6 [ValidateSet( 7 "SeAssignPrimaryTokenPrivilege", "SeAuditPrivilege", "SeBackupPrivilege", 8 "SeChangeNotifyPrivilege", "SeCreateGlobalPrivilege", "SeCreatePagefilePrivilege", 9 "SeCreatePermanentPrivilege", "SeCreateSymbolicLinkPrivilege", "SeCreateTokenPrivilege", 10 "SeDebugPrivilege", "SeEnableDelegationPrivilege", "SeImpersonatePrivilege", "SeIncreaseBasePriorityPrivilege", 11 "SeIncreaseQuotaPrivilege", "SeIncreaseWorkingSetPrivilege", "SeLoadDriverPrivilege", 12 "SeLockMemoryPrivilege", "SeMachineAccountPrivilege", "SeManageVolumePrivilege", 13 "SeProfileSingleProcessPrivilege", "SeRelabelPrivilege", "SeRemoteShutdownPrivilege", 14 "SeRestorePrivilege", "SeSecurityPrivilege", "SeShutdownPrivilege", "SeSyncAgentPrivilege", 15 "SeSystemEnvironmentPrivilege", "SeSystemProfilePrivilege", "SeSystemtimePrivilege", 16 "SeTakeOwnershipPrivilege", "SeTcbPrivilege", "SeTimeZonePrivilege", "SeTrustedCredManAccessPrivilege", 17 "SeUndockPrivilege", "SeUnsolicitedInputPrivilege")] 18 $Privilege, 19 ## The process on which to adjust the privilege. Defaults to the current process. 20 $ProcessId = $pid, 21 ## Switch to disable the privilege, rather than enable it. 22 [Switch] $Disable 23 ) 24 25 ## Taken from P/Invoke.NET with minor adjustments. 26 $definition = @' 27 using System; 28 using System.Runtime.InteropServices; 29 30 public class AdjPriv 31 { 32 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 33 internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, 34 ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); 35 36 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 37 internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok); 38 [DllImport("advapi32.dll", SetLastError = true)] 39 internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid); 40 [StructLayout(LayoutKind.Sequential, Pack = 1)] 41 internal struct TokPriv1Luid 42 { 43 public int Count; 44 public long Luid; 45 public int Attr; 46 } 47 48 internal const int SE_PRIVILEGE_ENABLED = 0x00000002; 49 internal const int SE_PRIVILEGE_DISABLED = 0x00000000; 50 internal const int TOKEN_QUERY = 0x00000008; 51 internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 52 public static bool EnablePrivilege(long processHandle, string privilege, bool disable) 53 { 54 bool retVal; 55 TokPriv1Luid tp; 56 IntPtr hproc = new IntPtr(processHandle); 57 IntPtr htok = IntPtr.Zero; 58 retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); 59 tp.Count = 1; 60 tp.Luid = 0; 61 if(disable) 62 { 63 tp.Attr = SE_PRIVILEGE_DISABLED; 64 } 65 else 66 { 67 tp.Attr = SE_PRIVILEGE_ENABLED; 68 } 69 retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid); 70 retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); 71 return retVal; 72 } 73 } 74'@ 75 76 $processHandle = (Get-Process -id $ProcessId).Handle 77 $type = Add-Type $definition -PassThru 78 $type[0]::EnablePrivilege($processHandle, $Privilege, $Disable) 79} 80 81enable-privilege SeTakeOwnershipPrivilege 82$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\powertoe",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::takeownership) 83# You must get a blank acl for the key b/c you do not currently have access 84$acl = $key.GetAccessControl([System.Security.AccessControl.AccessControlSections]::None) 85$me = [System.Security.Principal.NTAccount]"t-alien\tome" 86$acl.SetOwner($me) 87$key.SetAccessControl($acl) 88 89# After you have set owner you need to get the acl with the perms so you can modify it. 90$acl = $key.GetAccessControl() 91$rule = New-Object System.Security.AccessControl.RegistryAccessRule ("T-Alien\Tome","FullControl","Allow") 92$acl.SetAccessRule($rule) 93$key.SetAccessControl($acl) 94 95$key.Close() 96

目的
無人スリープタイムアウトを無効にするため
'HKLM:\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\381b4222-f694-41f0-9685-ff5bb260df2e\238c9fa8-0aad-41ed-83f4-97be242c8f20\7bc4a2f9-d8fc-4469-b07b-33eb785aaca0'
のキーを編集しようとしています。
多数のマシンを設定するのでコマンドでの実行が必須の状況です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2021/01/11 02:30

管理者として実行していないのでしょう。
wellwell

2021/01/11 02:34

ご回答ありがとうございます。 管理者での実行はやっております。 処理は全てps1に書いた上でrunasで実行しており、PowerShellのウィンドウ左上にも管理者と表示されていることを確認しております。
Zuishin

2021/01/11 02:48

どれを試したのか、また何の目的で何を書き換えようとしているのかわかりませんが、目的によっては .msi を使えばいいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問