0

I am trying to get the logic straightened out for a loop that I need to continue to prompt a user to enter a valid UNC path. I have it set to test the path and output to the console that the path is invalid. But, after that, it moves back to my prompt for choice. I want it to instead, ask the user to enter another valid path before moving on to the next step. Here is my loop:

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
}

What am I missing or not doing right here?

6
  • What you have written will loop until it gets a valid path, and then get the NTFS Access info on the path, at which point it will exit. If you want it to start over and ask for a new path, you should consider the possibility of wrapping your code in a loop. I should note that the code here strongly suggests that this is a homework problem, and it really is considered bad form to ask others to do your homework for you. Commented Apr 7, 2017 at 14:20
  • If is not homework, I assure you. I'm working on a tool that can get NTFS perms and then apply new or remove old perms, by choice. Commented Apr 7, 2017 at 14:34
  • You do not need to put $test in the condition. Directly Use (Test-path $path) then do the operation else not Commented Apr 7, 2017 at 14:38
  • @JeffZeitlin Here is the SO policy on homework questions, which are allowed. Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Which if this was homework, does meet those guidelines. Commented Apr 7, 2017 at 14:42
  • 1
    @RanadipDutta He needs to save the result as he is doing more than one test on it. The user feedback in the loop requires that he save the result. Commented Apr 7, 2017 at 15:45

1 Answer 1

1

Sounds like you want to reuse your validation test. You could put it in a function for reuse:

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
Sign up to request clarification or add additional context in comments.

5 Comments

I am still getting the same results. When I enter an invalid path, I get the error message and it returns to my prompt for choice, not the prompt to enter a path.
@T4RH33L So when it fails you don't want it to write "Please enter path.." just the "ERROR..."? Answer updated to only write the first time.
When it fails, I want it to say "ERROR! Invalid Path!" and then re-prompt the user to "Please enter path to file/folder:" until it gets a valid path.
@T4RH33L I don't know what you are asking for then, because that's exactly what this does...
I'm so sorry. I copied your function to one switch and tested it by selecting a switch that I didn't update. You are correct, your function worked perfectly. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.