我有一个用C#编写的自定义cmdlet,它将SwitchParameter作为其参数之一,我想使用import-csv执行我的cmdlet,我应该如何编写我的csv,以便能够传递正确的SwitchParameter值?
我已经尝试了True,False,0,1,在CSV中带引号和不带引号,但是它们似乎不起作用,我的代码中总是出现false
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName=true)]
public SwitchParameter Enable { get; set; }我运行的是Powershell 2.0版,我要执行的命令是:
Import-Csv c:\data.csv | Add-MyData发布于 2013-05-20 18:14:03
使用Import-CSV时,所有属性都是string-objects。因此,如果您使用0和1,则需要将其强制转换为int,并将转换为bool。例如:
test.csv
Name,Enabled
"Hey",1
"Lol",0脚本:
Import-Csv .\test.csv | % { $_.Enabled = [bool]($_.Enabled -as [int]); $_ }
#You could also cast it with [bool]([int]$_.Enabled), I just like to mix it up :)
Name Enabled
---- -------
Hey True
Lol False然后您可以将其传递给您的交换机,如下所示:
#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 Falsehttps://stackoverflow.com/questions/16646855
复制相似问题