我有一个文件夹结构脚本,它可以从Excel列表中创建带有权限的文件夹,在该列表中,有一列特定的权限,在这些列中,我想写下我想给予权限的用户名,脚本被编程为在Excel文件中一行一行地写。我已经将整个单元格转换为数组,并尝试使用foreach循环,但仍然没有效果,这是脚本的一部分。
$acl = Get-Acl $homedir
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"None"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($users, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.AddAccessRule($AccessRule)
所以 $users
这里是excel单元格,里面有用户名,它们是这样的。
user1,user2
现在foreach循环是一个很好的解决方案,但不知何故,它不工作,总是给人以 "Cannot convert value "user1" to type "System.Int32". Error: "Input string was not in a correct format"
谁能告诉我,我在这里做错了什么?
你试过这个吗?
$acl = Get-Acl $homedir
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"None"
foreach ($user in $users) {
$ace = New-Object Security.AccessControl.FileSystemAccessRule ("$user",$FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.AddAccessRule($ace)
}
谢谢大家,什么 怀疑论者 不得不使用foreach-object循环来代替foreach循环,工作代码。
$users | ForEach-Object{
$acl = Get-Acl $homedir
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Fullcontrol"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"None"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($_, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.setAccessRule($AccessRule)
$acl | Set-Acl "$homedir"
}