2017-03-31 244 views
0

我正在使用腳本來允許我的系統管理員對ACL進行更改,而不必深入到文件夾級別。到目前爲止,除了我的第一個開關中的第一個「If..Else」語句,一切都按預期執行。它完全被跳過,並繼續詢問帳戶名稱,我無法弄清楚原因。爲什麼我的「If Else」語句被跳過?

enter image description here

有沒有人有什麼想法?

$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

如果'$ test -eq $ false'它應該'中斷',而是繼續? –

+0

@DavorMlinaric是的,當我期待其中一個寫主機顯示時,它會轉到下一個If語句。 – T4RH33L

+0

我不知道這種語言,但可能是'break'只是在語句結束時退出,然後繼續。也許有其他方式來停止執行...'退出/結束開關'或其他... –

回答

4

您的if-else正如您所寫的那樣正常工作。然而,你寫的東西並不是你想要的。

第一條:在Write-Host中的else條款中,您不希望使用轉義的`r;你想使用一個轉義的`n,或者什麼也沒有。 `r表示返回行開始但不轉到下一行; `n表示返回到行開始行並轉到下一行。在上面的示例中,以綠色重複輸入的路徑暗示Write-Host正在執行。

其次,您的Test-Path導致$test沒有任何價值,因爲您將結果發送給空設備,而不是將其返回給語句以分配給變量。刪除| Out-Null

+0

@Matt - 謝謝 - 我無法弄清楚(或找到一個參考),以逃避反向,以便它會出現在你編輯它。 –

+0

是的。當我需要參考時,我總是很討厭。你越瞭解¸。⭐ – Matt

+0

@JeffZeitlin謝謝!它現在工作完美。 – T4RH33L