我正在尝试为某个注册表项设置权限,并为所有子项设置相同的权限。设置初始密钥的权限不是问题,但让其余的工作给我一个问题。
我为测试目的创建了以下内容:HKLM\Software\1 和子文件夹 2\3。
$REG = Get-Acl HKLM:\SOFTWARE\1
$Rule = New-Object System.Security.AccessControl.RegistryAccessRule ("Everyone","FullControl","Allow")
$REG.SetAccessRule($Rule)
$REG |Set-Acl -Path HKLM:\SOFTWARE\1
$Dir = Get-Childitem "HKLM:\SOFTWARE\1" -Recurse
foreach ($Folder in $Dir)
{
Write-Host $Folder
Set-Acl $Folder $Reg
}
当我执行 Write-Host $Folder 时,我得到了正确的子项,但是当它把它推到 Set-ACL 时,我得到以下错误:
Set-Acl : Cannot find path 'C:\Documents and Settings\USER\Desktop\HKEY_LOCAL_MACHINE\SOFTWARE\1\2\3' because it does not exist.
工作解决方案:
$REG = Get-Acl HKLM:\SOFTWARE\1
$Rule = New-Object System.Security.AccessControl.RegistryAccessRule ("Everyone","FullControl","Allow")
$REG.SetAccessRule($Rule)
$REG |Set-Acl -Path HKLM:\SOFTWARE\1
$Dir = Get-Childitem "HKLM:\SOFTWARE\1" -Recurse
foreach ($Folder in $Dir)
{
$REG.SetAccessRule($Rule)
$Reg | Set-Acl $Folder.PSPath
}