- This topic has 4 replies, 4 voices, and was last updated 2 months, 2 weeks ago by
July 2, 2020 at 10:05 am #239828ParticipantTopics: 2Replies: 7Points: 44Rank: Member
I’m converting a bat file to PowerShell and can’t figure out how to set permissions on a Registry Key. I’m trying to use subinacl.exe to do this.
Here is the command that I was using in the bat file.
"%~dp0subinacl.exe" /subkeyreg "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key" /grant="Everyone"=F
Here is what I am trying in PowerShell
$filespath = (Split-Path $MyInvocation.Mycommand.Path)
$testargsacl = @("/subkeyreg 'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key", "/grant="Everyone"=F")
Start-Process -filepath ($filespath + '\subinacl.exe') -ArgumentList $testargsacl -wait
At C:\Temp\Test\Install.ps1:21 char:109
+ … OFTWARE\Wow6432Node\Test Com'”, “/grant=”Everyon”=F”)
Unexpected token ‘Everyon”=F”‘ in expression or statement.
+ CategoryInfo : ParserError: (:) , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
July 3, 2020 at 2:21 am #239972Senior ModeratorTopics: 9Replies: 1309Points: 4,781Rank: Community Hero
You can use ACL cmdlets in PowerShell like below to achieve this.PowerShell1234$Acl = Get-Acl -Path $RegistryPath$ACLItem = New-Object -TypeName System.Security.AccessControl.RegistryAccessRule($UserName, [System.Security.AccessControl.RegistryRights]::ReadKey, "ContainerInherit,ObjectInherit", "None", "Allow")$Acl.SetAccessRule($ACLItem)Set-Acl -Path $RegistryPath -ACLObject $Acl
July 5, 2020 at 1:35 pm #240440ParticipantTopics: 6Replies: 20Points: 68Rank: Member
PowerShell returned and pointed to the error in the code.
Breaking down the code, please notice the error in the opening single quote (end of second ine) without a matching closing single quote, as follow:
$testargsacl = @("
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key", "/grant="Everyone"=F
July 7, 2020 at 6:14 pm #240917ParticipantTopics: 2Replies: 7Points: 44Rank: Member
Thanks, I tried closing the quote and it still fails. I cleaned up the quotes on the end of the variable so now i’m not getting an error in powershell when running this and it is now running the subinacl.exe but is failing because there is a space in the Registry Key path.
To be honest I’m having a hard time understanding how to deal with spaces in powershell. I created a Registry Key simply called Test and the code below does work but unfortunately the software I am working with has spaces in the Key name. Any idea what I can do to get it to work on a Key named “Test Key”?
$testargsacl = @(“/subkeyreg HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test”, “/grant=Everyone=F”)
Start-Process -filepath ($filespath + ‘\subinacl.exe’) -ArgumentList $testargsacl
July 8, 2020 at 1:22 am #240947ParticipantTopics: 7Replies: 557Points: 2,122Rank: Community Hero
I don’t understand why you aren’t trying the powershell way that kvprasoon provided. That said, I recommend you use splatting for your command arguments. This works for me.PowerShell12$cmdargs = @("/subkeyreg","HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key",'/grant="Everyone"=F').\subinacl.exe @cmdargs
outputPowerShell1234567SOFTWARE\Wow6432Node\Test Key : new ace for \everyoneHKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key : 1 change(s)Elapsed Time: 00 00:00:00Done: 1, Modified 1, Failed 0, Syntax errors 0Last Done : HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Test Key
- You must be logged in to reply to this topic.