Import-CSV with a switch parameter

Welcome Forums General PowerShell Q&A Import-CSV with a switch parameter

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #124130

    Participant
    Points: 15
    Rank: Member

    I have a New-User script that works great from the command line.

    New-User -Name BDole -FirstName Bob -LastName Dole -Email

    Simple enough, email is just $Name + $Domain so I don't ask that it be provided. My issue is, I use

    [switch]$Email

    because some staff get email and some don't. I am trying to get this to work with

    Import-CSV some.csv | New-User

    But it comes in as a string from the CSV. I can make it work by using boolean, but then the user creating the account would have to specify

    -Email:$true

    which complicates things (basically, the more complicated things become, the more likely I am going to be the new user guy (and nobody wants that)). Am I trying to do the impossible or am I just doing it wrong?

    Basic test script (using boolean):

    function testme {
       param (
          [parameter(ValueFromPipelineByPropertyName = $true)]
          $waffle
       )
       [System.Convert]::ToBoolean($waffle) | Out-Null
       switch ($true){
          $waffle {Write-Host "Waffles"}
       }
    }
  • #124188

    Participant
    Points: 290
    Helping Hand
    Rank: Contributor

    It comes in as a string "true" or "false" from the CSV?

    Mkay, you can use [bool]::Parse($string) to get that into a PowerShell boolean value for you. đŸ™‚

    You're not really that clear on what the intended user-facing behaviour should be, though. If you're importing a CSV you can have your function check for that and convert appropriate values to bool behind the scenes.

  • #124205

    Participant
    Points: 15
    Rank: Member

    I want them to be able to just use the command line for onsey twosey, but at the end of summer, we have a large influx and making a CSV would be very helpful. From the CSV I would have them coming in as True/False for clarity. How would I have the function check that I am importing CSV? I want to be able to use it 2 ways

    New-User -Name BDole -FirstName Bob -LastName Dole -Email

    and

    Import-CSV some.csv | New-User

    and the CSV just has headers for "Name" "FirstName" "LastName" and "Email", with Email being true/false

     

  • #124212

    Participant
    Points: 290
    Helping Hand
    Rank: Contributor

    I would then probably have -Email be a [string] parameter in a separate parameter set, and have regular users instead use a switch which is perhaps something like -CreateEmail or similar. That way piping the CSV directly in works as expected, and users don't need to worry about the other parameter as they can just use the switch.

    In your function's process block you can set the $CreateEmail as though it were provided from the switch using the CSV-input value instead, and making use of [bool]::Parse($Email)

  • #124218

    Participant
    Points: 15
    Rank: Member

    Thank you, I'll do that. They are used to working with CSV templates, so I'll send them one with the correct headers filled in and probably omit the [string] parameters from the help. Thank you for your help

You must be logged in to reply to this topic.