Import-CSV with a switch parameter

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 [pre][switch]$Email[/pre] 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 [pre]-Email:$true[/pre] 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"}
   }
}

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. :slight_smile:

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.

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

 

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)

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