【问题标题】:Powershell accessing command line argumentsPowershell 访问命令行参数
【发布时间】:2017-10-20 01:23:18
【问题描述】:

我有这个用于打开 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 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

【问题讨论】:

    标签: powershell command line


    【解决方案1】:

    最后一个比较中的 else 让你大吃一惊。

    由于 3 不匹配 '4',它会命中 else,并将您发送到 .two。

    这是一个使用 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"
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2019-08-05
      • 2022-01-19
      • 2012-09-01
      • 2019-06-21
      相关资源
      最近更新 更多