2017-10-20 8 views
0

kitty/PuTTY 세션을 여는 데 사용하는 powershell 명령이 있습니다. 형식이 경우는 server.TWO에 저를 필요에 내가 무엇을 넣어 명령 행 인수에 상관없이 server.three 저를 가지고,하지만해야Powershell이 ​​명령 줄 인수에 액세스 중

.\my_sshCommand three 

입니다. 명령 줄에 매개 변수가 없으면 server.TWO가 기본값이됩니다. PowerShell에서 루프를 만드는 방법을 모르므로 명령을 하드 코드합니다. 나는 왜 명령이 스크립트에 입력 한 명령 줄 인수에 관계없이 항상 else 절을 ​​수행하는지 설명하지 않습니다.

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 

답변

0

마지막으로 비교했을 때 다른 사람이 당신을 걸러냅니다.

3 명이 '4'와 일치하지 않으므로 다른 사람을 때리고, .two로 보냅니다.

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" 
     } 
    } 
} 
:

는 여기에 솔루션을 사용하여 스위치의