【问题标题】:Why Is my "If Else" statement getting skipped?为什么我的“If Else”语句被跳过?
【发布时间】:2017-08-25 17:46:16
【问题描述】:

我正在编写一个脚本,以允许我的系统管理员对 ACL 进行更改,而无需深入到文件夹级别。到目前为止,除了我在第一个 switch 中的第一个“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,它应该是break,而是继续?
  • @DavorMlinaric 是的,当我应该期待一个 Write-Host 显示时,它会转到下一个 If 语句。
  • 我不懂这种语言,但可能是 break 只是退出 if 语句,然后继续。也许还有其他方法可以停止执行...exit/end switch 或其他什么...
  • @DavorMlinaric 我刚刚通过删除中断对其进行了测试,但它仍然跳过了整个语句。

标签: powershell


【解决方案1】:

您的if-else 工作正常,正如您所写的那样。然而,你写的不是你想要的。

首先:在else子句中的Write-Host中,你不想使用转义的`r;您想使用转义的`n,或者根本不使用。 `r 表示返回到行首但不是转到下一行; `n 表示返回到开始行并转到下一行。在上面的示例中以绿色重复输入的路径强烈暗示 Write-Host 正在被执行。

其次,您的Test-Path 导致$test 没有值,因为您将结果发送到空设备,而不是允许它返回到语句以分配给变量。删除| Out-Null

【讨论】:

  • @Matt - 谢谢 - 我想不出(或找到参考)来逃避反引号,以便它在您编辑时出现。
  • 是的。当我需要引用它时,我总是讨厌。你知道的越多¸.·⭐
猜你喜欢
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多