【问题标题】:How do I suppress the errors that occur when a user calls my powershell script without a parameter argument当用户在没有参数参数的情况下调用我的 powershell 脚本时,如何抑制发生的错误
【发布时间】:2013-01-08 19:48:52
【问题描述】:

例如,我的脚本可以这样调用:

.\MyScript.ps1 -s <hostname1>

如果我在没有使用 -s 参数传递参数的情况下调用它,我会收到错误:

.\MyScript.ps1 -s

C:\MyScript.ps1 : Missing an argument for parameter 'sql'. Specify a pa
rameter of type 'System.String' and try again.
At line:1 char:18
+ .\MyScript.ps1 -s <<<<
    + CategoryInfo          : InvalidArgument: (:) [MyScript.ps1],     ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,MyScript.ps1

有什么办法可以抑制这个错误,或者让自定义错误出现:

.\MyScript.ps1 -s

Please pass a hostname with the s argument:
.\MyScript.ps1 -s <hostname>

【问题讨论】:

    标签: powershell parameters arguments parameter-passing powershell-2.0


    【解决方案1】:

    你可以做两件事中的一件。

    将该参数标记为必填,然后powershell会自动向用户查询:

    PARAM(  [Parameter(Mandatory=$true)][string]$sql )
    PROCESS
    {
        "You entered: " + $sql
    }
    

    给出:

    # C:\Temp> .\prompt.ps1 
    cmdlet prompt.ps1 at command pipeline position 1
    Supply values for the following parameters:
    sql: asdsaa
    You entered: asdsaa
    

    或者您可以提供一个表达式作为查询用户输入的默认参数:

    PARAM(  [string]$sql = (read-host "Enter a value for sql parameter") )
    
    PROCESS
    {
        "You entered: " + $sql
    }
    

    给出:

    # C:\Temp> .\prompt.ps1 
    Enter a value for sql parameter: hello
    You entered: hello
    

    编辑:

    回应您的评论。我能想到的获得您想要的行为的唯一解决方法是不指定参数并自己处理 $args 参数。或者,您可以使用一种技巧来推动工作将参数解析为单独的函数,然后捕获调用该函数时可能发生的任何错误。因此,您的脚本将如下所示:

    function parsePrompt ( [string]$sql)
    {
        $sql
    }
    
    $cmd = "parsePrompt $($args -join " ")"
    try
    {
        $sql = invoke-expression $cmd
    }
    catch [System.Management.Automation.ParameterBindingException]
    {
        $sql = read-host "Enter a value for sql parameter"
    }
    "You entered: " + $sql
    

    给出:

    # C:\Temp> .\Prompt.ps1 -s
    Enter a value for sql parameter: dsfds
    You entered: dsfds
    
    
    # C:\Temp> .\Prompt.ps1 -s dfds
    You entered: dfds
    

    【讨论】:

    • 解决方案不起作用。如果脚本像这样传递“.\prompt.ps1”,它就可以工作;但我试图抑制像这样运行脚本时出现的警报“.\prompt.ps1 -s”
    • @EGr 查看我发布的替代方案。
    【解决方案2】:

    您可以尝试使用 陷阱处理程序 在未传递参数时提供自定义消息,然后从那里继续。陷阱处理程序适用于当前范围,因此您只需在脚本顶部定义它即可处理您的错误。

    例如:

    trap [ArgumentException] {
        write-host "Please pass a hostname with the s argument"
        break
    }
    

    您还可以通过设置

    告诉 PowerShell 在此脚本中发生错误时不要警告您

    $ErrorActionPreference = SilentlyContinue

    这将使 PowerShell 安静地抑制错误并尝试继续执行脚本。但是,出于显而易见的原因,隐藏错误可能不是最好的主意。

    【讨论】:

      【解决方案3】:

      Param([parameter(mandatory =$true, helpmessage="you silly goat.")] $SQL)

      请原谅简洁,在电话上;)

      【讨论】:

        【解决方案4】:

        如果我正确地阅读了您的意图,您想要错误,而不是提示......所以:

        # MyScript
        param (
            $sql = $(throw @"
            Oops!
            Dude, this script requires SQL parameter!
            Run it like that: .\MyScript -s <hostname>
        "@)
        )
            "Got $sql"
        }
        

        您可以使用 v1 “强制”参数。

        【讨论】:

          【解决方案5】:

          MyScript.ps1 的简单方法:

          Param( [switch]$sql, [string]$_ValueSql )
          if ($_ValueSql){​​​​​​​
              [String]$sql=$_ValueSql
          }​​​​​​​ else {​​​​​​​
              [String]$sql=""
          }​​​​​​​
          "sql=$sql"
          
          
          Results:
          
          .\MyScript.ps1
          sql=
          
          .\MyScript.ps1 -s
          sql=
          
          .\MyScript.ps1 -s abcd
          sql=abcd
          

          【讨论】:

            猜你喜欢
            • 2020-06-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多