Powershell 命令在 Else 语句中重新启动/重新加载脚本

Powershell 命令在 Else 语句中重新启动/重新加载脚本

我正在创建一个 powershell 脚本来根据用户输入远程修复或重新安装客户端(下面的基本脚本示例):

$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"

}

我怎样才能重新加载/重新启动脚本别的 { }语句;理想情况下,我只想从$修复重新安装提示,而不是提示$env:计算机名值。

我搜索了 SuperUser、ServerFault 和 Google,但找不到合适的命令来执行此操作。任何帮助/建议都值得感激。

答案1

你需要的是一个do { ... } while( ... )循环:

$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)

这将循环你的程序,直到两个break语句中的任何一个被执行

相关内容