2017-10-20 39 views
0

J'ai cette commande powershell que j'utilise pour ouvrir une session Kitty/PuTTY. Le format estPowershell accédant aux arguments de la ligne de commande

.\my_sshCommand three 

dans ce cas devrait me conduire à server.three, mais peu importe quel argument en ligne de commande je mets qu'il me faut pour server.TWO. L'idée est que s'il n'y a pas de paramètres sur la ligne de commande, alors server.TWO sera la valeur par défaut. Je ne sais vraiment pas comment faire une boucle en PowerShell, donc j'ai juste codé en dur les commandes. Je n'arrive pas à comprendre pourquoi la commande remplit toujours la clause else quel que soit l'argument de la ligne de commande que j'ai mis dans le script.

param(
    [array] $ComputerName 
) 



ForEach ($Computer in $ComputerName) { 
    IF($Computer -match 'one') { 
    $Computer="server.one" 
    Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    $Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    } 
    IF($Computer -match 'two') { 
    $Computer="server.TWO" 
    Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    $Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    } 
    IF($Computer -match 'three') { 
    $Computer="server.three" 
    Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    $Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    } 
    IF($Computer -match 'four') { 
    $Computer="server.four" 
    Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    $Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
    } 
    ELSE { 
    Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected] -pw pizza" 
    $Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected] -pw pizza" 
    } 
} 



Invoke-Expression $Command 

Répondre

0

L'autre sur la dernière comparaison vous fait trébucher.

Puisque trois ne correspondent pas à 'quatre', il frappe l'autre, et vous envoie à.

est ici une solution en utilisant le commutateur:

param(
    [array] $ComputerName 
) 

ForEach ($Computer in $ComputerName) { 
    switch ($computer) { 
     'one' { 
      $Computer = "server.one" 
      Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
      $Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
     } 
     'two' { 
      $Computer = "server.TWO" 
      Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
      $Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
     } 
     'three' { 
      $Computer = "server.three" 
      Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
      $Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
     } 
     'four' { 
      $Computer = "server.four" 
      Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
      $Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected]$Computer -pw pizza" 
     } 
     default { 
      Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected] -pw pizza" 
      $Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh [email protected] -pw pizza" 
     } 
    } 
}