PowerShell循环直到变量

时间:2019-01-09 12:27:34

标签: powershell loops

我有一段简单的代码,我想循环直到它到达用户给定的变量(即,如果用户说3次,则循环3次)

$quantidade = Read-Host -Prompt 'NUMERO DE INSTANCIAS'
$loop = 0
while ($loop -ne $quantidade) {
    $Servers = Read-Host -Prompt 'NOME SQL'
    $Version = Read-Host -Prompt 'VERSAO SQL? (2014,2012,...'

    if ($Version -eq "2014") {
        Write-Host "VERSAO 2014"
        $VERSQL = "12"
    } elseif ($Version -eq "2008") {
        Write-Host "VERSAO 2008"
        $VERSQL = "10"
    } elseif ($Version -eq "2012") {
        Write-Host "VERSAO 2012"
        $VERSQL = "11"
    } elseif ($Version -eq "2016") {
        Write-Host "VERSAO 2016"
        $VERSQL = "13"
    } elseif ($Version -eq "2017") {
        Write-Host "VERSAO 2017"
        $VERSQL = "14"
    } else {
        Write-Host "ERRO"
        break
    }

    Write-Host "You input server '$Servers' and '$Version'"

    $SQL = -join("%ProgramFiles%\Microsoft SQL Server\MSSQL", $VERSQL, ".", $Servers, "\MSSQL\Binn\sqlservr.exe")

    Write-Host $SQL
    $loop = $lopp + 1
    Write-Host $loop
}

我知道怎么了,总和在循环内完成,使其始终为0 + 1 = 1。解决这个问题的更好方法是什么?

1 个答案:

答案 0 :(得分:0)

解决方案(我很努力地在你们的帮助下完成了工作,尽管我很希望能切换到与此一起工作)

$Quantidade = Read-Host -Prompt 'NUMERO DE INSTANCIAS'

for ($i=1;$i -le $quantidade;$i++) {

$Servers = Read-Host -Prompt 'NOME SQL'
$Version = Read-Host -Prompt 'VERSAO SQL? (2014,2012,...'

if($Version -eq "2014"){
Write-Host "VERSAO 2014"
$VERSQL="12"
}
elseif($Version -eq"2008"){
Write-Host "VERSAO 2008"
$VERSQL="10"
}
elseif($Version -eq"2012")
{Write-Host "VERSAO 2012"
$VERSQL="11"
}
elseif($Version -eq"2016"){
Write-Host "VERSAO 2016"
$VERSQL="13"
}
elseif($Version -eq"2017")
{Write-Host "VERSAO 2017"
$VERSQL="14"
}
else{
Write-Host "ERRO"
break
}