Dynacel
Dynacel

Reputation: 43

Change Variable with Switch in Powershell

I want to set $url1 based on different switches that are available. I'm using this to be able to set the download URL based on location using a switch. I have stripped everything else from the script to make it quicker for anyone that's trying to help.

Param (
    [switch] $newyork,
    [switch] $florida,
    [switch] $california

)

$url1 = if ($newyork) {"https://newyork.com"}
    elseif ($florida) {"https://florida.com"}
    elseif ($california) {"https://california.com"}

Write-Host $url1

This script works perfectly fine, but I want to make sure that this is the most efficient way to do this.

In particular, I'm curious if there's any way to do this, but keep it all in the Param block. (Doing so will help me with consistency with some of my other scripts.)

Upvotes: 3

Views: 635

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25021

What you are currently doing will work, and there's probably not an issue with it regarding performance or efficiency. However, one could use one, two, or all three switches when running the script or function. Since you are doing if and elseif blocks, the first one that evaluates to true will have its code block executed with the remaining blocks being ignored. Below are some other approaches that can be used.


Option 1: Any number of switches can be used at any time

Param (
    [switch] $newyork,
    [switch] $florida,
    [switch] $california

)

if ($newyork) {"https://newyork.com"}
if ($florida) {"https://florida.com"}
if ($california) {"https://california.com"}

You should go with only if statements if you want multiple switches to be used simultaneously.


Option 2: Only one switch can be used at once with parameter sets

function test {
  param(
    [Parameter(ParameterSetName='NewYork',Mandatory)]
    [switch]$newyork,
    [Parameter(ParameterSetName='florida',Mandatory)]
    [switch]$florida,
    [Parameter(ParameterSetName='california',Mandatory)]
    [switch]$california
  )
  if ($newyork) {
    "newyork"
  }
  if ($florida) {
    "florida"
  }
  if ($california) {
    "california"
  }
}

This requires that a single switch be used. More or less will throw an error.


Option 3: Using enumerated types to force the value of a single parameter

enum Location
{
    newyork
    florida
    california
}
function test {
    param(
        [Parameter(Mandatory)]
        [Location]$location
    )

    "https://{0}.com" -f $location
}

Enumerated types have their place. I like them for this solution. However, it will attempt to parse the input, which could cause unwanted results. For example, test new will bind newyork to $location.


Option 4: Validating a string of a single parameter

function test {
    param(
        [Parameter(Mandatory)]
        [ValidateSet('newyork','florida','california')]
        [string]$location
    )

    "https://{0}.com" -f $location
}

This requires that one of the three strings be entered when running the function. Using the string format operator (-f) does allow us to shorten the code and remove the entire if logic.

Upvotes: 2

Related Questions