来自管道的开关参数的值
Value for switch parameter from pipeline
我需要通过从 CSV 文件导入所需的值,将参数从管道输入传递给脚本。原始脚本有超过 15 个参数要传递,输入值存储在 CSV 文件中。我发布了一个简单的例子来表达这个问题。
以下是CSV文件的内容(Input.csv
)
ResourceGroupName,SetThrottling
TestRG1,1
TestRG2,0
TestRG3,1
TestRG4,0
TestRG5,0
脚本文件 - Switch-FromPipelineTest.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[String]$ResourceGroupName,
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[Switch]$SetThrottling
)
Begin {}
Process {
Function TestingValues {
Param(
$ResourceGroupName,
$SetThrottling
)
Write-Host "$ResourceGroupName is set to $SetThrottling"
}
TestingValues -ResourceGroupName $ResourceGroupName -SetThrottling $SetThrottling
}
End {}
如果我 运行 命令 Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1
它会给出如下错误:
C:\Scripts\Switch-FromPipelineTest.ps1 : Cannot process argument
transformation on parameter 'SetThrottling'. Cannot convert value
"System.String" to type "System.Management.Automation.SwitchParameter".
Boolean parameters accept only Boolean values and numbers, such as
$True, $False, 1 or 0.
At line:1 char:26
+ Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (@{ResourceGroup...etThrottling=1}:PSObject) [Swith-FromPipelineTest.ps1], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Switch-FromPipelineTest.ps1
为了完成这项工作,我必须 运行 下面的命令:
Import-Csv -Path .\Input.csv -Verbose |
Select-Object -Property ResourceGroupName,@{n='SetThrottling';e={[bool][int]$_.SetThrottling}} |
.\Switch-FromPipelineTest.ps1
有没有办法可以省略在第二个命令中使用自定义 属性 表达式完成的类型转换?与原始脚本一样,我有几个 [switch]
参数,我需要为每个 [switch]
参数做同样的事情。
导入 CSV 后将字符串值转换为布尔值:
Import-Csv 'C:\path\to\input.csv' |
Select-Object -Property *,@{n='SetThrottling';e={
[bool][int]$_.SetThrottling
}} -Exclude SetThrottling | ...
您需要为每个要从 CSV 导入的开关参数执行此操作。
如果您想避免这种情况,请将您的参数从开关更改为设置验证的参数,并相应地调整参数评估:
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[ValidateSet('0', '1')]
[string]$SetThrottling
我需要通过从 CSV 文件导入所需的值,将参数从管道输入传递给脚本。原始脚本有超过 15 个参数要传递,输入值存储在 CSV 文件中。我发布了一个简单的例子来表达这个问题。
以下是CSV文件的内容(Input.csv
)
ResourceGroupName,SetThrottling
TestRG1,1
TestRG2,0
TestRG3,1
TestRG4,0
TestRG5,0
脚本文件 - Switch-FromPipelineTest.ps1
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[String]$ResourceGroupName,
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[Switch]$SetThrottling
)
Begin {}
Process {
Function TestingValues {
Param(
$ResourceGroupName,
$SetThrottling
)
Write-Host "$ResourceGroupName is set to $SetThrottling"
}
TestingValues -ResourceGroupName $ResourceGroupName -SetThrottling $SetThrottling
}
End {}
如果我 运行 命令 Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1
它会给出如下错误:
C:\Scripts\Switch-FromPipelineTest.ps1 : Cannot process argument
transformation on parameter 'SetThrottling'. Cannot convert value
"System.String" to type "System.Management.Automation.SwitchParameter".
Boolean parameters accept only Boolean values and numbers, such as
$True, $False, 1 or 0.
At line:1 char:26
+ Import-Csv .\Input.csv | .\Switch-FromPipelineTest.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (@{ResourceGroup...etThrottling=1}:PSObject) [Swith-FromPipelineTest.ps1], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Switch-FromPipelineTest.ps1
为了完成这项工作,我必须 运行 下面的命令:
Import-Csv -Path .\Input.csv -Verbose |
Select-Object -Property ResourceGroupName,@{n='SetThrottling';e={[bool][int]$_.SetThrottling}} |
.\Switch-FromPipelineTest.ps1
有没有办法可以省略在第二个命令中使用自定义 属性 表达式完成的类型转换?与原始脚本一样,我有几个 [switch]
参数,我需要为每个 [switch]
参数做同样的事情。
导入 CSV 后将字符串值转换为布尔值:
Import-Csv 'C:\path\to\input.csv' |
Select-Object -Property *,@{n='SetThrottling';e={
[bool][int]$_.SetThrottling
}} -Exclude SetThrottling | ...
您需要为每个要从 CSV 导入的开关参数执行此操作。
如果您想避免这种情况,请将您的参数从开关更改为设置验证的参数,并相应地调整参数评估:
[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[ValidateSet('0', '1')]
[string]$SetThrottling