[英]Powershell accessing command line arguments
I have this powershell command that I use to open up a kitty/PuTTY session. 我有此powershell命令,用于打开kitty / PuTTY会话。 THe format is
格式为
.\my_sshCommand three
Which in this case should take me to server.three, but no matter what command line argument I put in it takes me to server.TWO. 在这种情况下,哪个应该带我到server.three,但是无论我输入什么命令行参数,它都带我到server.TWO。 The idea is that if there are not parameters on the command line, then server.TWO will be the default.
这个想法是,如果命令行上没有参数,则server.TWO将是默认值。 I really don't know how to make a loop in powershell, so I just hardcode out the commands.
我真的不知道如何在Powershell中进行循环,所以我只是硬编码了这些命令。 I just cant figure why the command always fulfills the else clause no matter what command line argument I put in the script.
我只是想不出为什么不管我在脚本中输入什么命令行参数,命令总是执行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 capser@$Computer -pw pizza"
$Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
IF($Computer -match 'two') {
$Computer="server.TWO"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
IF($Computer -match 'three') {
$Computer="server.three"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
IF($Computer -match 'four') {
$Computer="server.four"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
ELSE {
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@server.TWO -pw pizza"
$Command="c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@server.TWO -pw pizza"
}
}
Invoke-Expression $Command
The else on the last comparison is tripping you up. 最后一个比较的其他情况会让您绊倒。
Since three doesn't match 'four', it's hitting the else, and sending you to .two. 由于三个与“四个”不匹配,因此将其击中其他,然后将您发送到.two。
Here's a solution using switch: 这是使用switch的解决方案:
param(
[array] $ComputerName
)
ForEach ($Computer in $ComputerName) {
switch ($computer) {
'one' {
$Computer = "server.one"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
'two' {
$Computer = "server.TWO"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
'three' {
$Computer = "server.three"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
'four' {
$Computer = "server.four"
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
$Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@$Computer -pw pizza"
}
default {
Write-Output "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@server.TWO -pw pizza"
$Command = "c:\Working\Kitty.exe -load `"capser profile`" -ssh capser@server.TWO -pw pizza"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.