2

我正在尝试为某个注册表项设置权限,并为所有子项设置相同的权限。设置初始密钥的权限不是问题,但让其余的工作给我一个问题。

我为测试目的创建了以下内容: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 
    }
4

1 回答 1

2

您应该使用 PSPath 属性 [Tested on Windows 7 64-bit with Powershell ISE x86]-

$REG = Get-Acl HKLM:\SOFTWARE\1
$Dir = Get-Childitem "HKLM:\SOFTWARE\1" -Recurse

foreach ($Folder in $Dir)
{
     Write-Host $Folder
     Set-Acl $Folder.PSPath $Reg
}

您总是可以找出对象上支持的成员,如下所示 -

foreach ($Folder in $Dir) 
{    
  $Folder | Get-Member 

}

除了粘贴的内容之外,我不知道您是否正在使用 Get-ACL 进行任何其他预处理。

检查此链接 - http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/15/hey-scripting-guy-september-15-2009.aspx

于 2012-06-07T14:33:08.273 回答