我有一个新的启动脚本在Powershell 2.0中工作正常,但我不得不升级到Powershell 3.0以获得一些SQL工作。但是,这会破坏我的脚本中使用Set-Acl的所有部分。使用PowerShell 2.0不是一种选择。有没有人找到解决方法: 我的代码:
#Set home directory permissions
$inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit
$propagation = [system.security.accesscontrol.PropagationFlags]"None"
$acl = Get-Acl $newfolder
$accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("$username", "FullControl", $inherit, $propagation, "Allow")
$acl.AddAccessRule($accessrule)
set-acl -aclobject $acl $newfolder
write-host permissions set
#Set home folder owner
$acl = Get-Acl $newfolder;
$domain = "mydomain"
$sid = New-Object System.Security.Principal.NTAccount("$domain\$username");
$acl.SetOwner($sid);
Set-Acl $newfolder $acl;
write-host owner set
答案 0 :(得分:0)
尝试用冒号绑定参数。
示例:
在:set-acl -aclobject $acl $newfolder
在:set-acl -aclobject:$acl -Path:$newfolder
注意:强> 使用$ newfolder的完整路径。
set-acl -aclobject:$acl -path:$newfolder.FullName
说明:Powershell cmdlet具有有序绑定和定位功能,使用冒号可确保将值分配给正确的参数,无论对象的顺序或类型如何。 .FullName
属性可以防止SetSecurityDescriptor
错误。
答案 1 :(得分:0)
万一其他人像我一样偶然发现了这个……
我遇到以下错误:
Set-Acl:找不到路径'C:\ WINDOWS \ system32 \ System.Security.AccessControl.DirectorySecurity',因为它不存在。
我必须实际指定Path和AclObject参数:
Set-Acl -Path C:\mydir -AclObject $acl