Powershell Switch Parameters No Values Required
Mar 3, 2021
--
The aim of this tutorialš is the lesson needed for starting to use Powershell's functions with just -parameter
without the provision of any value
1. steps
- this feature is called switch parameters
- handled in about_Functions_Advanced_Parameters ā PowerShell @ Microsoft Docs
- the control flow is handled elegantly with a truthy test against the value
if($switchname) {activated}
- you need to set a proper type
[switch]
before the argument - this works also in simple inline functions without the lengthy
param
block
2. example: query hashicorp consul
- this is the local script I am using during operations to lookup a given value from within the HashiCorps Consul database
- there are other things I am implementing: a short command-help, and argument typing
- the prereq to using this is
- ā Consul installed
- ā Consul authenticated
>>> query-consul com_acme md5 -detailed