Reputation: 77
I have a script, pingtest.ps1
, which makes the following call:
Start-abc -SourceIpaddress x.y.x.z -Protocol TCP -DestinationIpAddress a.c.d.e
During execution it prompts for the source port and destination port.
How do I avoid these interactive prompts and instead pass the values in the same script?
Upvotes: 0
Views: 1622
Reputation: 437111
tl;dr
Adding something like -SourcePort 42
and -DestinationPort 666
to your Start-abc
call should prevent these interactive prompts (of course you must supply appropriate values in lieu of the sample values 42
and 666
used here).
To determine the exact name of these parameters and for background information, read on.
If invocation of a command doesn't supply arguments to all of its mandatory parameters (parameters that require values in order for the command to operate):
Note: This prompting mechanism is rarely used in practice and doesn't work with all parameter types - see GitHub issue #4068.
If you simply want to prevent interactive prompts at runtime, run your command via a new PowerShell instance started with -noninteractive
:
powershell.exe -noninteractive -file C:\path\to\pingtest.ps1
This way your script won't prompt and report an error instead, which indicates the name of the (first) mandatory parameter for which no argument was provided.
Caveat: With default error handling in effect, script execution will still continue; setting $ErrorActionPreference = 'Stop'
would prevent that.
If you want to determine at design time which parameters are mandatory, use Get-Command <command> -Syntax
.
If necessary and available, supplement with Get-Help -Detailed <command>
(or -Full
) or Get-Help -Parameter <parameterName> <command>
to learn specifics about the parameters.
Note, however, that custom scripts and functions may not have help information associated with them.
PowerShell's syntax diagrams, documented in the conceptual about_Command_Syntax help topic, aren't the easiest to decipher, but they do tell you which parameters are mandatory.
Using the Get-Item
cmdlet as an example:
PS> Get-Command Get-Item -Syntax
Get-Item [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-UseTransaction] [-Stream <string[]>] [<CommonParameters>]
Get-Item -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-UseTransaction] [-Stream <string[]>] [<CommonParameters>]
Each output line represents a distinct parameter set.
What parameters distinguish one parameters set from another isn't readily obvious; in this case, it is -Path
vs. -LiteralPath
, which is common for provider-related cmdlets.
In a given parameter set, any parameter that is not enclosed in [...]
as a whole (around both the parameter name (e.g., -LiteralPath
) and the parameter type (e.g., <string[]>
)) is mandatory.
[...]
(e.g., [-Path]
), the parameter is also mandatory, but arguments may be specified positionally - that is, you may omit the parameter name and specify just the argument (the value to bind to the parameter); e.g., instead of Get-Item -Path foo.txt
you may specify just Get-Item foo.txt
.Based on the above, armed with the information as to:
you can then call your command in a manner that provides arguments to all mandatory parameters and thereby avoids interactive prompts.
Upvotes: 2