【问题标题】:Multiple powershell switch parameters - can it be optimized?多个powershell开关参数-可以优化吗?
【发布时间】:2018-07-31 05:10:16
【问题描述】:

我正在尝试编写一个简单的包装器,它接受一个输出参数。 这就是现在的样子

function Get-data{
param (
    [switch]$network,
    [switch]$profile,
    [switch]$server,
    [switch]$devicebay
 )

    if ($network.IsPresent) { $item = "network"}
    elseif ($profile.IsPresent) {$item = "profile"}
    elseif ($server.IsPresent) {$item = "server"}
    elseif ($devicebay.IsPresent){$item = "devicebay"}

    $command = "show $item -output=script2"
}

显然这可以优化,但我正在努力思考如何实现它。是否有一些简单的方法可以确保只接受和使用单个参数而不使用多个 elseif 语句?
此外,我想提供一系列参数,而不是按照目前的方式进行操作。

【问题讨论】:

    标签: powershell parameters switch-statement


    【解决方案1】:

    您可以做的另一件事是使用[ValidateSet] 来代替所有这些开关参数

    function Get-Data{
        [cmdletbinding()]
    
        param(
            [Parameter(Mandatory=$true)]
            [ValidateSet('Network','Profile','Server','DeviceBay')]
            [string]$Item
        )
    
        Switch ($Item){
            'network' {'Do network stuff'}
            'profile' {'Do profile stuff'}
            'server' {'Do server stuff'}
            'devicebay' {'Do devicebay stuff'}
        }
    }
    

    【讨论】:

      【解决方案2】:

      可能不是最优雅的解决方案,但使用参数集可以让 powershell 为您完成一些工作:

      #requires -version 2.0
      
      function Get-data {
          [cmdletbinding()]
      
          param(
              [parameter(parametersetname="network")]
              [switch]$network,
              [parameter(parametersetname="profile")]
              [switch]$profile,
              [parameter(parametersetname="server")]
              [switch]$server,
              [parameter(parametersetname="devicebay")]
              [switch]$devicebay
          )
      
          $item = $PsCmdlet.ParameterSetName
      
          $command = "show $item -output=script2"
      }
      

      如果您不提供其中一个开关,则此示例将出错,但如果您想考虑这种情况,您可能可以提供一个额外的开关,该开关不执行任何操作或更优雅地出错...

      【讨论】:

        【解决方案3】:

        您可以添加 [cmdletbinding()] 关键字以获得 $PSBoundParameters,并将其用于切换管道:

        function Get-data{
            [cmdletbinding()]
        
            param (
                [switch]$network,
                [switch]$profile,
                [switch]$server,
                [switch]$devicebay
             )
             Switch ($PSBoundParameters.GetEnumerator().
              Where({$_.Value -eq $true}).Key)
             {
               'network'    { 'Do network stuff' }
               'profile'    { 'Do profile stuff'  }
               'server'     { 'Do server stuff' }
               'devicebay'  { 'Do devicebay stuff' }
             }
        }
        

        【讨论】:

          【解决方案4】:

          由于您只想启用一个开关,因此枚举可能会对您有所帮助。 这样,您使用的不是开关而是标准参数 - 尽管如此,cmdlet 的用户仍可以使用 TAB 自动完成可能输入的值。

          只需将参数的类型设置为您的枚举即可。

          【讨论】:

          • 而且,它可以很好地接受枚举列表(与 OP 的情况不同),以执行多个操作。简单、优雅!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-24
          • 1970-01-01
          相关资源
          最近更新 更多