Powershell - How to use import-csv for SwitchParameter -
i have custom cmdlet written in c# takes switchparameter 1 of arguments, want execute cmdlet using import-csv, how should write csv able pass correct switchparameter value?
i have tried true, false, 0 , 1, , without quotes in csv dont seem work, false in code
[parameter(mandatory = false, valuefrompipelinebypropertyname=true)] public switchparameter enable { get; set; } i running powershell version 2.0, command want execute :
import-csv c:\data.csv | add-mydata
when use import-csv, properties string-objects. if use 0 , 1, need cast int, , bool. ex:
test.csv
name,enabled "hey",1 "lol",0 script:
import-csv .\test.csv | % { $_.enabled = [bool]($_.enabled -as [int]); $_ } #you cast [bool]([int]$_.enabled), mix :) name enabled ---- ------- hey true lol false you can pass switch, like:
#my test-func function testfunc ($name, [switch]$enabled) { "$name has switchvalue $enabled" } import-csv .\test.csv | % { $_.enabled = [bool]($_.enabled -as [int]) testfunc -name $_.name -enabled:$_.enabled } hey has switchvalue true lol has switchvalue false
Comments
Post a Comment