2017-10-20 131 views
0

我有这个powershell命令,我用它来打开一个kitty/PuTTY会话。 格式是Powershell访问命令行参数

.\my_sshCommand three 

在这种情况下,应该带我去server.three,但无论我把什么命令行参数在需要我来server.TWO。这个想法是,如果命令行中没有参数,那么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

上次比较中的其他人正在让你失望。

由于三个不匹配'四',它击中其他,并发送给.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" 
     } 
    } 
}