1

I'm creating a powershell script to repair or re-install a client remotely based upon the users input (basic script example below):

$env:COMPUTERNAME = Read-Host 'Please enter hostname'

$RepairReinstall = Read.Host '
1: Repair Client
2: Re-install Client

Answer'

If (RepairReinstall -eq 1) {

Write-Host "

Repair Started" 

}

If (RepairReinstall -eq 2) {

Write-Host "

Re-install Started" 

}

Else {

Write-Host "

[WARNING] Please select whether you'd like to repair or re-install the client"

}

How can I reload/restart the script within the Else { } statement; ideally I'd like to only restart from the $RepairReinstall prompt rather than prompting for the $env:COMPUTERNAME value again.

I have searched through SuperUser, ServerFault and Google but can't find a suitable command to perform this. Any help/advice is appreciated.

2

What you need is a do { ... } while( ... ) loop:

$env:COMPUTERNAME = Read-Host 'Please enter hostname'

Do {

    $RepairReinstall = Read.Host '
    1: Repair Client
    2: Re-install Client

    Answer'

    If (RepairReinstall -eq 1) {

        Write-Host "

        Repair Started" 

        break

    }

    If (RepairReinstall -eq 2) {

        Write-Host "

        Re-install Started" 

        break

    }

    Else {

        Write-Host "

        [WARNING] Please select whether you'd like to repair or re-install the client"

    }
}
While ($true)

This will loop your program until any of the two break statements gets executed

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy