我正在使用腳本來允許我的系統管理員對ACL進行更改,而不必深入到文件夾級別。到目前爲止,除了我的第一個開關中的第一個「If..Else」語句,一切都按預期執行。它完全被跳過,並繼續詢問帳戶名稱,我無法弄清楚原因。爲什麼我的「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
}
}
}
如果'$ test -eq $ false'它應該'中斷',而是繼續? –
@DavorMlinaric是的,當我期待其中一個寫主機顯示時,它會轉到下一個If語句。 – T4RH33L
我不知道這種語言,但可能是'break'只是在語句結束時退出,然後繼續。也許有其他方式來停止執行...'退出/結束開關'或其他... –