简体   繁体   中英

Passing switch parameter thru pipeline in PowerShell

Passing switch parameter thru pipeline in PowerShell

Problem

I am trying to make a function that has a switch parameter, but also I want to able to pass all function parameters thru pipeline in a script, and I don't know ho to do that. Is it that even possible? I my case I load parameters from .csv file in witch values are string values.

Exposition

To simplify my problem and to make it easier for others to use answers of this question, I am not going to use my code but an abstract version of my code. Let us call my function New-Function that has a -StringParameter , a -IntParameter and a -SwitchParameter parameters. And just to be clear in my .csv file all fields are named same as the New-Function parameters.

Using the function

Normally I you can use the New-Function this way:

New-Function -StringParameter "value" -IntParameter 123 -SwitchParameter

But I also want to use the New-Function this way:

$Data = Import-Csv -Path "$PSScriptRoot\Data.csv" -Delimiter ';'
$Data | New-Function

My attempts

I have tried to convert the string values in pipe line to boolean but it seems like the function's -SwitchParameter does not accept boolean ( $true , $false ) values, because it skipping the process block completely when I debug it.

$Data | ForEach-Object -Process {
    if ($_.SwitchParameter -eq "true") {
        $_.SwitchParameter = $true
    }
    else {
    $_.SwitchParameter = $false
    }
} | New-Function

My temporary workaround

I have settled to use a string parameter instead of a switch parameter, so I can feed the New-Function with data thru pipeline from a .csv file with no problem.

function New-Function {
    param (
        [Parameter(Position = 0, Mandatory, ValueFromPipelineByPropertyName)]
        [string]
        $StringParameter,

        [Parameter(Position = 1, Mandatory, ValueFromPipelineByPropertyName)]
        [int]
        $IntParameter,

        [Parameter(Position = 2, ValueFromPipelineByPropertyName)]
        [string]
        $SwitchParameter = "false"
    )
#----------------------------------------------------------------------------
}

You have to convert values for switch parameter to boolean type.

It works to me:

function Out-Test
{
    param
    (
        [Parameter(ValueFromPipelineByPropertyName)]
        [String]
        $Label,
        [Parameter(ValueFromPipelineByPropertyName)]
        [Switch]
        $Show
    )
    process
    {
        $Color = if ($Show) { 'Yellow' } else { 'Gray' }
        Write-Host -ForegroundColor $Color $Label
    }
}

$row1 = '' | select Label, Show
$row1.Label = 'First'
$row1.Show = 'True'
$row2 = '' | select Label, Show
$row2.Label = 'Second'
$row1.Show = 'False'
$rows = $row1, $row2

$rows |% { $_.Show = [bool]$_.Show }
$rows | Out-Test

Result:

在此处输入图片说明

You can convert your string to a Boolean object while leaving your parameter as type [switch] in your function. The Boolean type will be coerced into [switch] during binding.

$Data | Foreach-Object {
    $_.SwitchParameter = [boolean]::Parse($_.SwitchParameter)
    $_
} | New-Function

Alternatively, you can update all of your objects first and then pipe to your function. It matters how your function handles the input objects.

$Data | Foreach-Object {
    $_.SwitchParameter = [boolean]::Parse($_.SwitchParameter)
}
$Data | New-Function

Part of the issue with your Foreach-Object attempt is that you never output the updated object $_ before piping into your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM