我正在编写一个脚本,允许我的系统管理员对ACL进行更改,而无需深入到文件夹级别。到目前为止,除了我在第一个开关中的第一个“If..Else”语句外,一切都按预期执行。它被完全跳过并继续询问帐户名称,我无法弄清楚原因。
有没有人有任何想法?
$account = $null
$accesslevel = $null
$accesstype = $null
$acl = $null
$title = Write-Host "Modify ACL" -ForegroundColor Green
$message = Write-Host "Select the action to initiate:" -ForegroundColor Cyan
$add = New-Object System.Management.Automation.Host.ChoiceDescription "&Add Permissions", "Add Permissions"
$remove = New-Object System.Management.Automation.Host.ChoiceDescription "&Remove Permissions", "Remove Permissions"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($add, $remove)
$selectAction = $Host.UI.PromptForChoice($title, $message, $options, 0)
switch($selectAction){
0{
$pathPrompt = Write-Host "Please enter path to file/folder:" -ForegroundColor Green
$path = Read-Host
$test = Test-Path $path | Out-Null
if($test -eq $false){
Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
Break
}Else{
Write-Host "Getting ACL on`r"$path -ForegroundColor Green
$acl = get-acl $path
}
if($account -eq $null){
Write-Host "Enter Account (ex. Domain\Account)" -ForegroundColor Green
$account = Read-Host
}
$title2 = Write-Host "Permission Levels" -ForegroundColor Green
$message2 = Write-Host "Select the appropriate permissions to apply:" -ForegroundColor Cyan
$fullControl = New-Object System.Management.Automation.Host.ChoiceDescription "&FullControl", "FullControl"
$modify = New-Object System.Management.Automation.Host.ChoiceDescription "&Modify", "Modify"
$readExecute = New-Object System.Management.Automation.Host.ChoiceDescription "&ReadAndExecute", "ReadAndExecute"
$read = New-Object System.Management.Automation.Host.ChoiceDescription "&Read", "Read"
$write = New-Object System.Management.Automation.Host.ChoiceDescription "&Write", "Write"
$readWrite = New-Object System.Management.Automation.Host.ChoiceDescription "&Read, Write", "Read, Write"
$list = New-Object System.Management.Automation.Host.ChoiceDescription "&List", "List"
$options2 = [System.Management.Automation.Host.ChoiceDescription[]]($fullControl, $modify, $readExecute, $read, $write, $readWrite, $list)
do{
$selectAction2 = $Host.UI.PromptForChoice($title2, $message2, $options2, 1)
switch($selectAction2){
0{$accesslevel = 'FullControl'}
1{$accesslevel = 'Modify'}
2{$accesslevel = 'ReadandExecute'}
3{$accesslevel = 'Read'}
4{$accesslevel = 'Write'}
5{$accesslevel = 'Read, Write'}
6{$accesslevel = 'List'}
}
}Until($accesslevel -ne $null)
$title3 = Write-Host "Access Type" -ForegroundColor Green
$message3 = Write-Host "Select the type of access:" -ForegroundColor Cyan
$allow = New-Object System.Management.Automation.Host.ChoiceDescription "&Allow", "Allow"
$deny = New-Object System.Management.Automation.Host.ChoiceDescription "&Deny", "Deny"
$options3 = [System.Management.Automation.Host.ChoiceDescription[]]($allow, $deny)
do{
$selectAction3 = $Host.UI.PromptForChoice($title3, $message3, $options3, 0)
switch($selectAction3){
0{$accesstype = 'Allow'}
1{$accesstype = 'Deny'}
}
}Until($accesstype -ne $null)
Write-Host "Setting ACL on"$path -ForegroundColor Yellow
$arguments = $account, $accesslevel, $accesstype
Try{
$accessrule = New-Object System.Security.AccessControl.FileSystemAccessRule $arguments
$acl.SetAccessRule($accessrule)
}Catch{
Write-Host "Exception thrown : $($error[0].exception.message)"
}Finally{
$acl | set-acl $path
}
Write-Host "ACL settings have been completed." -ForegroundColor Cyan
}
1{
$pathPrompt
$path
$test | Out-Null
if($test -eq $false){
Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
Break
}Else{
Write-Host "Getting ACL on`r"$path -ForegroundColor Green
$acl = get-acl $path
}
if($account -eq $null){
$account = Read-Host "Enter Account (ex. Domain\Account)" -ForegroundColor Green
}
}
}
答案 0 :(得分:4)
您的if-else
正如您所写的那样正常工作。但是,你所写的并不是你想要的。
首先:在Write-Host
子句的else
中,您不想使用转义`r
;你想使用转义`n
,或者根本没有。 `r
表示返回行首,但不是下行行; `n
表示返回到开始行和下一行。在上面的示例中,以绿色重复输入的路径强烈暗示Write-Host
正在执行。
其次,您的Test-Path
导致$test
没有值,因为您将结果发送到空设备,而不是允许将其返回到语句以分配给变量。删除| Out-Null
。