Powershell:修复我的循环逻辑

时间:2017-04-07 14:09:10

标签: powershell

我试图让逻辑理顺一个循环,我需要继续提示用户输入有效的UNC路径。我设置为测试路径并输出到路径无效的控制台。但是,在那之后,它会回到我的选择提示。我想要它,要求用户输入另一个有效路径,然后再继续下一步。这是我的循环:

do{
    Write-Host ""                    
    $pathPrompt  = Write-Host "Please enter path to file/folder:" -ForegroundColor Cyan;
    $path        = Read-Host; 
    $test        = Test-Path $path

    if($test -eq $false){
        Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
    }
}until($test -eq $true){
    Write-Host ""
    Write-Host "Getting ACL on"$path -ForegroundColor Green
    Get-NTFSAccess -Path $path
}

我在这里错过或不做什么?

1 个答案:

答案 0 :(得分:1)

听起来您想要重复使用验证测​​试。你可以把它放在一个函数中重用:

Function Get-ValidPath {
    do {
        Write-Host "`r`nPlease enter path to file/folder:" -ForegroundColor Cyan
        $path = Read-Host
        $test = Test-Path $path

        if ($test -eq $false) {
            Write-Host "ERROR! Invalid Path!" -ForegroundColor Red
        }
    } until ($test -eq $true)
    $path
}
$validatedpath1 = Get-ValidPath
Write-Host "`r`nGetting ACL on $validatedpath1" -ForegroundColor Green
Get-NTFSAccess -Path $validatedpath1
$validatedpath2 = Get-ValidPath