How to suppress errors that occur when a user calls my powershell script without a parameter argument

For example, my script can be called as follows:

.\MyScript.ps1 -s <hostname1> 

If I call it without passing an argument with the -s option, I get an error message:

 .\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 

Is there a way to suppress this error or create a custom error:

 .\MyScript.ps1 -s Please pass a hostname with the s argument: .\MyScript.ps1 -s <hostname> 
+4
source share
4 answers

You can do 1 of 2 things.

Mark the parameter as required, then powershell will automatically ask the user:

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

which gives:

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

or you can provide the expression as a default parameter, requesting input from the user:

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

which gives:

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

Edit:

In response to your comment. The only workarounds that I can think of to get the desired behavior are either to not specify the parameters, but to process the $ args argument myself. Or you can use a hack that pushes the task to parse the arguments into a separate function, and then delays any errors that may occur when this function is called. So your script will look like this:

 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 

which gives:

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

You can try using a trap handler to provide custom messages when the argument is not passed, and then continue. Trap handlers work for the current area, so you can simply define it at the top of your script to handle your errors.

eg:

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

You can also tell PowerShell not to warn you when an error occurs in this script by setting

$ErrorActionPreference = SilentlyContinue

This will force PowerShell to quietly suppress the error and try to continue executing the script. However, for obvious reasons, this is probably not the best idea to suppress your mistakes.

0
source

Parameter ([parameter (required = $ true, helpmessage = "you stupid goat.")] $ SQL)

please excuse me on the phone;)

0
source

If I read your intentions correctly, you want to get an error, but don’t tell me ... like this:

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

You can use the required parameter v1.

0
source

All Articles