此脚本旨在使向新服务器应用权限变得更加容易。但目前它无法做到这一点 :(
出于某种原因,它在我制作它的 Win 8 机器上运行良好。当我在目标机器(2008 R2 服务器)上运行它时,它只是挂在第一个目录中。
不确定 PS 版本在 v3 和 v4 之间是否很重要 - 脚本相当简单: - Windows Server 2008 R2 框是 PowerShell v3 - Windows 8 框是 PowerShell v4
有谁知道这是为什么呢?
$csvFile = Read-Host "path to the CSV file with permissions"
$csv = Import-Csv $csvFile
Write-Host "Imported CSV File"
function applyPermissions {
Param(
[string]$Permission,
[string]$Group,
[string]$Dir,
[bool]$Enclosed
)
if ($Enclosed) {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Group,$Permission,"ObjectInherit, ContainerInherit","None","Allow")
$Dir = $Dir.Replace("\*","")
$acl = Get-Acl $Dir
$acl.SetAccessRule($rule)
Set-Acl $Dir $acl
}
else {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Group,$Permission,"ObjectInherit","NoPropagateInherit","Allow")
$acl = Get-Acl $Dir
$acl.SetAccessRule($rule)
Set-Acl $Dir $acl
}
}
foreach ($r in $csv) {
Write-Host "Processing: $r"
$perm = $r.Permission
$grp = $r.Group
$dr = $r.Path
# enclosed needs to apply recursively
$enc = $false
if($dr -match "\*") { $enc = $true } else { $enc = $false }
if ($perm -eq "ReadWrite") {
applyPermissions -Permission "ReadAndExecute, Write" -Group $grp -Dir $dr -Enclosed $enc
}
else {
applyPermissions -Permission $perm -Group $grp -Dir $dr -Enclosed $enc
}
}
编辑:
在 ISE 上添加一些断点后,我发现它在 applyPermissions 函数的 Set-Acl 部分被捕获
eventvwr 中似乎没有与此命令上 powershell 挂起的任何问题/原因相关的输出