A Loop of One

Of late, I've been seeing some of my scripts do this:

if (!(Test-Path file1)) {
    Write-Warning "File1 not found.";
} else {
    do-something
    if (!(Test-Something)) {
        Write-Warning "Test-Something failed";
    } else {
        do-something -part 2
        if (!(Test-Something -part 2)) {
            Write-Warning "Test-Something -part 2 failed";
        } else {
            do-something part 3;
            # etc
        }
    }
}

I can't do all my checks up front - I have to check for one thing, then write something, check the next, etc.  It's that steady rightward march that annoys me. 

One way to do that is something that is slightly Perl-ish in that it relies on 'side-effect scripting', but works better:

$true | % {

    if (!(Test-Path file1)) {
        Write-Warning "File1 not found.";
        break;
    }

    do-something
    if (!(Test-Something)) {
        Write-Warning "Test-Something failed";
        break;
    } 

    do-something -part 2
    if (!(Test-Something -part 2)) {
        Write-Warning "Test-Something -part 2 failed";
        break;
    } 

    do-something part 3;
    # etc

}

It's not the best, but it does give the idea that this is a series of steps and tests, and we bail out after any failure.