powershell - how do I turn on a param switch from an env variable? - Stack Overflow

admin2025-04-19  3

How do I turn -one on with the environment? "-one" Ends up in the opts string if I don't specify one, or it's ignored.

function test-run {
  Param (
    [switch]$one = $false,
    [switch]$two = $false,
    [switch]$tre = $false,
    [string]$branch = "release",
    [int]$forkid = -1,
    [string]$opts = ""
  )
  "one: $one, two: $two, tre: $tre" | Out-Host
  "branch: $branch, forkid: $forkid, opts: $opts" | Out-Host
}
$env:setupflag = "-one"
test-run $env:setupflag -two -tre -branch "test" -forkid 0 -opts ""

test-param
one: False, two: True, tre: True
branch: test, forkid: 0, opts:

How do I turn -one on with the environment? "-one" Ends up in the opts string if I don't specify one, or it's ignored.

function test-run {
  Param (
    [switch]$one = $false,
    [switch]$two = $false,
    [switch]$tre = $false,
    [string]$branch = "release",
    [int]$forkid = -1,
    [string]$opts = ""
  )
  "one: $one, two: $two, tre: $tre" | Out-Host
  "branch: $branch, forkid: $forkid, opts: $opts" | Out-Host
}
$env:setupflag = "-one"
test-run $env:setupflag -two -tre -branch "test" -forkid 0 -opts ""

test-param
one: False, two: True, tre: True
branch: test, forkid: 0, opts:

Share Improve this question edited Mar 6 at 0:37 mklement0 443k68 gold badges711 silver badges928 bronze badges asked Mar 5 at 23:28 B.McKeeB.McKee 4421 gold badge3 silver badges18 bronze badges 1
  • If you want to make part of your code dependend of an environment variable why not checking this environment variable inside your function? – Olaf Commented Mar 6 at 0:15
Add a comment  | 

2 Answers 2

Reset to default 2
  • Since you're calling a PowerShell-native command (function), what you're trying to do requires splatting based on hashtables.

  • This isn't exactly straightforward if the splatting source is an environment variable:

$env:setupflag = '-one'

# Construct a hashtable that, when used with splatting, 
# in effect passes the -one switch.
# The literal equivalent of this is:
#   $htSetupFlag = @{
#     one = $true
#   }
$htSetupFlag = @{
  $env:setupflag.Substring(1) = $true
}

# Pass the hashtable via sigil @ instead of $ in order to perform splatting
test-run @htSetupFlag -two -tre -branch "test" -forkid 0 -opts ""

I'll defer to @mklement0's answer for the main question, but using [switch] with a $false default is quite discouraged.
It's pretty much against their whole point as they are "off-by-default".

use the .IsPresent property of the switch parameters, instead:

function test-run {
    Param (
        [switch]$one,
        [switch]$two,
        [switch]$tre,
        [string]$branch = 'release',
        [int]$forkid = -1,
        [string]$opts = ''
    )

    if($one.IsPresent){
        # do stuff when -one is used
    }else{
        # do stuff if -one is not used
    }
}

...unless you specifically need a true boolean value, in that case use [bool] instead of [switch]

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002514a279316.html

最新回复(0)